Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: hanko passkeys
Marc Seitzd3b56a2fa80b
diff --git a/app/register/page.tsx b/app/register/page.tsxnew file mode 100644index 00000000..7fd67d62--- /dev/null+++ b/app/register/page.tsx@@ -0,0 +1,84 @@+"use client";++import { Button } from "@/components/ui/button";+import { signIn } from "next-auth/react";+import Link from "next/link";+import { useRouter, useParams } from "next/navigation";+import Passkey from "@/components/shared/icons/passkey";+import {+ finishServerPasskeyRegistration,+ startServerPasskeyRegistration,+} from "@/lib/api/auth/passkey";+import { create } from "@github/webauthn-json";++export default function Register() {+ const router = useRouter();+ const { next } = useParams as { next?: string };++ async function registerPasskey() {+ const createOptions = await startServerPasskeyRegistration();++ // Open "register passkey" dialog+ const credential = await create(createOptions as any);++ await finishServerPasskeyRegistration(credential);++ // Now the user has registered their passkey and can use it to log in.+ }++ return (+ <div className="flex h-screen w-screen justify-center">+ <div+ className="absolute inset-x-0 top-10 -z-10 flex transform-gpu justify-center overflow-hidden blur-3xl"+ aria-hidden="true"+ >+ <div+ className="aspect-[1108/632] w-[69.25rem] flex-none bg-gradient-to-r from-[#80caff] to-[#4f46e5] opacity-20"+ style={{+ clipPath:+ "polygon(73.6% 51.7%, 91.7% 11.8%, 100% 46.4%, 97.4% 82.2%, 92.5% 84.9%, 75.7% 64%, 55.3% 47.5%, 46.5% 49.4%, 45% 62.9%, 50.3% 87.2%, 21.3% 64.1%, 0.1% 100%, 5.4% 51.1%, 21.4% 63.9%, 58.9% 0.2%, 73.6% 51.7%)",+ }}+ />+ </div>+ <div className="z-10 mt-[calc(30vh)] h-fit w-full mx-5 sm:mx-0 max-w-md overflow-hidden border border-border bg-gray-50 dark:bg-gray-900 rounded-lg sm:shadow-xl">+ <div className="flex flex-col items-center justify-center space-y-3 px-4 py-6 pt-8 text-center sm:px-16">+ <Link href="/">+ <span className="text-xl font-bold tracking-tighter text-foreground">+ Papermark+ </span>+ </Link>+ <h3 className="text-2xl text-foreground font-medium">+ Start sharing documents+ </h3>+ </div>+ <div className="flex flex-col px-4 py-8 sm:px-16">+ <Button+ onClick={() => {+ signIn("google", {+ ...(next && next.length > 0 ? { callbackUrl: next } : {}),+ });+ }}+ className="flex justify-center items-center space-x-2"+ >+ <svg+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 488 512"+ fill="currentColor"+ className="h-4 w-4"+ >+ <path d="M488 261.8C488 403.3 391.1 504 248 504 110.8 504 0 393.2 0 256S110.8 8 248 8c66.8 0 123 24.5 166.3 64.9l-67.5 64.9C258.5 52.6 94.3 116.6 94.3 256c0 86.5 69.1 156.6 153.7 156.6 98.2 0 135-70.4 140.8-106.9H248v-85.3h236.1c2.3 12.7 3.9 24.9 3.9 41.4z" />+ </svg>+ <span>Continue with Google</span>+ </Button>+ <Button+ onClick={() => registerPasskey().then(router.refresh)}+ className="flex justify-center items-center space-x-2"+ >+ <Passkey className="w-4 h-4" />+ <span>Register a passkey</span>+ </Button>+ </div>+ </div>+ </div>+ );+}diff --git a/components/shared/icons/passkey.tsx b/components/shared/icons/passkey.tsxnew file mode 100644index 00000000..9a045e5f--- /dev/null+++ b/components/shared/icons/passkey.tsx@@ -0,0 +1,15 @@+export default function Passkey({ className }: { className?: string }) {+ return (+ <svg+ width="16"+ height="16"+ viewBox="0 0 16 16"+ fill="currentColor"+ className={className}+ shapeRendering="geometricPrecision"+ >+ <path d="M2.743 4.757a3.757 3.757 0 1 1 5.851 3.119 5.991 5.991 0 0 1 2.15 1.383c.17.17.257.405.258.646.003.598.001 1.197 0 1.795L11 12.778v.721a.5.5 0 0 1-.5.5H1.221a.749.749 0 0 1-.714-.784 6.004 6.004 0 0 1 3.899-5.339 3.754 3.754 0 0 1-1.663-3.119Z"></path>+ <path d="M15.75 6.875c0 .874-.448 1.643-1.127 2.09a.265.265 0 0 0-.123.22v.59c0 .067-.026.13-.073.177l-.356.356a.125.125 0 0 0 0 .177l.356.356c.047.047.073.11.073.176v.231c0 .067-.026.13-.073.177l-.356.356a.125.125 0 0 0 0 .177l.356.356c.047.047.073.11.073.177v.287a.247.247 0 0 1-.065.168l-.8.88a.52.52 0 0 1-.77 0l-.8-.88a.247.247 0 0 1-.065-.168V9.185a.264.264 0 0 0-.123-.22 2.5 2.5 0 1 1 3.873-2.09ZM14 6.5a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Z"></path>+ </svg>+ );+}diff --git a/lib/api/auth/passkey.ts b/lib/api/auth/passkey.tsnew file mode 100644index 00000000..c660e220--- /dev/null+++ b/lib/api/auth/passkey.ts@@ -0,0 +1,49 @@+"use server";++import hanko from "@/lib/hanko";+import { CustomUser } from "@/lib/types";+import { getServerSession } from "next-auth";+import prisma from "@/lib/prisma";++export async function startServerPasskeyRegistration() {+ const session = await getServerSession();+ if (!session) throw new Error("Not logged in");++ const sessionUser = session.user as CustomUser;++ const user = await prisma.user.findUnique({+ where: { email: sessionUser.email as string },+ select: { id: true, name: true },+ });++ console.log("user prisma", user);++ const createOptions = await hanko.registration.initialize({+ userId: user!.id,+ username: user!.name,+ });++ return createOptions;+}++// This is *your* server-side code; you need to implement this yourself.+// NextAuth takes care of logging in the user after they have registered their passkey.+export async function finishServerPasskeyRegistration(credential: any) {+ const session = await getServerSession();+ if (!session) throw new Error("Not logged in");++ await hanko.registration.finalize(credential);++ // Now the user has registered their passkey and can use it to log in.++ const user = await prisma.user.findUnique({+ where: { id: (session.user as CustomUser).id },+ select: { id: true },+ });++ console.log("user has registered their passkey", user?.id);++ // if (user) {+ // user.hasPasskeys = true;+ // }+}diff --git a/lib/hanko.ts b/lib/hanko.tsnew file mode 100644index 00000000..5969f827--- /dev/null+++ b/lib/hanko.ts@@ -0,0 +1,16 @@+import { tenant } from "@teamhanko/passkeys-next-auth-provider";++if (!process.env.HANKO_API_KEY || !process.env.HANKO_TENANT_ID) {+ // These need to be set in .env.local+ // You get them from the Passkey API itself, e.g. when first setting up the server.+ throw new Error(+ "Please set HANKO_API_KEY and HANKO_TENANT_ID in your .env.local file.",+ );+}++const hanko = tenant({+ apiKey: process.env.HANKO_API_KEY!,+ tenantId: process.env.HANKO_TENANT_ID!,+});++export default hanko;diff --git a/middleware.ts b/middleware.tsindex 8328244f..9887e266 100644--- a/middleware.ts+++ b/middleware.ts@@ -23,16 +23,14 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {if (process.env.NODE_ENV !== "development" &&- !(- host?.includes("papermark.io") ||- host?.endsWith(".vercel.app")- )+ !(host?.includes("papermark.io") || host?.endsWith(".vercel.app"))) {return DomainMiddleware(req);}if (path !== "/" &&+ path !== "/register" &&path !== "/privacy" &&path !== "/oss-friends" &&path !== "/pricing" &&diff --git a/package-lock.json b/package-lock.jsonindex 7a343d93..b0966820 100644--- a/package-lock.json+++ b/package-lock.json@@ -10,6 +10,7 @@"hasInstallScript": true,"dependencies": {"@chronark/zod-bird": "^0.3.0",+ "@github/webauthn-json": "^2.1.1","@headlessui/react": "^1.7.17","@heroicons/react": "^2.0.18","@jitsu/js": "^1.7.1",@@ -29,6 +30,7 @@"@react-email/components": "^0.0.11","@stripe/stripe-js": "^2.1.7","@tailwindcss/typography": "^0.5.10",+ "@teamhanko/passkeys-next-auth-provider": "^0.1.8","@tremor/react": "^3.11.1","@trigger.dev/nextjs": "^2.2.6","@trigger.dev/react": "^2.2.6",@@ -807,6 +809,14 @@"resolved": "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.1.4.tgz","integrity": "sha512-qprfWkn82Iw821mcKofJ5Pk9wgioHicxcQMxx+5zt5GSKoqdWvgG5AxVmpmUUjzTLPVSH5auBrhI93Deayn/DA=="},+ "node_modules/@github/webauthn-json": {+ "version": "2.1.1",+ "resolved": "https://registry.npmjs.org/@github/webauthn-json/-/webauthn-json-2.1.1.tgz",+ "integrity": "sha512-XrftRn4z75SnaJOmZQbt7Mk+IIjqVHw+glDGOxuHwXkZBZh/MBoRS7MHjSZMDaLhT4RjN2VqiEU7EOYleuJWSQ==",+ "bin": {+ "webauthn-json": "dist/bin/main.js"+ }+ },"node_modules/@headlessui/react": {"version": "1.7.17","resolved": "https://registry.npmjs.org/@headlessui/react/-/react-1.7.17.tgz",@@ -2812,6 +2822,35 @@}}},+ "node_modules/@teamhanko/passkeys-next-auth-provider": {+ "version": "0.1.8",+ "resolved": "https://registry.npmjs.org/@teamhanko/passkeys-next-auth-provider/-/passkeys-next-auth-provider-0.1.8.tgz",+ "integrity": "sha512-jFS9Pl+WqVahseTSNl0E+zsTUHF8gZdw1gQWbELF4J3/aoz/BiEwe+Qtp0qoTSX2HAybl1VLroWWerM4IQui1Q==",+ "dependencies": {+ "@github/webauthn-json": "^2.1.1",+ "@teamhanko/passkeys-sdk": "^0.1.7",+ "jose": "^5.1.1"+ },+ "peerDependencies": {+ "next-auth": "^4.24.5"+ }+ },+ "node_modules/@teamhanko/passkeys-next-auth-provider/node_modules/jose": {+ "version": "5.1.1",+ "resolved": "https://registry.npmjs.org/jose/-/jose-5.1.1.tgz",+ "integrity": "sha512-bfB+lNxowY49LfrBO0ITUn93JbUhxUN8I11K6oI5hJu/G6PO6fEUddVLjqdD0cQ9SXIHWXuWh7eJYwZF7Z0N/g==",+ "funding": {+ "url": "https://github.com/sponsors/panva"+ }+ },+ "node_modules/@teamhanko/passkeys-sdk": {+ "version": "0.1.7",+ "resolved": "https://registry.npmjs.org/@teamhanko/passkeys-sdk/-/passkeys-sdk-0.1.7.tgz",+ "integrity": "sha512-FHh67uucq99spGo6zjrAyhp+e8XRZg2g5s5vFcl1MWIzJI9ABoFVRnSuEMeXF+bG98nPY1javbNUWuKAx/gR8w==",+ "dependencies": {+ "openapi-fetch": "^0.8.1"+ }+ },"node_modules/@tootallnate/once": {"version": "2.0.0","resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",@@ -8062,6 +8101,19 @@"url": "https://github.com/sponsors/sindresorhus"}},+ "node_modules/openapi-fetch": {+ "version": "0.8.2",+ "resolved": "https://registry.npmjs.org/openapi-fetch/-/openapi-fetch-0.8.2.tgz",+ "integrity": "sha512-4g+NLK8FmQ51RW6zLcCBOVy/lwYmFJiiT+ckYZxJWxUxH4XFhsNcX2eeqVMfVOi+mDNFja6qDXIZAz2c5J/RVw==",+ "dependencies": {+ "openapi-typescript-helpers": "^0.0.5"+ }+ },+ "node_modules/openapi-typescript-helpers": {+ "version": "0.0.5",+ "resolved": "https://registry.npmjs.org/openapi-typescript-helpers/-/openapi-typescript-helpers-0.0.5.tgz",+ "integrity": "sha512-MRffg93t0hgGZbYTxg60hkRIK2sRuEOHEtCUgMuLgbCC33TMQ68AmxskzUlauzZYD47+ENeGV/ElI7qnWqrAxA=="+ },"node_modules/openid-client": {"version": "5.4.2","resolved": "https://registry.npmjs.org/openid-client/-/openid-client-5.4.2.tgz",@@ -11329,6 +11381,11 @@"resolved": "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.1.4.tgz","integrity": "sha512-qprfWkn82Iw821mcKofJ5Pk9wgioHicxcQMxx+5zt5GSKoqdWvgG5AxVmpmUUjzTLPVSH5auBrhI93Deayn/DA=="},+ "@github/webauthn-json": {+ "version": "2.1.1",+ "resolved": "https://registry.npmjs.org/@github/webauthn-json/-/webauthn-json-2.1.1.tgz",+ "integrity": "sha512-XrftRn4z75SnaJOmZQbt7Mk+IIjqVHw+glDGOxuHwXkZBZh/MBoRS7MHjSZMDaLhT4RjN2VqiEU7EOYleuJWSQ=="+ },"@headlessui/react": {"version": "1.7.17","resolved": "https://registry.npmjs.org/@headlessui/react/-/react-1.7.17.tgz",@@ -12568,6 +12625,31 @@"client-only": "0.0.1"}},+ "@teamhanko/passkeys-next-auth-provider": {+ "version": "0.1.8",+ "resolved": "https://registry.npmjs.org/@teamhanko/passkeys-next-auth-provider/-/passkeys-next-auth-provider-0.1.8.tgz",+ "integrity": "sha512-jFS9Pl+WqVahseTSNl0E+zsTUHF8gZdw1gQWbELF4J3/aoz/BiEwe+Qtp0qoTSX2HAybl1VLroWWerM4IQui1Q==",+ "requires": {+ "@github/webauthn-json": "^2.1.1",+ "@teamhanko/passkeys-sdk": "^0.1.7",+ "jose": "^5.1.1"+ },+ "dependencies": {+ "jose": {+ "version": "5.1.1",+ "resolved": "https://registry.npmjs.org/jose/-/jose-5.1.1.tgz",+ "integrity": "sha512-bfB+lNxowY49LfrBO0ITUn93JbUhxUN8I11K6oI5hJu/G6PO6fEUddVLjqdD0cQ9SXIHWXuWh7eJYwZF7Z0N/g=="+ }+ }+ },+ "@teamhanko/passkeys-sdk": {+ "version": "0.1.7",+ "resolved": "https://registry.npmjs.org/@teamhanko/passkeys-sdk/-/passkeys-sdk-0.1.7.tgz",+ "integrity": "sha512-FHh67uucq99spGo6zjrAyhp+e8XRZg2g5s5vFcl1MWIzJI9ABoFVRnSuEMeXF+bG98nPY1javbNUWuKAx/gR8w==",+ "requires": {+ "openapi-fetch": "^0.8.1"+ }+ },"@tootallnate/once": {"version": "2.0.0","resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",@@ -16342,6 +16424,19 @@"is-wsl": "^2.2.0"}},+ "openapi-fetch": {+ "version": "0.8.2",+ "resolved": "https://registry.npmjs.org/openapi-fetch/-/openapi-fetch-0.8.2.tgz",+ "integrity": "sha512-4g+NLK8FmQ51RW6zLcCBOVy/lwYmFJiiT+ckYZxJWxUxH4XFhsNcX2eeqVMfVOi+mDNFja6qDXIZAz2c5J/RVw==",+ "requires": {+ "openapi-typescript-helpers": "^0.0.5"+ }+ },+ "openapi-typescript-helpers": {+ "version": "0.0.5",+ "resolved": "https://registry.npmjs.org/openapi-typescript-helpers/-/openapi-typescript-helpers-0.0.5.tgz",+ "integrity": "sha512-MRffg93t0hgGZbYTxg60hkRIK2sRuEOHEtCUgMuLgbCC33TMQ68AmxskzUlauzZYD47+ENeGV/ElI7qnWqrAxA=="+ },"openid-client": {"version": "5.4.2","resolved": "https://registry.npmjs.org/openid-client/-/openid-client-5.4.2.tgz",diff --git a/package.json b/package.jsonindex eb970b77..50310eb9 100644--- a/package.json+++ b/package.json@@ -17,6 +17,7 @@},"dependencies": {"@chronark/zod-bird": "^0.3.0",+ "@github/webauthn-json": "^2.1.1","@headlessui/react": "^1.7.17","@heroicons/react": "^2.0.18","@jitsu/js": "^1.7.1",@@ -36,6 +37,7 @@"@react-email/components": "^0.0.11","@stripe/stripe-js": "^2.1.7","@tailwindcss/typography": "^0.5.10",+ "@teamhanko/passkeys-next-auth-provider": "^0.1.8","@tremor/react": "^3.11.1","@trigger.dev/nextjs": "^2.2.6","@trigger.dev/react": "^2.2.6",diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].tsindex 36c3e8eb..76914e3c 100644--- a/pages/api/auth/[...nextauth].ts+++ b/pages/api/auth/[...nextauth].ts@@ -5,6 +5,8 @@ import prisma from "@/lib/prisma";import { CreateUserEmailProps, CustomUser } from "@/lib/types";import { sendWelcomeEmail } from "@/lib/emails/send-welcome";import { analytics, identifyUser, trackAnalytics } from "@/lib/analytics";+import { PasskeyProvider } from "@teamhanko/passkeys-next-auth-provider";+import hanko from "@/lib/hanko";const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;@@ -19,6 +21,14 @@ export const authOptions: NextAuthOptions = {clientId: process.env.GOOGLE_CLIENT_ID as string,clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,}),+ PasskeyProvider({+ tenant: hanko,+ async authorize({ userId }) {+ const user = await prisma.user.findUnique({ where: { id: userId } });+ if (!user) return null;+ return user;+ },+ }),],adapter: PrismaAdapter(prisma),session: { strategy: "jwt" },@@ -51,6 +61,7 @@ export const authOptions: NextAuthOptions = {// @ts-ignore...(token || session).user,};+ console.log("session", session);return session;},},diff --git a/pages/login.tsx b/pages/login.tsxindex adb1d6fd..91e2be62 100644--- a/pages/login.tsx+++ b/pages/login.tsx@@ -1,7 +1,9 @@import { Button } from "@/components/ui/button";import { signIn } from "next-auth/react";+import { signInWithPasskey } from "@teamhanko/passkeys-next-auth-provider/client";import Link from "next/link";import { useRouter } from "next/router";+import Passkey from "@/components/shared/icons/passkey";export default function Login() {const router = useRouter();@@ -39,18 +41,29 @@ export default function Login() {...(next && next.length > 0 ? { callbackUrl: next } : {}),});}}- className="flex justify-center items-center"+ className="flex justify-center items-center space-x-2"><svgxmlns="http://www.w3.org/2000/svg"viewBox="0 0 488 512"fill="currentColor"- className="h-4 w-4 mr-2"+ className="h-4 w-4"><path d="M488 261.8C488 403.3 391.1 504 248 504 110.8 504 0 393.2 0 256S110.8 8 248 8c66.8 0 123 24.5 166.3 64.9l-67.5 64.9C258.5 52.6 94.3 116.6 94.3 256c0 86.5 69.1 156.6 153.7 156.6 98.2 0 135-70.4 140.8-106.9H248v-85.3h236.1c2.3 12.7 3.9 24.9 3.9 41.4z" /></svg><span>Continue with Google</span></Button>+ {/* <Button+ onClick={() =>+ signInWithPasskey({+ tenantId: process.env.HANKO_TENANT_ID as string,+ })+ }+ className="flex justify-center items-center space-x-2"+ >+ <Passkey className="w-4 h-4" />+ <span>Sign in with a passkey</span>+ </Button> */}</div></div></div>