mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-20 22:24:28 +01:00
Multiuser support (#60)
This commit is contained in:
44
auth.ts
Normal file
44
auth.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import NextAuth from "next-auth"
|
||||
import Credentials from "next-auth/providers/credentials"
|
||||
import { getUser } from "./app/actions/data"
|
||||
import { signInSchema } from "./lib/zod"
|
||||
import { SafeUser, SessionUser } from "./lib/types"
|
||||
|
||||
export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||
trustHost: true,
|
||||
providers: [
|
||||
Credentials({
|
||||
credentials: {
|
||||
username: {},
|
||||
password: {},
|
||||
},
|
||||
authorize: async (credentials) => {
|
||||
const { username, password } = await signInSchema.parseAsync(credentials)
|
||||
|
||||
// Pass the plaintext password to getUser for verification
|
||||
const user = await getUser(username, password)
|
||||
|
||||
if (!user) {
|
||||
throw new Error("Invalid credentials.")
|
||||
}
|
||||
|
||||
const safeUser: SessionUser = { id: user.id }
|
||||
return safeUser
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
jwt: async ({ token, user }) => {
|
||||
if (user) {
|
||||
token.id = (user as SessionUser).id
|
||||
}
|
||||
return token
|
||||
},
|
||||
session: async ({ session, token }) => {
|
||||
if (session?.user) {
|
||||
session.user.id = token.id as string
|
||||
}
|
||||
return session
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user