Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
update passkey register flow
Marc Seitzcb39f47cba1f
diff --git a/lib/api/auth/passkey.ts b/lib/api/auth/passkey.tsindex 65e5e7fd..969db583 100644--- a/lib/api/auth/passkey.ts+++ b/lib/api/auth/passkey.ts@@ -1,12 +1,13 @@-"use server";-import hanko from "@/lib/hanko";import { CustomUser } from "@/lib/types";-import { getServerSession } from "next-auth";+import { type Session } from "next-auth";import prisma from "@/lib/prisma";-export async function startServerPasskeyRegistration() {- const session = await getServerSession();+export async function startServerPasskeyRegistration({+ session,+}: {+ session: Session;+}) {if (!session) throw new Error("Not logged in");const sessionUser = session.user as CustomUser;@@ -26,8 +27,13 @@ export async function startServerPasskeyRegistration() {// 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();+export async function finishServerPasskeyRegistration({+ credential,+ session,+}: {+ credential: any;+ session: Session;+}) {if (!session) throw new Error("Not logged in");await hanko.registration.finalize(credential);diff --git a/pages/api/passkeys/register.ts b/pages/api/passkeys/register.tsnew file mode 100644index 00000000..e95a8347--- /dev/null+++ b/pages/api/passkeys/register.ts@@ -0,0 +1,44 @@+import { NextApiRequest, NextApiResponse } from "next";+import { getServerSession } from "next-auth/next";+import { authOptions } from "../auth/[...nextauth]";+import { CustomUser } from "@/lib/types";+import { errorhandler } from "@/lib/errorHandler";+import {+ finishServerPasskeyRegistration,+ startServerPasskeyRegistration,+} from "@/lib/api/auth/passkey";++export default async function handler(+ req: NextApiRequest,+ res: NextApiResponse,+) {+ if (req.method === "POST") {+ const session = await getServerSession(req, res, authOptions);+ if (!session) {+ return res.status(401).end("Unauthorized");+ }++ const { start, finish, credential } = req.body as {+ start: boolean;+ finish: boolean;+ credential: any;+ };++ try {+ if (start) {+ const createOptions = await startServerPasskeyRegistration({ session });+ res.status(200).json({ createOptions });+ return;+ }+ if (finish) {+ await finishServerPasskeyRegistration({ credential, session });+ res.status(200).json({ message: "Registered Passkey" });+ return;+ }+ } catch (error) {+ errorhandler(error, res);+ }+ }++ return res.status(405).json({ error: "Method not allowed" });+}diff --git a/pages/settings/general.tsx b/pages/settings/general.tsxindex e6a0c438..0b5ac535 100644--- a/pages/settings/general.tsx+++ b/pages/settings/general.tsx@@ -1,9 +1,13 @@import AppLayout from "@/components/layouts/app";import Navbar from "@/components/settings/navbar";-import { DeleteTeamModal } from "@/components/teams/delete-team-modal";+import Passkey from "@/components/shared/icons/passkey";import { Button } from "@/components/ui/button";import { Input } from "@/components/ui/input";import { useTeam } from "@/context/team-context";+import {+ create,+ type CredentialCreationOptionsJSON,+} from "@github/webauthn-json";import { useRef, useState } from "react";import { toast } from "sonner";import { mutate } from "swr";@@ -41,6 +45,33 @@ export default function General() {setTeamNameChanging(false);};+ async function registerPasskey() {+ const createOptionsResponse = await fetch("/api/passkeys/register", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ start: true, finish: false, credential: null }),+ });++ const { createOptions } = await createOptionsResponse.json();++ // Open "register passkey" dialog+ const credential = await create(+ createOptions as CredentialCreationOptionsJSON,+ );++ const response = await fetch("/api/passkeys/register", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ start: false, finish: true, credential }),+ });++ if (response.ok) {+ toast.success("Registered passkey successfully!");+ return;+ }+ // Now the user has registered their passkey and can use it to log in.+ }+return (<AppLayout><Navbar current="General" />@@ -71,23 +102,24 @@ export default function General() {</Button></div>- {/* <div className="p-10 rounded-lg border border-destructive">+ <div className="p-10 rounded-lg border border-muted"><div className="space-y-6"><div className="space-y-3">- <h2 className="text-xl font-medium">Delete Team</h2>+ <h2 className="text-xl font-medium">Register a passkey</h2><p className="text-sm text-secondary-foreground mt-3">- Permanently delete your team, custom domain, and all- associated documents + their stats. This action cannot be- undone - please proceed with caution.+ Never use a password or oauth again. Register a passkey to+ make logging in easy.</p></div>- <DeleteTeamModal>- <Button variant={"destructive"} size={"lg"}>- Delete Team- </Button>- </DeleteTeamModal>+ <Button+ onClick={() => registerPasskey()}+ className="flex justify-center items-center space-x-2"+ >+ <Passkey className="w-4 h-4" />+ <span>Register a new passkey</span>+ </Button></div>- </div> */}+ </div></div></div></AppLayout>diff --git a/pages/settings/security.tsx b/pages/settings/security.tsxnew file mode 100644index 00000000..e69de29b