summaryrefslogtreecommitdiff
path: root/src/lib/api/RefreshUser.ts
blob: d93d310e3841f1d440441cade4e1291f5d518cd5 (plain)
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
const API_URL = process.env.NEXT_PUBLIC_API_URL;

export type RefreshUserResponse = {
    authenticated: boolean;
    user: any | null;
};

export const refreshUser = async (): Promise<RefreshUserResponse> => {
    try {
        const res = await fetch(`${API_URL}/api/me`, {
            method: 'GET',
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json',
            },
        });

        const data = await res.json().catch(() => null);

        if (!res.ok || !data) {
            return {
                authenticated: false,
                user: null,
            };
        }

        return {
            authenticated: data.authenticated,
            user: data.authenticated ? data.user : null,
        };
    } catch (err) {
        return {
            authenticated: false,
            user: null,
        };
    }
};