|
1 | | -import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next'; |
| 1 | +import { NextApiRequest, NextApiResponse } from 'next'; |
| 2 | +import { getIronSession } from "iron-session"; |
| 3 | +import { SessionData, sessionOptions } from './session'; |
2 | 4 | import type { VerifiedAuthenticationResponse, VerifiedRegistrationResponse } from '@simplewebauthn/server'; |
3 | 5 | import { verifyAuthenticationResponse, verifyRegistrationResponse } from '@simplewebauthn/server'; |
4 | | -// import type { PublicKeyCredentialWithAssertionJSON, PublicKeyCredentialWithAttestationJSON } from '@github/webauthn-json'; |
5 | 6 | import crypto from 'crypto'; |
6 | 7 | import * as jose from 'jose'; |
7 | 8 | import objectPath from 'object-path'; |
8 | 9 |
|
9 | | -type SessionRequest = NextApiRequest | GetServerSidePropsContext["req"]; |
10 | | - |
11 | 10 | var user = process.env.COUCHDB_USER; |
12 | 11 | var pass = process.env.COUCHDB_PASSWORD; |
13 | 12 | const domain: string = process.env.DOMAIN !== undefined ? process.env.DOMAIN: ''; |
@@ -35,11 +34,13 @@ function clean(str: string) { |
35 | 34 | export function generateChallenge() { |
36 | 35 | return clean(crypto.randomBytes(32).toString("base64")); |
37 | 36 | } |
38 | | -export function isLoggedIn(req: SessionRequest) { |
39 | | - return req.session.userId != null; |
40 | | -} |
41 | | -export async function register(req: NextApiRequest) { |
42 | | - const challenge = req.session.challenge ?? ""; |
| 37 | +export async function register(req: NextApiRequest, res: NextApiResponse) { |
| 38 | + const session = await getIronSession<SessionData>( |
| 39 | + req, |
| 40 | + res, |
| 41 | + sessionOptions, |
| 42 | + ); |
| 43 | + const challenge = session.challenge ?? ""; |
43 | 44 | const credential = req.body.credential as any; |
44 | 45 | const { email } = req.body; |
45 | 46 | let verification: VerifiedRegistrationResponse; |
@@ -79,8 +80,13 @@ export async function register(req: NextApiRequest) { |
79 | 80 | console.log(`Registered new user ${req.body.email}`); |
80 | 81 | return user; |
81 | 82 | } |
82 | | -export async function login(req: NextApiRequest) { |
83 | | - const challenge = req.session.challenge ?? ""; |
| 83 | +export async function login(req: NextApiRequest, res: NextApiResponse) { |
| 84 | + const session = await getIronSession<SessionData>( |
| 85 | + req, |
| 86 | + res, |
| 87 | + sessionOptions, |
| 88 | + ); |
| 89 | + const challenge = session.challenge ?? ""; |
84 | 90 | const credential = req.body.credential; |
85 | 91 | const email = req.body.email; |
86 | 92 | if (credential?.id == null) { |
|
0 commit comments