feat/valid-session: check valid user/session from token instead of making a slow request to api

This commit is contained in:
Linus Flood
2024-12-17 15:15:55 +01:00
parent 431ab477eb
commit 711bf4b2d3
5 changed files with 35 additions and 15 deletions

18
utils/session.ts Normal file
View File

@@ -0,0 +1,18 @@
import type { Session } from "next-auth"
export function isValidSession(session: Session | null) {
if (!session) {
console.log("No session available (user not authenticated).")
return false
}
if (session.error) {
console.log(`Session error: ${session.error}`)
return false
}
if (session.token.expires_at && session.token?.expires_at < Date.now()) {
console.log(`Session expired: ${session.token?.expires_at}`)
return false
}
return true
}