Merged in fix/SW-3578-user-is-forced-to-login- (pull request #3044)
fix(SW-3578): Fixed session issue when sas session expires * fix(SW-3578): Fixed session issue when sas session expires * base socialLogin auto-features on validSession and being linked * remove unused object * remove 'import server-only' for isValidSession() since it's only using passed data * remove isValidClientSession() Approved-by: Joakim Jäderberg Approved-by: Anton Gunnarsson
This commit is contained in:
committed by
Joakim Jäderberg
parent
15a2da333d
commit
1c7f72e95d
@@ -3,15 +3,11 @@
|
||||
import { useSession } from "next-auth/react"
|
||||
|
||||
import { BookingFlowContextProvider } from "@scandic-hotels/booking-flow/BookingFlowContextProvider"
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
import { trpc } from "@scandic-hotels/trpc/client"
|
||||
import { isValidSession } from "@scandic-hotels/trpc/utils/session"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
import type { ComponentProps, ReactNode } from "react"
|
||||
|
||||
const logger = createLogger("BookingFlowProviders")
|
||||
|
||||
export function BookingFlowProviders({ children }: { children: ReactNode }) {
|
||||
const user = useBookingFlowUser()
|
||||
const isLinkedUser =
|
||||
@@ -36,7 +32,7 @@ type BookingFlowUser = BookingFlowContextData["user"]
|
||||
|
||||
function useBookingFlowUser(): BookingFlowUser {
|
||||
const { data: session } = useSession()
|
||||
const hasValidSession = isValidClientSession(session)
|
||||
const hasValidSession = isValidSession(session)
|
||||
|
||||
const {
|
||||
data: euroBonusProfile,
|
||||
@@ -66,25 +62,3 @@ function useBookingFlowUser(): BookingFlowUser {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function isValidClientSession(session: Session | null) {
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
if (session.error) {
|
||||
logger.error(`Session error: ${session.error}`)
|
||||
return false
|
||||
}
|
||||
|
||||
if (session.token.error) {
|
||||
logger.error(`Session token error: ${session.token.error}`)
|
||||
return false
|
||||
}
|
||||
const expiresAt = dt(session.token.expires_at)
|
||||
if (session.token.expires_at && expiresAt.isBefore(dt())) {
|
||||
logger.warn(`Session expired: ${expiresAt.toISOString()}`)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useEffect } from "react"
|
||||
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
import { isValidSession } from "@scandic-hotels/trpc/utils/session"
|
||||
|
||||
import type { User } from "next-auth"
|
||||
|
||||
@@ -26,10 +27,12 @@ export function useSocialSession() {
|
||||
|
||||
function useSocialSessionQuery() {
|
||||
const { data: session } = useSession()
|
||||
const enabled = isValidSession(session) && isUserLinked(session?.user)
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["socialSession"],
|
||||
queryFn: getSocialSession,
|
||||
enabled: !!session,
|
||||
enabled: enabled,
|
||||
refetchInterval: getTime(1, "m"),
|
||||
})
|
||||
}
|
||||
@@ -38,7 +41,8 @@ function useAutoLogin() {
|
||||
const { data: session } = useSession()
|
||||
const { isSuccess, data: socialSession } = useSocialSessionQuery()
|
||||
|
||||
const isLinked = isLinkedUser(session?.user) ? session.user.isLinked : false
|
||||
const isLinked = isUserLinked(session?.user)
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLinked) {
|
||||
autoLoginLogger.info("User is not linked")
|
||||
@@ -174,11 +178,10 @@ function getTime(value: number, unit: "m" | "s") {
|
||||
}
|
||||
}
|
||||
|
||||
function isLinkedUser(
|
||||
user: User | undefined
|
||||
): user is User & { isLinked: boolean } {
|
||||
function isUserLinked(user: User | undefined): boolean {
|
||||
if (user && "isLinked" in user) {
|
||||
return true
|
||||
return !!user.isLinked
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user