Skip to content

Commit

Permalink
feat: log error no user
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Mar 12, 2024
1 parent 8c6289b commit e0c8986
Showing 1 changed file with 61 additions and 60 deletions.
121 changes: 61 additions & 60 deletions webapp/src/providers/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
readonly userChoice: Promise<{
outcome: "accepted" | "dismissed";
platform: string;
}>;
prompt(): Promise<void>;
readonly platforms: Array<string>;
readonly userChoice: Promise<{
outcome: "accepted" | "dismissed";
platform: string;
}>;
prompt(): Promise<void>;
}

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<UserIncluded | null>(null);
const [user, setUser] = useState<UserIncluded | null>(null);

const [isOtpGenerated, setIsOtpGenerated] = useState<boolean>(false);
const [isOtpGenerated, setIsOtpGenerated] = useState<boolean>(false);

const [showing, setShowing] = useState(false);
const [deferredEvent, setDeferredEvent] =
useState<BeforeInstallPromptEvent | null>(null);
const [showing, setShowing] = useState(false);
const [deferredEvent, setDeferredEvent] =
useState<BeforeInstallPromptEvent | null>(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 (
<Context.Provider
value={{
user,
setUser,
isOtpGenerated,
setIsOtpGenerated,
showing,
setShowing,
deferredEvent,
setDeferredEvent,
refetchUser: fetchMe,
}}
>
{children}
</Context.Provider>
);
return (
<Context.Provider
value={{
user,
setUser,
isOtpGenerated,
setIsOtpGenerated,
showing,
setShowing,
deferredEvent,
setDeferredEvent,
refetchUser: fetchMe,
}}
>
{children}
</Context.Provider>
);
};

type UseAuth = () => AuthContext;
Expand Down

0 comments on commit e0c8986

Please sign in to comment.