fix(SW-3549): update social session management functions for clarity and consistency * refactor(SW-3549): rename session management functions for clarity and consistency * merge Approved-by: Hrishikesh Vaipurkar
31 lines
716 B
TypeScript
31 lines
716 B
TypeScript
import "server-only"
|
|
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
|
|
import type { Session } from "next-auth"
|
|
|
|
export function isValidSession(session: Session | null): session is Session {
|
|
const sessionLogger = createLogger("session")
|
|
if (!session) {
|
|
return false
|
|
}
|
|
|
|
if (session.error) {
|
|
sessionLogger.error(`Session error: ${session.error}`)
|
|
return false
|
|
}
|
|
|
|
const token = session.token
|
|
|
|
if (token?.error) {
|
|
sessionLogger.error(`Session token error: ${token.error}`)
|
|
return false
|
|
}
|
|
if (token?.expires_at && token.expires_at < Date.now()) {
|
|
sessionLogger.debug(`Session expired: ${session.token.expires_at}`)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|