a4f1a55e56
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
57 lines
1.1 KiB
TypeScript
57 lines
1.1 KiB
TypeScript
import "server-only"
|
|
|
|
import { getIronSession } from "iron-session"
|
|
import { cookies } from "next/headers"
|
|
|
|
import { dt } from "@scandic-hotels/common/dt"
|
|
|
|
import { env } from "@/env/server"
|
|
|
|
async function internalGetSession() {
|
|
return await getIronSession<{
|
|
access_token: string
|
|
refresh_token: string | undefined
|
|
expires_at: string
|
|
}>(await cookies(), {
|
|
password: env.IRON_SESSION_SECRET,
|
|
cookieName: "scandic_session",
|
|
})
|
|
}
|
|
|
|
export async function getSocialSession() {
|
|
const session = await internalGetSession()
|
|
|
|
if (!session?.access_token) {
|
|
return null
|
|
}
|
|
|
|
return session
|
|
}
|
|
|
|
export async function createSocialSession({
|
|
access_token,
|
|
refresh_token,
|
|
expires_in,
|
|
}: {
|
|
access_token: string
|
|
expires_in: number
|
|
refresh_token?: string
|
|
}) {
|
|
const session = await internalGetSession()
|
|
|
|
session.access_token = access_token
|
|
session.refresh_token = refresh_token
|
|
session.expires_at = dt()
|
|
.add(expires_in * 1000)
|
|
.toISOString()
|
|
|
|
await session.save()
|
|
}
|
|
|
|
export async function destroySocialSession() {
|
|
const session = await internalGetSession()
|
|
if (!session) return
|
|
|
|
session.destroy()
|
|
}
|