forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseUser.ts
More file actions
38 lines (33 loc) · 774 Bytes
/
Copy pathuseUser.ts
File metadata and controls
38 lines (33 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"use client";
import { useEffect, useState } from "react";
export interface AuthUser {
id: string;
username: string;
email: string;
emailVerified: boolean;
srn: string | null;
prn: string;
createdAt: number;
}
/**
* The signed-in user, fetched from /api/auth/me.
* `undefined` = still loading, `null` = logged out, otherwise the user.
*/
export function useUser() {
const [user, setUser] = useState<AuthUser | null | undefined>(undefined);
useEffect(() => {
let alive = true;
fetch("/api/auth/me")
.then((r) => r.json())
.then((d) => {
if (alive) setUser(d.user ?? null);
})
.catch(() => {
if (alive) setUser(null);
});
return () => {
alive = false;
};
}, []);
return user;
}