Skip to content

Commit

Permalink
refactor: session max age
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Apr 22, 2024
1 parent b7c56b2 commit af03d53
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/app/src/constants/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const JWT_MAX_AGE = 90 * 24 * 60 * 60 // 90 days
export const SESSION_MAX_AGE = 360 * 24 * 60 * 60 // 360 days

export const authRoutes = {
signIn: ["/sign-in", "/login", "/signin"],
Expand Down
10 changes: 4 additions & 6 deletions apps/app/src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { z } from "zod"

import { sendVerificationEmail } from "@/api/me/email/mutations"
import { otpWindow } from "@/constants"
import { authRoutes, JWT_MAX_AGE } from "@/constants/auth"
import { authRoutes, SESSION_MAX_AGE } from "@/constants/auth"
import { env } from "@/lib/env"
import { i18n } from "@/lib/i18n-config"
import { ITrpcContext } from "@/types"
Expand Down Expand Up @@ -102,7 +102,7 @@ export const providers: Provider[] = [
try {
const ua = req.headers?.["user-agent"] ?? ""
const ip = requestIp.getClientIp(req) ?? ""
const expires = new Date(Date.now() + JWT_MAX_AGE * 1000)
const expires = new Date(Date.now() + SESSION_MAX_AGE * 1000)
const body: z.infer<ReturnType<typeof sessionsSchema>> = {
id: uuid,
userId: user.id,
Expand All @@ -113,7 +113,7 @@ export const providers: Provider[] = [
lastUsedAt: new Date(),
createdAt: new Date(),
}
await redis.setex(`session:${user.id}:${uuid}`, JWT_MAX_AGE, JSON.stringify(body))
await redis.setex(`session:${user.id}:${uuid}`, SESSION_MAX_AGE, JSON.stringify(body))
} catch (error) {
logger.error("Error creating session", error)
}
Expand Down Expand Up @@ -298,16 +298,14 @@ export const nextAuthOptions: NextAuthOptions = {
return true
},
},
jwt: {
maxAge: JWT_MAX_AGE,
},
pages: {
signIn: authRoutes.signIn[0],
newUser: authRoutes.signUp[0],
error: authRoutes.signIn[0],
},
session: {
strategy: "jwt", //? Strategy database could not work with credentials provider for security reasons
maxAge: SESSION_MAX_AGE,
},
logger: {
error(code, metadata) {
Expand Down

0 comments on commit af03d53

Please sign in to comment.