summaryrefslogtreecommitdiff
path: root/src/lib/contexts/Auth.context.tsx
blob: ff2d369d266b04eb500883660dd3749bd53ba93f (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
'use client';

import React, { createContext, use } from 'react';

type User = {
    id: string;
    avatar?: string;
    // Чёто там ещё
};

interface AuthContext {
    user: User | null;
}

const AuthContext = createContext<AuthContext | null>(null);

export const AuthContextProvider = ({ children }: React.PropsWithChildren) => {
    // TODO: подключить бэк
    const user = null;
    return (
        <AuthContext.Provider value={{ user }}>{children}</AuthContext.Provider>
    );
};

export const useAuthContext = () => {
    const context = use(AuthContext);

    if (!context) {
        throw new Error(
            'useAuthContext must be used within AuthContextProvider',
        );
    }

    return context;
};

export const useUser = () => useAuthContext().user;