File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
import React, {createContext, useState} from 'react';
import {Buffer} from 'buffer';
import {AsyncStorage} from '@react-native-async-storage/async-storage';
import {url} from '../url';
export const AuthContext = createContext();
export default function AuthProvider({children}) {
const [userToken, setUserToken] = useState(null);
const [userInfo, setUserInfo] = useState(null);
const parseJWT = token => {
return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
};
const onClickLogin = (userID, userPassword) => {
fetch(`${url}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({
id: userID,
password: userPassword,
}),
})
.then(response => response.json())
.then(data => {
const {result, token} = data;
if (result === 'success') {
setUserToken(token);
let userInfo = parseJWT(token);
setUserInfo(userInfo);
console.log('login 성공');
console.log('userInfo', userInfo);
} else {
alert('로그인 정보를 다시 한 번 확인해주세요.');
}
})
.catch(err => {
console.error(err);
});
};
const logout = () => {
console.log('logout');
};
const value = React.useMemo(() => ({
onClickLogin,
setUserInfo,
userToken,
userInfo,
}));
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}