Skip to content

Commit

Permalink
add token to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
lukbar48 committed Oct 14, 2022
1 parent 9c5a5cf commit 358282b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,28 @@ export const AuthProvider = ({ children }: { children: ReactNode }): any => {
useEffect(() => {
const localStorageUser = localStorage.getItem('user');
if (!localStorageUser) return;
console.log(JSON.parse(localStorageUser));
axios.defaults.headers.common['Authorization'] = `Bearer ${JSON.parse(localStorageUser).token}`;
setUser(JSON.parse(localStorageUser));
}, []);

const register = async ({ email, password }: { email: string; password: string }) => {
console.log(email, password);
setIsLoading(true);
try {
const response = await axios.post('http://localhost:4000/api/user/register', { email, password });
setIsLoading(false);

return response.data;
} catch (error) {
console.log(error);
setErrMsg(error + 'Invalid email or password.');
} finally {
setIsLoading(false);
}
};

const logIn = async ({ email, password }: { email: string; password: string }) => {
try {
const response = await axios.post('http://localhost:4000/api/user/login', { email, password });
console.log('axi', response.data);
localStorage.setItem('user', JSON.stringify(response.data));
setUser(response.data);
} catch (error) {
Expand Down

0 comments on commit 358282b

Please sign in to comment.