Skip to content

Commit b981317

Browse files
committed
fix secret key lazy loading
1 parent 69e27ae commit b981317

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/lib/auth/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ export const config = {
1111
secure: process.env.NODE_ENV === 'production',
1212
};
1313

14-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
15-
export const secretKey = createSecretKey(process.env.JWT_SECRET!, 'utf-8');
14+
function getSecretKey() {
15+
const jwtSecret = process.env.JWT_SECRET;
16+
if (!jwtSecret) {
17+
throw new Error('JWT_SECRET environment variable is required');
18+
}
19+
return createSecretKey(jwtSecret, 'utf-8');
20+
}
1621

1722
export interface SessionPayload {
1823
userId: string;
@@ -25,7 +30,7 @@ export async function createSession(payload: SessionPayload) {
2530
.setIssuedAt()
2631
.setIssuer(config.issuer)
2732
.setExpirationTime(Math.floor(Date.now() / 1000) + config.expiration)
28-
.sign(secretKey);
33+
.sign(getSecretKey());
2934

3035
const cookieStore = await cookies();
3136
cookieStore.set(config.cookieName, session, {
@@ -48,7 +53,7 @@ export async function verifySession(): Promise<SessionPayload | null> {
4853
}
4954

5055
try {
51-
const { payload } = await jwtVerify(session, secretKey, {
56+
const { payload } = await jwtVerify(session, getSecretKey(), {
5257
issuer: config.issuer,
5358
});
5459

0 commit comments

Comments
 (0)