-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseAuth.ts
More file actions
47 lines (40 loc) · 1.21 KB
/
useAuth.ts
File metadata and controls
47 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use client';
import { useState, useEffect } from 'react';
import { useWalletConnector } from './useWalletConnector';
interface AuthState {
isAuthenticated: boolean;
isLoading: boolean;
userAddress: string | null;
}
export function useAuth() {
const { connectWallet } = useWalletConnector();
const [authState, setAuthState] = useState<AuthState>({
isAuthenticated: false,
isLoading: true,
userAddress: null,
});
useEffect(() => {
// Check for existing session/token on mount
const checkAuth = async () => {
try {
// In a real Web3 app, we'd check if the wallet is still connected
// and if a valid session token exists in cookies
const hasToken = document.cookie.includes('auth-token=');
// Mocking check - in production, validate JWT or wallet state here
setAuthState({
isAuthenticated: hasToken,
isLoading: false,
userAddress: hasToken ? '0x...' : null, // Get from wallet provider
});
} catch (error) {
setAuthState({
isAuthenticated: false,
isLoading: false,
userAddress: null,
});
}
};
checkAuth();
}, []);
return authState;
}