Spaces:
Sleeping
Sleeping
Download src/contexts/AuthContext.jsx from Aya07/emotion-ui: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/spaces/Aya07/emotion-ui/resolve/main/src/contexts/AuthContext.jsx
- Command line
-
hf download hf://spaces/Aya07/emotion-ui/src/contexts/AuthContext.jsx
-
curl -L -o AuthContext.jsx https://huggingface.co/spaces/Aya07/emotion-ui/resolve/main/src/contexts/AuthContext.jsx
3.29 kB
| // src/contexts/AuthContext.jsx | |
| import React, { createContext, useState, useContext, useEffect } from 'react'; | |
| const AuthContext = createContext(); | |
| export const useAuth = () => { | |
| const context = useContext(AuthContext); | |
| if (!context) { | |
| throw new Error('useAuth must be used within AuthProvider'); | |
| } | |
| return context; | |
| }; | |
| export const AuthProvider = ({ children }) => { | |
| const [user, setUser] = useState(null); | |
| const [loading, setLoading] = useState(true); | |
| const [error, setError] = useState(null); | |
| // Vérifier si l'utilisateur est déjà connecté | |
| useEffect(() => { | |
| const checkAuth = () => { | |
| const storedUser = localStorage.getItem('user'); | |
| const token = localStorage.getItem('token'); | |
| if (storedUser && token) { | |
| setUser(JSON.parse(storedUser)); | |
| } | |
| setLoading(false); | |
| }; | |
| checkAuth(); | |
| }, []); | |
| // Inscription | |
| const signup = async (email, password, name) => { | |
| setError(null); | |
| try { | |
| // Récupérer les utilisateurs existants | |
| const users = JSON.parse(localStorage.getItem('users') || '[]'); | |
| // Vérifier si l'email existe déjà | |
| if (users.find(u => u.email === email)) { | |
| throw new Error('Email already exists'); | |
| } | |
| // Créer le nouvel utilisateur | |
| const newUser = { | |
| id: Date.now().toString(), | |
| email, | |
| name, | |
| password: btoa(password), // Encodage simple (à remplacer par hash en production) | |
| createdAt: new Date().toISOString() | |
| }; | |
| users.push(newUser); | |
| localStorage.setItem('users', JSON.stringify(users)); | |
| // Connecter automatiquement | |
| const token = btoa(`${email}:${Date.now()}`); | |
| const userWithoutPassword = { id: newUser.id, email: newUser.email, name: newUser.name }; | |
| localStorage.setItem('token', token); | |
| localStorage.setItem('user', JSON.stringify(userWithoutPassword)); | |
| setUser(userWithoutPassword); | |
| return { success: true }; | |
| } catch (err) { | |
| setError(err.message); | |
| return { success: false, error: err.message }; | |
| } | |
| }; | |
| // Connexion | |
| const login = async (email, password) => { | |
| setError(null); | |
| try { | |
| const users = JSON.parse(localStorage.getItem('users') || '[]'); | |
| const user = users.find(u => u.email === email && u.password === btoa(password)); | |
| if (!user) { | |
| throw new Error('Invalid email or password'); | |
| } | |
| const token = btoa(`${email}:${Date.now()}`); | |
| const userWithoutPassword = { id: user.id, email: user.email, name: user.name }; | |
| localStorage.setItem('token', token); | |
| localStorage.setItem('user', JSON.stringify(userWithoutPassword)); | |
| setUser(userWithoutPassword); | |
| return { success: true }; | |
| } catch (err) { | |
| setError(err.message); | |
| return { success: false, error: err.message }; | |
| } | |
| }; | |
| // Déconnexion | |
| const logout = () => { | |
| localStorage.removeItem('token'); | |
| localStorage.removeItem('user'); | |
| setUser(null); | |
| }; | |
| const value = { | |
| user, | |
| loading, | |
| error, | |
| login, | |
| signup, | |
| logout, | |
| isAuthenticated: !!user | |
| }; | |
| return ( | |
| <AuthContext.Provider value={value}> | |
| {children} | |
| </AuthContext.Provider> | |
| ); | |
| }; |