The FutureSea frontend implements a comprehensive authentication system using Solana wallet connection and JWT tokens. The system provides automatic sign up/sign in functionality with protected routes.
- 🔐 Wallet-based Authentication: Connect Solana wallet for automatic sign up/sign in
- 🛡️ Protected Routes: Profile page and other sensitive areas require authentication
- 🔄 Auto-refresh: JWT tokens are automatically refreshed
- 💾 Persistent Sessions: Authentication state persists across browser sessions
- 🎨 User Interface: Loading states, error handling, and user feedback
The main authentication context that manages:
- User state and authentication status
- JWT token management
- Wallet connection handling
- Local storage persistence
Usage:
import { useAuth } from '@/contexts/AuthContext';
const { user, isAuthenticated, login, logout } = useAuth();A wrapper component that protects routes requiring authentication:
- Redirects unauthenticated users to home page
- Shows loading states during authentication checks
- Provides wallet connection prompts
Usage:
import ProtectedRoute from '@/components/auth/ProtectedRoute';
<ProtectedRoute>
<YourProtectedComponent />
</ProtectedRoute>A reusable loading component for authentication states:
- Customizable sizes and text
- Consistent styling across the app
A component that displays current authentication status:
- Shows user avatar and username when authenticated
- Displays wallet connection button when not authenticated
- Handles loading states
- User connects wallet → AuthContext detects connection
- Auto-authentication → Backend API call to
/auth/connect-wallet - Token storage → JWT token stored in localStorage
- State update → User data and authentication status updated
- Route protection → Protected routes become accessible
- Requires authentication
- Shows user information and statistics
- Provides disconnect functionality
// src/app/profile/page.tsx
import ProtectedRoute from '@/components/auth/ProtectedRoute';
const ProfilePage = () => {
return (
<ProtectedRoute>
<Layout>
{/* Profile content */}
</Layout>
</ProtectedRoute>
);
};Create a .env.local file in the frontend root:
# API Configuration
NEXT_PUBLIC_API_URL=http://localhost:3001/api
# App Configuration
NEXT_PUBLIC_APP_NAME=FutureSea
NEXT_PUBLIC_APP_DESCRIPTION=Casino web3 game on solana
# Solana Configuration
NEXT_PUBLIC_SOLANA_NETWORK=devnet
NEXT_PUBLIC_SOLANA_RPC_URL=https://api.devnet.solana.comThe frontend communicates with the backend API for authentication:
POST /api/auth/connect-wallet
{
"walletAddress": "user_wallet_address"
}{
"token": "jwt_token",
"isNewUser": true/false,
"user": {
"id": "user_id",
"walletAddress": "wallet_address",
"username": "auto_generated_username",
"isVerified": true,
"balance": 0,
"totalBets": 0,
"totalWins": 0,
"totalLosses": 0,
"totalWagered": 0,
"totalWon": 0,
"winRate": "0.00",
"profitLoss": 0
}
}- Loading: Shows spinner while checking authentication
- Not Authenticated: Shows wallet connection button
- Authenticated: Shows user avatar and username
- Connecting: Shows "Connecting..." message
The navbar now shows:
- User avatar with username when authenticated
- Wallet connection button when not authenticated
- Loading indicator during authentication checks
- JWT Tokens: Secure session management
- Token Refresh: Automatic token renewal
- Local Storage: Persistent authentication state
- Route Protection: Unauthorized access prevention
- Error Handling: Graceful error management
The system handles various error scenarios:
- Network failures
- Invalid tokens
- Wallet connection issues
- API errors
- Wrap your component with
ProtectedRoute:
import ProtectedRoute from '@/components/auth/ProtectedRoute';
const YourProtectedComponent = () => {
return (
<ProtectedRoute>
{/* Your component content */}
</ProtectedRoute>
);
};- Use the
useAuthhook for authentication data:
import { useAuth } from '@/contexts/AuthContext';
const { user, isAuthenticated, logout } = useAuth();Use the useAuthGuard hook for custom authentication logic:
import { useAuthGuard } from '@/hooks/useAuthGuard';
const { isAuthenticated, isLoading, shouldRender } = useAuthGuard({
redirectTo: '/login',
requireAuth: true
});The authentication system can be tested by:
- Connecting a Solana wallet
- Navigating to protected routes
- Testing logout functionality
- Verifying persistent sessions
- Wallet not connecting: Check Solana network configuration
- API errors: Verify backend is running and API URL is correct
- Token issues: Clear localStorage and reconnect wallet
- Route protection not working: Ensure ProtectedRoute is properly implemented
Enable debug logging by adding to your component:
const { user, isAuthenticated, isLoading } = useAuth();
console.log('Auth state:', { user, isAuthenticated, isLoading });