From 37f51ee88710868a77b4645294cf32862f55e7c4 Mon Sep 17 00:00:00 2001
From: l3wdfut4pwr <l3wdfut4pwr@gmail.com>
Date: Tue, 31 Mar 2026 13:52:16 +0300
Subject: add jwt

---
 src/lib/contexts/Auth.context.tsx | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

(limited to 'src/lib/contexts/Auth.context.tsx')

diff --git a/src/lib/contexts/Auth.context.tsx b/src/lib/contexts/Auth.context.tsx
index d93ca92..29e2005 100644
--- a/src/lib/contexts/Auth.context.tsx
+++ b/src/lib/contexts/Auth.context.tsx
@@ -1,5 +1,6 @@
 'use client';
-import React, { createContext, use, useState, useContext } from 'react';
+import React, { createContext, useState, useContext, useEffect } from 'react';
+
 type User = {
     id: number;
     username: string;
@@ -8,15 +9,36 @@ type User = {
 interface AuthContextType {
     user: User | null;
     setUser: (user: User | null) => void;
+    logout: () => void;
 }
 
-const AuthContext = createContext<AuthContext | null>(null);
+const AuthContext = createContext<AuthContextType | null>(null);
 
 export const AuthContextProvider = ({ children }: React.PropsWithChildren) => {
     const [user, setUser] = useState<User | null>(null);
 
+    useEffect(() => {
+        const token = localStorage.getItem('token');
+        if (!token) return;
+
+        fetch('http://localhost:8000/api/me', {
+            headers: { Authorization: `Bearer ${token}` },
+        })
+            .then((res) => {
+                if (!res.ok) throw new Error('Not authenticated');
+                return res.json();
+            })
+            .then((userData) => setUser(userData))
+            .catch(() => setUser(null));
+    }, []);
+
+    const logout = () => {
+        localStorage.removeItem('token');
+        setUser(null);
+    };
+
     return (
-        <AuthContext.Provider value={{ user, setUser }}>
+        <AuthContext.Provider value={{ user, setUser, logout }}>
             {children}
         </AuthContext.Provider>
     );
@@ -32,3 +54,4 @@ export const useAuthContext = () => {
 };
 
 export const useUser = () => useAuthContext().user;
+export const useLogout = () => useAuthContext().logout;
-- 
cgit v1.3-3-g829e

