-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathuse-user.ts
More file actions
46 lines (40 loc) · 882 Bytes
/
use-user.ts
File metadata and controls
46 lines (40 loc) · 882 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
39
40
41
42
43
44
45
46
"use client";
import useSWR from "swr";
import { normalizeWithBasePath } from "../../utils/pathUtils.js";
import type { User } from "../../types/index.js";
export function useUser() {
const { data, error, isLoading, mutate } = useSWR<User, Error, string>(
normalizeWithBasePath(
process.env.NEXT_PUBLIC_PROFILE_ROUTE || "/auth/profile"
),
(...args) =>
fetch(...args).then((res) => {
if (!res.ok) {
throw new Error("Unauthorized");
}
return res.json();
})
);
if (error) {
return {
user: null,
isLoading: false,
error,
invalidate: () => mutate()
};
}
if (data) {
return {
user: data,
isLoading: false,
error: null,
invalidate: () => mutate()
};
}
return {
user: data,
isLoading,
error,
invalidate: () => mutate()
};
}