auth_context.js 3.23 KB
Newer Older
한규민's avatar
한규민 committed
1
import { createContext,  useCallback, useContext, useEffect, useState } from "react";
한규민's avatar
push    
한규민 committed
2
3
4
5
6
7
import authApi from "../apis/auth.api";
import catchErrors from "../utils/catchErrors";

const AuthContext = createContext({
    error: "",
    loading: false,
한규민's avatar
한규민 committed
8
    user: {id:0, role:"user"},
한규민's avatar
push    
한규민 committed
9
10
11
    setUser: () => { },
    login: () => Promise.resolve(false),
    logout: () => { },
한규민's avatar
한규민 committed
12
    guestLogin: () => Promise.resolve(false),
한규민's avatar
push    
한규민 committed
13
14
15
16
17
18
    catchErrorAuth: (error, displayError) => { },
});

const AuthProvider = ({ children }) => {
    const [error, setError] = useState("");
    const [loading, setLoading] = useState(false);
한규민's avatar
한규민 committed
19
20
21
22
23
24
25
26
27
28
29
    const [user, setUser] = useState({id:0, role:"user"});

    const getUser = async() => {
        const {id, role} = await authApi.getUser();
        const user = {"id" : id, "role" : role};
        setUser(user);
    };

    useEffect(() => {
        getUser();
    },[]);
한규민's avatar
push    
한규민 committed
30

한규민's avatar
한규민 committed
31
    const login = useCallback(async (id, password) => {
한규민's avatar
push    
한규민 committed
32
33
34
        try {
            setError("");
            setLoading(true);
한규민's avatar
한규민 committed
35
            const user = await authApi.login(id, password);
한규민's avatar
push    
한규민 committed
36
            setUser(user);
한규민's avatar
한규민 committed
37
            return true
한규민's avatar
push    
한규민 committed
38
39
40
41
42
43
44
45
46
47
48
49
        } catch (error) {
            catchErrors(error, setError);
            return false;
        } finally {
            setLoading(false);
        }
    }, []);

    const logout = useCallback(async () => {
        try {
            setError("");
            setLoading(true);
한규민's avatar
한규민 committed
50
51
52
            const user = await authApi.logout();
            setUser(user);
            alert("로그아웃되었습니다.");
한규민's avatar
push    
한규민 committed
53
54
55
56
57
58
59
        } catch (error) {
            catchErrors(error, setError);
        } finally {
            setLoading(false);
        }
    }, []);

한규민's avatar
한규민 committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    const guestLogin = useCallback(async (guest) => {
        try {
            setError("");
            setLoading(true);
            const user = await authApi.guestLogin(guest);
            setUser(user);
            return true
        } catch (error) {
            catchErrors(error, setError);
        } finally {
            setLoading(false);
        }
    }, []);

한규민's avatar
push    
한규민 committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    const catchErrorAuth = useCallback(async (error, displayError) => {
        let errorMsg;
        if (error.response) {
            if (typeof error.response.data === "string") {
                errorMsg = error.response.data;
                console.log("Error response:", errorMsg);
            } else {
                const { data } = error.response;
                if (data.redirectUrl) {
                    errorMsg = data.message;
                    console.log("Error response with redirected message:", errorMsg);
                    console.log("redirect url", data.redirectUrl);
                    return await logout();
                }
            }
        } else if (error.request) {
            errorMsg = error.request;
            console.log("Error request:", errorMsg);
        } else {
            errorMsg = error.message;
            console.log("Error message:", errorMsg);
        }

        displayError(errorMsg);
    }, []);

    return (
        <AuthContext.Provider
한규민's avatar
한규민 committed
102
            value={{ error, loading, user, setUser, login, logout, guestLogin, catchErrorAuth }}
한규민's avatar
push    
한규민 committed
103
104
105
106
107
108
109
110
111
        >
            {children}
        </AuthContext.Provider>
    );
};

const useAuth = () => useContext(AuthContext);

export { AuthProvider, useAuth };