Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): add /login endpoint for authentication #42

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class AuthController extends Controller
{
/**
* Handle user login.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
$credentials = $request->validate([
'email' => ['required', 'string', 'email'],
'password' => ['required']
]);

if (!auth()->attempt($credentials)) {
return response()->json([
'message' => 'Invalid login credentials'
], Response::HTTP_UNAUTHORIZED);
}

/** @var \App\Models\User $user */
$user = auth()->user();
$token = $user->createToken('authToken')->plainTextToken;

return response()->json([
'user' => $user,
'access_token' => $token
], Response::HTTP_OK);
}
}
2 changes: 2 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function run(): void
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// 'email_verified_at' => now(),
// 'password' => bcrypt('test1234'),
// ]);
}
}
10 changes: 8 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\AuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -14,6 +15,11 @@
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
/**
* Defines the API routes for version 1.
*
* @group v1
*/
Route::group(['prefix' => 'v1'], function () {
require __DIR__ . '/api/v1/auth.php';
});
6 changes: 6 additions & 0 deletions routes/api/v1/auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use App\Http\Controllers\AuthController;
use Illuminate\Support\Facades\Route;

Route::post('/login', [AuthController::class, 'login']);
68 changes: 68 additions & 0 deletions tests/Feature/AuthControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Response;
use Tests\TestCase;

class AuthControllerTest extends TestCase
{
use RefreshDatabase, WithFaker;

/**
* Test login with valid credentials.
*
* @return void
*/
public function test_login_with_valid_credentials(): void
{

$user = User::factory()->create([
'email' => 'valid@example.com',
'password' => bcrypt('ValidPassword'),
]);

$response = $this->postJson('/api/v1/login', [
'email' => 'valid@example.com',
'password' => 'ValidPassword',
]);

$response->assertStatus(Response::HTTP_OK)
->assertJsonStructure([
'user' => [
'id',
'name',
'email',
'email_verified_at',
'created_at',
'updated_at',
],
'access_token',
]);

$this->assertAuthenticatedAs($user);
}

/**
* Test login with invalid credentials.
*
* @return void
*/
public function test_login_with_invalid_credentials(): void
{
$response = $this->postJson('/api/v1/login', [
'email' => 'invalid@example.com',
'password' => 'invalidpassword',
]);

$response->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertJson([
'message' => 'Invalid login credentials',
]);

$this->assertGuest();
}
}