-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug: need to fix react cache and remove auth package from the packages
- Loading branch information
1 parent
17d1535
commit 122588c
Showing
7 changed files
with
598 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { PrismaAdapter } from "@lucia-auth/adapter-prisma"; | ||
import prisma from "@zephyr/db/prisma"; | ||
import { Lucia, type Session, type User } from "lucia"; | ||
import { cookies } from "next/headers"; | ||
import { cache } from "react"; | ||
|
||
const adapter = new PrismaAdapter(prisma.session, prisma.user); | ||
|
||
export const lucia = new Lucia(adapter, { | ||
sessionCookie: { | ||
expires: false, | ||
attributes: { | ||
secure: process.env.NODE_ENV === "production" | ||
} | ||
}, | ||
getUserAttributes(databaseUserAttributes) { | ||
return { | ||
id: databaseUserAttributes.id, | ||
username: databaseUserAttributes.username, | ||
displayName: databaseUserAttributes.displayName, | ||
avatarUrl: databaseUserAttributes.avatarUrl, | ||
googleId: databaseUserAttributes.googleId | ||
}; | ||
} | ||
}); | ||
|
||
declare module "lucia" { | ||
interface Register { | ||
Lucia: typeof lucia; | ||
DatabaseUserAttributes: DatabaseUserAttributes; | ||
} | ||
} | ||
|
||
interface DatabaseUserAttributes { | ||
id: string; | ||
username: string; | ||
displayName: string; | ||
avatarUrl: string | null; | ||
googleId: string | null; | ||
} | ||
|
||
export const validateRequest = cache( | ||
async (): Promise< | ||
{ user: User; session: Session } | { user: null; session: null } | ||
> => { | ||
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null; | ||
|
||
if (!sessionId) { | ||
return { | ||
user: null, | ||
session: null | ||
}; | ||
} | ||
|
||
const result = await lucia.validateSession(sessionId); | ||
|
||
try { | ||
// biome-ignore lint/complexity/useOptionalChain: <explanation> | ||
if (result.session && result.session.fresh) { | ||
const sessionCookie = lucia.createSessionCookie(result.session.id); | ||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes | ||
); | ||
} | ||
if (!result.session) { | ||
const sessionCookie = lucia.createBlankSessionCookie(); | ||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes | ||
); | ||
} | ||
} catch {} | ||
|
||
return result; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "@zephyr/auth", | ||
"version": "0.0.1", | ||
"license": "AGPL-3.0", | ||
"private": true, | ||
"dependencies": { | ||
"@lucia-auth/adapter-prisma": "^4.0.1", | ||
"@zephyr/db": "workspace:*", | ||
"i": "^0.3.7", | ||
"lucia": "^3.2.0", | ||
"next": "15.0.0-rc.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.7.4", | ||
"@types/react": "^18.3.11", | ||
"@zephyr/config": "workspace:*", | ||
"react": "19.0.0-rc-459fd418-20241001", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "@zephyr/config/base.tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"noEmit": false, | ||
"outDir": "dist", | ||
"baseUrl": "." | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.