-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9517024
commit 79b0428
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters