From e0c89862513579ed8fbbaa57569cd8a52bea084e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 12 Mar 2024 10:21:52 +0100 Subject: [PATCH] feat: log error no user --- webapp/src/providers/Auth.tsx | 121 +++++++++++++++++----------------- 1 file changed, 61 insertions(+), 60 deletions(-) diff --git a/webapp/src/providers/Auth.tsx b/webapp/src/providers/Auth.tsx index b7eb4113..c9b25c86 100644 --- a/webapp/src/providers/Auth.tsx +++ b/webapp/src/providers/Auth.tsx @@ -5,83 +5,84 @@ import React, { useState, createContext, useContext, useEffect } from "react"; import { UserIncluded } from "~/server/api/routers/user"; export interface BeforeInstallPromptEvent extends Event { - readonly platforms: Array; - readonly userChoice: Promise<{ - outcome: "accepted" | "dismissed"; - platform: string; - }>; - prompt(): Promise; + readonly platforms: Array; + readonly userChoice: Promise<{ + outcome: "accepted" | "dismissed"; + platform: string; + }>; + prompt(): Promise; } type AuthContext = { - user: UserIncluded | null; - setUser: (user: UserIncluded | null) => void; - isOtpGenerated: boolean; - setIsOtpGenerated: (isOtpGenerated: boolean) => void; - showing: boolean; - setShowing: (showing: boolean) => void; - deferredEvent: BeforeInstallPromptEvent | null; - setDeferredEvent: (event: BeforeInstallPromptEvent | null) => void; - refetchUser: () => void; + user: UserIncluded | null; + setUser: (user: UserIncluded | null) => void; + isOtpGenerated: boolean; + setIsOtpGenerated: (isOtpGenerated: boolean) => void; + showing: boolean; + setShowing: (showing: boolean) => void; + deferredEvent: BeforeInstallPromptEvent | null; + setDeferredEvent: (event: BeforeInstallPromptEvent | null) => void; + refetchUser: () => void; }; const Context = createContext({} as AuthContext); export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ - children, + children, }) => { - const [user, setUser] = useState(null); + const [user, setUser] = useState(null); - const [isOtpGenerated, setIsOtpGenerated] = useState(false); + const [isOtpGenerated, setIsOtpGenerated] = useState(false); - const [showing, setShowing] = useState(false); - const [deferredEvent, setDeferredEvent] = - useState(null); + const [showing, setShowing] = useState(false); + const [deferredEvent, setDeferredEvent] = + useState(null); - const router = useRouter(); + const router = useRouter(); - const fetchMe = async () => { - const jwtToken = getCookie(process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"); - if (!jwtToken) return; + const fetchMe = async () => { + const jwtToken = getCookie(process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"); + if (!jwtToken) return; - const decoded = jwtDecode(jwtToken); - const collection = (decoded as any)["collection"] as string; - const result = await fetch(`/api/${collection}/me`, { - headers: { - Authorization: `Bearer ${jwtToken}`, - }, - }).then((req) => req.json()); + const decoded = jwtDecode(jwtToken); + const collection = (decoded as any)["collection"] as string; + const result = await fetch(`/api/${collection}/me`, { + headers: { + Authorization: `Bearer ${jwtToken}`, + }, + }).then((req) => req.json()); - if (result && result.user !== null) { - setUser(result.user); - } else { - setUser(null); - deleteCookie(process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"); - router.push("/"); - } - }; + if (result && result.user !== null) { + setUser(result.user); + } else { + console.error('NO USER : ', result) + setUser(null); + deleteCookie(process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"); + router.push("/"); + } + }; - useEffect(() => { - fetchMe(); - }, []); + useEffect(() => { + fetchMe(); + }, []); - return ( - - {children} - - ); + return ( + + {children} + + ); }; type UseAuth = () => AuthContext;