-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add Sanctum API token authentication (Issue #50 PR-4) #60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01cad0d
feat: Add Sanctum API token authentication (Issue #50 PR-4)
kevalyq 4add8ed
fix: Address Copilot review comments
kevalyq 18e5676
fix: Address ALL 20 Copilot review comments
kevalyq 7df9464
fix: Address 2 final Copilot review comments (quality gaps)
kevalyq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,91 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Http\Requests\TokenRequest; | ||
| use App\Models\User; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Hash; | ||
| use Illuminate\Validation\ValidationException; | ||
|
|
||
| class AuthController extends Controller | ||
| { | ||
| /** | ||
| * Generate a new API token for the user. | ||
| * | ||
| * @throws ValidationException | ||
| */ | ||
| public function token(TokenRequest $request): JsonResponse | ||
| { | ||
| /** @var array{email: string, password: string, device_name?: string} $validated */ | ||
| $validated = $request->validated(); | ||
|
|
||
| $user = User::where('email', $validated['email'])->first(); | ||
|
|
||
| if (! $user || ! Hash::check($validated['password'], $user->password)) { | ||
| throw ValidationException::withMessages([ | ||
| 'email' => ['The provided credentials are incorrect.'], | ||
| ]); | ||
| } | ||
|
|
||
| $deviceName = $validated['device_name'] ?? 'api-client'; | ||
| $token = $user->createToken($deviceName); | ||
|
|
||
| return response()->json([ | ||
| 'token' => $token->plainTextToken, | ||
| 'user' => [ | ||
| 'id' => $user->id, | ||
| 'name' => $user->name, | ||
| 'email' => $user->email, | ||
| ], | ||
| ], 201); | ||
| } | ||
|
|
||
| /** | ||
| * Revoke the current user's access token. | ||
| */ | ||
| public function logout(Request $request): JsonResponse | ||
| { | ||
| /** @var User $user */ | ||
| $user = $request->user(); | ||
| $user->currentAccessToken()->delete(); | ||
|
|
||
| return response()->json([ | ||
| 'message' => 'Token revoked successfully.', | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Revoke all tokens for the authenticated user. | ||
| */ | ||
| public function logoutAll(Request $request): JsonResponse | ||
| { | ||
| /** @var User $user */ | ||
| $user = $request->user(); | ||
|
|
||
| $user->tokens()->delete(); | ||
|
|
||
| return response()->json([ | ||
| 'message' => 'All tokens revoked successfully.', | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Get the authenticated user's information. | ||
| */ | ||
| public function me(Request $request): JsonResponse | ||
| { | ||
| /** @var User $user */ | ||
| $user = $request->user(); | ||
|
|
||
| return response()->json([ | ||
| 'id' => $user->id, | ||
| 'name' => $user->name, | ||
| 'email' => $user->email, | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or 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,48 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class TokenRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'email' => 'required|email', | ||
| 'password' => 'required|string', | ||
| 'device_name' => 'nullable|string|max:255', | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get custom validation error messages. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| public function messages(): array | ||
| { | ||
| return [ | ||
| 'email.required' => 'Email address is required.', | ||
| 'email.email' => 'Please provide a valid email address.', | ||
| 'password.required' => 'Password is required.', | ||
| 'device_name.max' => 'Device name must not exceed 255 characters.', | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
database/migrations/2025_11_01_195727_create_personal_access_tokens_table.php
This file contains hidden or 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,36 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| /** | ||
| * Run the migrations. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| Schema::create('personal_access_tokens', function (Blueprint $table) { | ||
| $table->id(); | ||
| $table->morphs('tokenable'); | ||
| $table->string('name'); | ||
| $table->string('token', 64)->unique(); | ||
| $table->text('abilities')->nullable(); | ||
| $table->timestamp('last_used_at')->nullable(); | ||
| $table->timestamp('expires_at')->nullable(); | ||
| $table->timestamps(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| */ | ||
| public function down(): void | ||
| { | ||
| Schema::dropIfExists('personal_access_tokens'); | ||
| } | ||
| }; |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.