feat: Add token-based authentication with Laravel Sanctum
This commit is contained in:
parent
9ac865d71c
commit
ced142b8f0
3 changed files with 42 additions and 0 deletions
|
|
@ -1,5 +1,16 @@
|
|||
# API Documentation
|
||||
|
||||
## Authentication
|
||||
|
||||
### POST /api/login
|
||||
- **Description**: Authenticate a user and generate a token.
|
||||
- **Request Body**: JSON object with `email` and `password`.
|
||||
- **Response**: JSON object with the generated token.
|
||||
|
||||
### POST /api/logout
|
||||
- **Description**: Logout the user and revoke the token.
|
||||
- **Response**: JSON object with a success message.
|
||||
|
||||
## Frontend API
|
||||
|
||||
### GET /api/frontend
|
||||
|
|
|
|||
31
app/Http/Controllers/Api/AuthController.php
Normal file
31
app/Http/Controllers/Api/AuthController.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function login(Request $request)
|
||||
{
|
||||
$credentials = $request->only('email', 'password');
|
||||
|
||||
if (Auth::attempt($credentials)) {
|
||||
$user = Auth::user();
|
||||
$token = $user->createToken('authToken')->plainTextToken;
|
||||
|
||||
return response()->json(['token' => $token], 200);
|
||||
}
|
||||
|
||||
return response()->json(['error' => 'Unauthorized'], 401);
|
||||
}
|
||||
|
||||
public function logout(Request $request)
|
||||
{
|
||||
$request->user()->tokens()->delete();
|
||||
|
||||
return response()->json(['message' => 'Logged out'], 200);
|
||||
}
|
||||
}
|
||||
0
app/Http/Kernel.php
Normal file
0
app/Http/Kernel.php
Normal file
Loading…
Reference in a new issue