Merged in feat/SW-3549-handle-unlinked-account (pull request #3019)

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
This commit is contained in:
Joakim Jäderberg
2025-10-28 09:51:30 +00:00
parent 4a6c64f921
commit a4f1a55e56
15 changed files with 105 additions and 90 deletions

View File

@@ -3,7 +3,7 @@ import { type NextRequest, NextResponse } from "next/server"
import { getPublicURL } from "@/server/utils"
import { signOut } from "@/auth"
import { destroySession } from "@/auth/scandic/session"
import { destroySocialSession } from "@/auth/scandic/session"
import type { Lang } from "@scandic-hotels/common/constants/language"
@@ -17,7 +17,7 @@ export async function GET(
await signOut({ redirectTo, redirect: false })
// Delete scandic session once user logouts from sas
await destroySession()
await destroySocialSession()
return NextResponse.redirect(redirectTo)
}

View File

@@ -6,7 +6,7 @@ import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { env } from "@/env/server"
import { getToken } from "@/auth/scandic/getToken"
import { createSession } from "@/auth/scandic/session"
import { createSocialSession } from "@/auth/scandic/session"
const logger = createLogger("curity-callback")
export async function GET(req: NextRequest) {
@@ -37,7 +37,7 @@ export async function GET(req: NextRequest) {
code,
})
await createSession({
await createSocialSession({
access_token: tokenResponse.access_token,
refresh_token: tokenResponse.refresh_token,
expires_in: tokenResponse.expires_in,

View File

@@ -2,10 +2,10 @@ import { type NextRequest } from "next/server"
import { noContent } from "@/server/errors/next"
import { destroySession } from "@/auth/scandic/session"
import { destroySocialSession } from "@/auth/scandic/session"
export async function GET(_req: NextRequest) {
await destroySession()
await destroySocialSession()
// TODO: Should we call Scandic's logout endpoint?
return noContent()

View File

@@ -14,14 +14,14 @@ import {
import { config } from "@/auth/scandic/config"
import { endpoints } from "@/auth/scandic/endpoints"
import {
createSession,
destroySession,
getSession,
createSocialSession,
destroySocialSession,
getSocialSession,
} from "@/auth/scandic/session"
const logger = createLogger("scandic/refresh")
export async function POST(_req: NextRequest) {
const session = await getSession()
const session = await getSocialSession()
if (!session) {
return badRequest("No session found")
}
@@ -39,7 +39,7 @@ export async function POST(_req: NextRequest) {
if (isResponseError(error)) {
if (error.status === 400 && error.cause === "invalid_grant") {
await destroySession()
await destroySocialSession()
return badRequest("invalid_grant")
}
@@ -55,7 +55,7 @@ export async function POST(_req: NextRequest) {
got_new_access_token: newTokens.access_token !== session.access_token,
})
await createSession({
await createSocialSession({
access_token: newTokens.access_token,
refresh_token: newTokens.refresh_token ?? session.refresh_token,
expires_in: newTokens.expires_in,

View File

@@ -4,7 +4,7 @@ import { z } from "zod"
import { dt } from "@scandic-hotels/common/dt"
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { getSession } from "@/auth/scandic/session"
import { getSocialSession } from "@/auth/scandic/session"
const logger = createLogger("scandic/session")
@@ -29,8 +29,8 @@ export type SocialSessionResponse = z.infer<typeof socialSessionResponseSchema>
export async function GET(): Promise<NextResponse<SocialSessionResponse>> {
try {
const session = await getSession()
if (!session || !session.access_token) {
const session = await getSocialSession()
if (!session) {
return createResponse({ status: "no_session", user: null })
}

View File

@@ -7,8 +7,8 @@ import { dt } from "@scandic-hotels/common/dt"
import { env } from "@/env/server"
export async function getSession() {
return getIronSession<{
async function internalGetSession() {
return await getIronSession<{
access_token: string
refresh_token: string | undefined
expires_at: string
@@ -18,7 +18,17 @@ export async function getSession() {
})
}
export async function createSession({
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,
@@ -27,7 +37,7 @@ export async function createSession({
expires_in: number
refresh_token?: string
}) {
const session = await getSession()
const session = await internalGetSession()
session.access_token = access_token
session.refresh_token = refresh_token
@@ -38,8 +48,8 @@ export async function createSession({
await session.save()
}
export async function destroySession() {
const session = await getSession()
export async function destroySocialSession() {
const session = await internalGetSession()
if (!session) return
session.destroy()

View File

@@ -10,7 +10,7 @@ import {
} from "@scandic-hotels/trpc/serverClient"
import { auth } from "@/auth"
import { getSession } from "@/auth/scandic/session"
import { getSocialSession } from "@/auth/scandic/session"
import type { Lang } from "@scandic-hotels/common/constants/language"
@@ -29,7 +29,7 @@ export async function createAppContext() {
return session
},
getScandicUserToken: async () => {
const session = await getSession()
const session = await getSocialSession()
return session?.access_token ?? null
},
getUserPointsBalance: async () => {
@@ -46,17 +46,17 @@ export async function createAppContext() {
return euroBonusProfile.points.total
},
getScandicUser: async () => {
const session = await getSession()
const session = await getSocialSession()
if (!session) return null
// The getSession will either return empty object or session object, hence we need to validate if the object is empty or not
if (!session?.access_token) return null
return await getVerifiedUser({
const user = await getVerifiedUser({
token: {
expires_at: dt(session.expires_at).unix() * 1000,
access_token: session.access_token,
},
})
return user ?? null
},
})

View File

@@ -73,12 +73,14 @@ export async function createAppContext() {
const session = await getUserSession()
if (!session) return null
return await getVerifiedUser({
const user = await getVerifiedUser({
token: {
expires_at: session.token.expires_at ?? 0,
access_token: session.token.access_token,
},
})
return user ?? null
},
})