fix(SW-2210): Checking for session inside MyPagesMenu to sync session state
This commit is contained in:
committed by
Michael Zetterberg
parent
7b76e351d9
commit
d827bf04d0
@@ -1,5 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useSession } from "next-auth/react"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { MembershipLevelEnum } from "@/constants/membershipLevels"
|
||||
@@ -7,6 +8,7 @@ import { trpc } from "@/lib/trpc/client"
|
||||
|
||||
import LoginButton from "@/components/LoginButton"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { isValidClientSession } from "@/utils/clientSession"
|
||||
|
||||
import Avatar from "../Avatar"
|
||||
import MyPagesMenu, { MyPagesMenuSkeleton } from "../MyPagesMenu"
|
||||
@@ -20,6 +22,9 @@ export default function MyPagesMenuWrapper() {
|
||||
const intl = useIntl()
|
||||
const lang = useLang()
|
||||
|
||||
const { data: session } = useSession()
|
||||
const isUserLoggedIn = isValidClientSession(session)
|
||||
|
||||
const { data: user, isLoading: isLoadingUser } = trpc.user.name.useQuery()
|
||||
const { data: membership, isLoading: isLoadingMembership } =
|
||||
trpc.user.safeMembershipLevel.useQuery()
|
||||
@@ -41,7 +46,7 @@ export default function MyPagesMenuWrapper() {
|
||||
|
||||
return (
|
||||
<>
|
||||
{user ? (
|
||||
{isUserLoggedIn && user ? (
|
||||
<>
|
||||
<MyPagesMenu
|
||||
membershipLevel={membershipLevel ?? null}
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { Session } from "next-auth"
|
||||
|
||||
export function isValidClientSession(session: Session | null) {
|
||||
if (!session) {
|
||||
console.log("No session available (user not authenticated).")
|
||||
return false
|
||||
}
|
||||
if (session.error) {
|
||||
@@ -10,5 +9,14 @@ export function isValidClientSession(session: Session | null) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (session.token.error) {
|
||||
console.log(`Session token error: ${session.token.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
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import type { Session } from "next-auth"
|
||||
|
||||
export function isValidSession(session: Session | null): session is Session {
|
||||
if (!session) {
|
||||
console.log("No session available (user not authenticated).")
|
||||
return false
|
||||
}
|
||||
if (session.error) {
|
||||
|
||||
Reference in New Issue
Block a user