Changeset 66bf4fd for server/authjs-handler.ts
- Timestamp:
- 12/16/25 01:31:19 (7 months ago)
- Branches:
- main
- Children:
- c30935a
- Parents:
- fdd776b
- File:
-
- 1 edited
-
server/authjs-handler.ts (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
server/authjs-handler.ts
rfdd776b r66bf4fd 3 3 import type { Session } from "@auth/core/types"; 4 4 import { enhance, type UniversalHandler, type UniversalMiddleware } from "@universal-middleware/core"; 5 import {eq} from "drizzle-orm"; 6 import { db } from "../database/drizzle/db"; 7 import { usersTable } from "../database/drizzle/schema"; 8 import bcrypt from "bcrypt"; 9 // import { users, accounts, sessions, verificationTokens } from "../database/drizzle/schema/auth"; 5 10 6 11 const authjsConfig = { 7 12 basePath: "/api/auth", 8 13 trustHost: true, 9 // TODO: Replace secret {@see https://authjs.dev/reference/core#secret} 10 secret: "MY_SECRET", 14 secret: process.env.AUTH_SECRET, 15 session: { 16 strategy: "jwt", 17 }, 11 18 providers: [ 12 // TODO: Choose and implement providers13 19 CredentialsProvider({ 14 20 name: "Credentials", 15 21 credentials: { 16 username: { label: "Username", type: "text" , placeholder: "jsmith"},22 username: { label: "Username", type: "text" }, 17 23 password: { label: "Password", type: "password" }, 18 24 }, 19 async authorize( ) {20 // Add logic here to look up the user from the credentials supplied21 const user = { id: "1", name: "J Smith", email: "jsmith@example.com" };25 async authorize(credentials) { 26 const username = typeof credentials?.username === "string" ? credentials.username : null; 27 const password = typeof credentials?.password === "string" ? credentials.password : null; 22 28 23 // Any object returned will be saved in `user` property of the JWT 24 // If you return null then an error will be displayed advising the user to check their details. 25 // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter 26 return user ?? null; 29 if(!username || !password) { return null; } 30 31 const user = await db.query.usersTable.findFirst({ 32 where: eq(usersTable.username, username), 33 }); 34 35 if(!user) { return null; } 36 37 const isPasswordValid = await bcrypt.compare(password, user.passwordHash); 38 39 if(!isPasswordValid) { return null; } 40 41 return { 42 id: String(user.id), 43 name: user.username, 44 email: user.email, 45 } 27 46 }, 28 47 }), 29 48 ], 49 50 callbacks: { 51 async jwt({ token, user }) { 52 if (user) token.sub = user.id; 53 return token; 54 }, 55 async session({ session, token }) { 56 if (session.user && token.sub) session.user.id = token.sub; 57 return session; 58 }, 59 }, 30 60 } satisfies Omit<AuthConfig, "raw">; 31 61 … … 69 99 }, 70 100 { 71 name: " my-app:authjs-middleware",101 name: "pc-forge:authjs-middleware", 72 102 immutable: false, 73 103 }, … … 83 113 }, 84 114 { 85 name: " my-app:authjs-handler",115 name: "pc-forge:authjs-handler", 86 116 path: "/api/auth/**", 87 117 method: ["GET", "POST"],
Note:
See TracChangeset
for help on using the changeset viewer.
