Skip to content

Commit

Permalink
Added authentication endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtendo12 committed Dec 11, 2023
1 parent 9517024 commit 79b0428
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/app/Enums/ResponseStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Enums;

enum ResponseStatus: string
{
case success = 'success';
case error = 'error';
case unauthorized = 'unauthorized';
case forbidden = 'forbidden';
case notFound = 'not_found';
case validationError = 'validation_error';
case internalServerError = 'internal_server_error';

public function getStatusCode(): int
{
return match ($this) {
static::success => 200,
static::error => 400,
static::unauthorized => 401,
static::forbidden => 403,
static::notFound => 404,
static::validationError => 422,
static::internalServerError => 500,
};
}

public function label(): string
{
return match ($this) {
static::success => 'Success',
static::error => 'Error',
static::unauthorized => 'Unauthorized',
static::forbidden => 'Forbidden',
static::notFound => 'Not Found',
static::validationError => 'Validation Error',
static::internalServerError => 'Internal Server Error',
};
}
}
58 changes: 58 additions & 0 deletions src/app/Http/Controllers/SessionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Controllers;

use App\Enums\ResponseStatus;
use App\Http\Requests\LibraryPassReadRequest;
use App\Models\LibraryPass;
use App\Models\User;
use App\Traits\CommonTrait;

class SessionController extends Controller
{
use CommonTrait;
public function auth(LibraryPassReadRequest $request)
{
$code = $request->validated()['code'];

if (!isset($code)) {
return $this->CommonResponse(
ResponseStatus::validationError,
'Code is required',
null,
ResponseStatus::validationError->getStatusCode()
);
}

$pass = LibraryPass::where('barcode', $code)->first();

if (!isset($pass)) {
return $this->CommonResponse(
ResponseStatus::unauthorized,
'Incorrect code',
null,
ResponseStatus::unauthorized->getStatusCode()
);
}

$user = User::find($pass->user_id);

if (!isset($user)) {
return $this->CommonResponse(
ResponseStatus::unauthorized,
'Incorrect code',
null,
ResponseStatus::unauthorized->getStatusCode()
);
}

Auth()->login($user);

return $this->CommonResponse(
ResponseStatus::success,
'User logged in current session',
['user' => $user],
ResponseStatus::success->getStatusCode()
);
}
}
29 changes: 29 additions & 0 deletions src/app/Http/Requests/LibraryPassReadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class LibraryPassReadRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return !Auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'code' => ['required', 'string', 'max:255']
];
}
}
28 changes: 28 additions & 0 deletions src/app/Traits/CommonTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Traits;

use App\Enums\ResponseStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

trait CommonTrait {

/**
* Common response
*
* @param ResponseStatus $status response status
* @param string $message response message
* @param array|null $data response data
* @param int $code http status code
* @return JsonResponse response formatted in json
*/
public function CommonResponse(ResponseStatus $status, string $message, array|null $data, int $code): JsonResponse
{
return response()->json([
'status' => $status,
'message' => $message,
'data' => $data
], $code);
}
}
3 changes: 3 additions & 0 deletions src/routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\IndexController;
use App\Http\Controllers\SessionController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -15,3 +16,5 @@
*/

Route::get('/', [IndexController::class, 'index']);

Route::post('/auth', [SessionController::class, 'auth'])->name('auth');

0 comments on commit 79b0428

Please sign in to comment.