From 1d20080db8a26e4b7dd4071daac1166142592afa Mon Sep 17 00:00:00 2001
From: l3wdfut4pwr <l3wdfut4pwr@gmail.com>
Date: Thu, 2 Apr 2026 08:36:51 +0300
Subject: minor improvements

---
 src/lib/contexts/Auth.context.tsx | 44 ++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 17 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 29e2005..fbf2e24 100644
--- a/src/lib/contexts/Auth.context.tsx
+++ b/src/lib/contexts/Auth.context.tsx
@@ -1,39 +1,49 @@
 'use client';
 import React, { createContext, useState, useContext, useEffect } from 'react';
 
-type User = {
+export type User = {
     id: number;
     username: string;
+    avatar?: string;
+    banner_file?: string;
+    premium?: boolean;
+    is_banned?: boolean;
+    is_moderator?: boolean;
 };
-
 interface AuthContextType {
     user: User | null;
     setUser: (user: User | null) => void;
     logout: () => void;
 }
-
 const AuthContext = createContext<AuthContextType | null>(null);
+const API_URL = process.env.NEXT_PUBLIC_API_URL;
 
 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 fetchUser = async () => {
+            const hasCookies = document.cookie.includes('access_token');
+            if (!hasCookies) return;
+
+            try {
+                const res = await fetch(`${API_URL}/api/me`, {
+                    credentials: 'include',
+                });
+                if (!res.ok) return;
+                const userData = await res.json();
+                setUser(userData);
+            } catch {}
+        };
+
+        fetchUser();
     }, []);
 
-    const logout = () => {
-        localStorage.removeItem('token');
+    const logout = async () => {
+        await fetch(`${API_URL}/api/auth/logout`, {
+            method: 'POST',
+            credentials: 'include',
+        });
         setUser(null);
     };
 
-- 
cgit v1.3-3-g829e

