diff --git a/src/app/Enums/ResponseStatus.php b/src/app/Enums/ResponseStatus.php new file mode 100644 index 0000000..1805124 --- /dev/null +++ b/src/app/Enums/ResponseStatus.php @@ -0,0 +1,40 @@ + 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', + }; + } +} diff --git a/src/app/Http/Controllers/SessionController.php b/src/app/Http/Controllers/SessionController.php new file mode 100644 index 0000000..440b3ed --- /dev/null +++ b/src/app/Http/Controllers/SessionController.php @@ -0,0 +1,58 @@ +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() + ); + } +} diff --git a/src/app/Http/Requests/LibraryPassReadRequest.php b/src/app/Http/Requests/LibraryPassReadRequest.php new file mode 100644 index 0000000..8551b41 --- /dev/null +++ b/src/app/Http/Requests/LibraryPassReadRequest.php @@ -0,0 +1,29 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + return [ + 'code' => ['required', 'string', 'max:255'] + ]; + } +} diff --git a/src/app/Traits/CommonTrait.php b/src/app/Traits/CommonTrait.php new file mode 100644 index 0000000..b788571 --- /dev/null +++ b/src/app/Traits/CommonTrait.php @@ -0,0 +1,28 @@ +json([ + 'status' => $status, + 'message' => $message, + 'data' => $data + ], $code); + } +} diff --git a/src/routes/web.php b/src/routes/web.php index dcc5223..def7080 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -1,6 +1,7 @@ name('auth');