feat: support guest session (#919)

This commit is contained in:
Jeremy 2025-04-25 23:40:15 -07:00 committed by GitHub
parent 24cb2ce19b
commit 9279135355
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 741 additions and 288 deletions

View file

@ -1,14 +1,33 @@
import { compare } from 'bcrypt-ts';
import NextAuth, { type User, type Session } from 'next-auth';
import NextAuth, { type DefaultSession } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { getUser } from '@/lib/db/queries';
import { createGuestUser, getUser } from '@/lib/db/queries';
import { authConfig } from './auth.config';
import { DUMMY_PASSWORD } from '@/lib/constants';
import type { DefaultJWT } from 'next-auth/jwt';
interface ExtendedSession extends Session {
user: User;
export type UserType = 'guest' | 'regular';
declare module 'next-auth' {
interface Session extends DefaultSession {
user: {
id: string;
type: UserType;
} & DefaultSession['user'];
}
interface User {
id?: string;
email?: string | null;
type: UserType;
}
}
declare module 'next-auth/jwt' {
interface JWT extends DefaultJWT {
id: string;
type: UserType;
}
}
export const {
@ -40,27 +59,31 @@ export const {
if (!passwordsMatch) return null;
return user as any;
return { ...user, type: 'regular' };
},
}),
Credentials({
id: 'guest',
credentials: {},
async authorize() {
const [guestUser] = await createGuestUser();
return { ...guestUser, type: 'guest' };
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.id = user.id as string;
token.type = user.type;
}
return token;
},
async session({
session,
token,
}: {
session: ExtendedSession;
token: any;
}) {
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
session.user.id = token.id;
session.user.type = token.type;
}
return session;