# 🚀 Quick Start - Authentication System
## ✅ What's Been Set Up
Your Laravel + Vue 3 + Inertia.js application now has a complete authentication system with:
1. ✅ **Axios** - Already installed (v1.11.0)
2. ✅ **Pinia** - State management (already installed, replaces Vuex)
3. ✅ **Auth Service** - API calls to `http://localhost:8000/api/auth/login`
4. ✅ **Auth Store** - Centralized authentication state
5. ✅ **Route Guards** - Protect pages based on authentication
6. ✅ **Persistence** - Auth state survives page reloads
## 📁 Files Created
```
resources/js/
├── services/
│ └── authService.ts ← API service for auth endpoints
├── stores/
│ └── auth.ts ← Pinia store for auth state
├── middleware/
│ └── auth.ts ← Route guard middleware
├── composables/
│ └── useAuthGuard.ts ← Composables for protecting routes
└── components/
└── LoginExample.vue ← Example login component
```
## 🎯 How to Use
### 1. In Your Login Component
```vue
```
### 2. Protect Your Dashboard
```vue
Protected Dashboard
```
### 3. Display User Info
```vue
Welcome, {{ authStore.currentUser?.name }}!
```
## 🔧 Backend Requirements
You need these Laravel API endpoints:
### Login Endpoint
```php
// POST http://localhost:8000/api/auth/login
Route::post('api/auth/login', function(Request $request) {
$credentials = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
if (Auth::attempt($credentials)) {
return response()->json([
'user' => Auth::user(),
'token' => Auth::user()->createToken('auth')->plainTextToken
]);
}
return response()->json(['message' => 'Invalid credentials'], 401);
});
```
### User Endpoint
```php
// GET http://localhost:8000/api/auth/user
Route::get('api/auth/user', function() {
return response()->json(['user' => Auth::user()]);
})->middleware('auth:sanctum');
```
### Logout Endpoint
```php
// POST http://localhost:8000/api/auth/logout
Route::post('api/auth/logout', function(Request $request) {
$request->user()->currentAccessToken()->delete();
return response()->json(['message' => 'Logged out']);
})->middleware('auth:sanctum');
```
## 🎨 Complete Working Examples
### Example 1: Simple Login Page
```vue
Login
{{ authStore.error }}
```
### Example 2: Protected Dashboard
```vue
Dashboard
{{ authStore.currentUser?.name }}
Welcome back, {{ authStore.currentUser?.name }}!
Email: {{ authStore.currentUser?.email }}
```
## 📋 Auth Store API Reference
```typescript
const authStore = useAuthStore();
// State
authStore.user // User object or null
authStore.token // Auth token or null
authStore.loading // Boolean: loading state
authStore.error // String: error message or null
// Getters
authStore.isAuthenticated // Boolean: is user logged in?
authStore.currentUser // User object or null
// Actions
await authStore.login({ email, password, remember }) // Returns boolean
await authStore.logout() // Returns void
await authStore.checkAuth() // Returns boolean
await authStore.fetchUser() // Returns void
authStore.clearError() // Returns void
authStore.setUser(userData) // Returns void
```
## ✨ Key Features
- ✅ **Auto-persistence**: Auth state saved to localStorage
- ✅ **CSRF Protection**: Automatically handled
- ✅ **Error Handling**: Built-in error messages
- ✅ **Loading States**: Track async operations
- ✅ **TypeScript**: Full type safety
- ✅ **Inertia Compatible**: Works with your existing setup
## 🐛 Troubleshooting
### Issue: User not staying logged in
**Solution**: Make sure your Laravel API returns the user object on the `/api/auth/user` endpoint
### Issue: CSRF token mismatch
**Solution**: Your existing `resources/js/lib/http.ts` handles this automatically
### Issue: Redirect not working
**Solution**: Ensure you're using `router.get()` from `@inertiajs/vue3`
## 📚 Next Steps
1. Create your Laravel API endpoints (see examples above)
2. Update your login page component
3. Add `useAuthGuard()` to protected pages
4. Test the authentication flow
For detailed documentation, see `AUTH_SETUP.md`