|
1 |
| -import { createContext, useContext } from "react"; |
| 1 | +import { createContext, useContext } from 'react'; |
2 | 2 |
|
3 | 3 | const authApiUrl = import.meta.env.VITE_AUTH_API_URL;
|
4 | 4 | const AuthContext = createContext();
|
5 | 5 |
|
6 | 6 | export const AuthProvider = ({ children }) => {
|
7 |
| - const getToken = () => localStorage.getItem("token"); |
| 7 | + const getToken = () => localStorage.getItem('token'); |
8 | 8 |
|
9 | 9 | const login = async ({ email, password }) => {
|
10 | 10 | const response = await fetch(`${authApiUrl}/login`, {
|
11 |
| - method: "POST", |
| 11 | + method: 'POST', |
12 | 12 | headers: {
|
13 |
| - "Content-Type": "application/json", |
| 13 | + 'Content-Type': 'application/json', |
14 | 14 | },
|
15 | 15 | body: JSON.stringify({ email, password }),
|
16 | 16 | });
|
17 | 17 | const data = await response.json();
|
18 | 18 | if (data.token) {
|
19 |
| - localStorage.setItem("token", data.token); |
| 19 | + localStorage.setItem('token', data.token); |
20 | 20 | } else if (data.error) {
|
21 | 21 | throw new Error(data.error);
|
22 | 22 | } else {
|
23 |
| - throw new Error("Unknown error"); |
| 23 | + throw new Error('Unknown error'); |
24 | 24 | }
|
25 | 25 | };
|
26 | 26 |
|
27 | 27 | const register = async ({ email, password }) => {
|
28 | 28 | const response = await fetch(`${authApiUrl}/register`, {
|
29 |
| - method: "POST", |
| 29 | + method: 'POST', |
30 | 30 | headers: {
|
31 |
| - "Content-Type": "application/json", |
| 31 | + 'Content-Type': 'application/json', |
32 | 32 | },
|
33 | 33 | body: JSON.stringify({ email, password }),
|
34 | 34 | });
|
35 | 35 | const data = await response.json();
|
36 | 36 | if (data.token) {
|
37 |
| - localStorage.setItem("token", data.token); |
| 37 | + localStorage.setItem('token', data.token); |
38 | 38 | } else if (data.error) {
|
39 | 39 | throw new Error(data.error);
|
40 | 40 | } else {
|
41 |
| - throw new Error("Unknown error"); |
| 41 | + throw new Error('Unknown error'); |
42 | 42 | }
|
43 | 43 | };
|
44 | 44 |
|
45 | 45 | const logout = () => {
|
46 |
| - localStorage.removeItem("token"); |
| 46 | + localStorage.removeItem('token'); |
47 | 47 | };
|
48 | 48 |
|
49 | 49 | const isLoggedIn = () => {
|
|
0 commit comments