Skip to content

Commit 04ecc5e

Browse files
committed
update auth
1 parent 86100ec commit 04ecc5e

File tree

9 files changed

+119
-88
lines changed

9 files changed

+119
-88
lines changed

.env.ff

Lines changed: 0 additions & 73 deletions
This file was deleted.

app/Http/Controllers/V1/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* @OA\Info(
9-
* title="Mrprototcol",
9+
* title="Documentation",
1010
* version="1.0"
1111
* ),
1212
* @OA\SecurityScheme(
@@ -19,11 +19,11 @@
1919
* ),
2020
* @OA\Server(
2121
* description="Base URL",
22-
* url="https://mrprotocol.com/api/v1"
22+
* url="https://domain.dev/v1"
2323
* ),
2424
* @OA\Server(
2525
* description="Local Base URL",
26-
* url="http://127.0.0.1:8000/api/v1"
26+
* url="http://127.0.0.1:8000/v1"
2727
* ),
2828
* @OA\Response(
2929
* response=500,

config/l5-swagger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/*
1515
* Route for accessing api documentation interface
1616
*/
17-
'api' => 'api/documentation',
17+
'api' => 'v1/documentation',
1818
],
1919
'paths' => [
2020
/*

config/sanctum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
|
4848
*/
4949

50-
'expiration' => null,
50+
'expiration' => 60,
5151

5252
/*
5353
|--------------------------------------------------------------------------

database/migrations/2024_07_01_212031_create_personal_access_tokens_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ public function up(): void
1313
{
1414
Schema::create('personal_access_tokens', function (Blueprint $table) {
1515
$table->id();
16-
$table->morphs('tokenable');
16+
$table->uuidMorphs('tokenable');
1717
$table->string('name');
1818
$table->string('token', 64)->unique();
1919
$table->text('abilities')->nullable();
2020
$table->timestamp('last_used_at')->nullable();
2121
$table->timestamp('expires_at')->nullable();
22-
$table->timestamps();
22+
$table->timestamp('created_at')->useCurrent();
23+
$table->timestamp('updated_at')->useCurrent();
2324
});
2425
}
2526

routes/v1/auth.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
->name('verification.send');
3535

3636
Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
37-
->middleware('auth')
37+
->middleware('auth:sanctum')
3838
->name('logout');
3939

4040
Route::get('/google/url', [GoogleAuthController::class, 'googleAuthUrl']);
4141
Route::post('/google/login', [GoogleAuthController::class, 'googleOauthLogin']);
4242

43+
Route::post('/refresh-token', [AuthenticatedSessionController::class, 'refreshToken']);
4344
});

src/modules/V1/Auth/Controllers/AuthenticatedSessionController.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,49 @@ public function store(LoginRequest $request): \Illuminate\Http\JsonResponse
104104
public function destroy(Request $request): \Illuminate\Http\JsonResponse
105105
{
106106
if ( ! Auth::check()) {
107-
return ResponseHelper::error('Unauthorized', 401);
107+
return ResponseHelper::error('Unauthenticated', 401);
108108
}
109109

110110
$request->user()->tokens()->delete();
111111

112112
return ResponseHelper::success(message: 'logged out successfully', status: 204);
113113
}
114+
115+
/**
116+
* @OA\Post(
117+
* path="/auth/refresh-token",
118+
* summary="Refresh the authentication token",
119+
* description="Revokes the existing token and generates a new token for the authenticated user.",
120+
* tags={"Authentication"},
121+
* @OA\Response(
122+
* response=200,
123+
* description="Token refreshed successfully",
124+
* @OA\JsonContent(
125+
* @OA\Property(property="status", type="boolean", example="success"),
126+
* @OA\Property(property="message", type="string", example="Token refreshed"),
127+
* @OA\Property(property="meta", type="object",
128+
* @OA\Property(property="accessToken", type="string", example="1|abc123..."),
129+
* @OA\Property(property="expires_in", type="integer", example=60)
130+
* )
131+
* )
132+
* ),
133+
* @OA\Response(response=401, ref="#/components/responses/401"),
134+
* )
135+
*/
136+
public function refreshToken(Request $request): \Illuminate\Http\JsonResponse
137+
{
138+
$user = auth()->user();
139+
$user->tokens()->delete(); // Revoke all existing tokens
140+
141+
$device = Str::limit($request->userAgent(), 255);
142+
$token = $user->createToken($device)->plainTextToken;
143+
144+
return ResponseHelper::success(
145+
message: 'Token refreshed',
146+
meta: [
147+
'accessToken' => $token,
148+
'expires_in' => config('sanctum.expiration'),
149+
],
150+
);
151+
}
114152
}

storage/api-docs/api-docs.json

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"openapi": "3.0.0",
33
"info": {
4-
"title": "Mrprototcol",
4+
"title": "Documentation",
55
"version": "1.0"
66
},
77
"servers": [
88
{
9-
"url": "https://mrprotocol.com/api/v1",
9+
"url": "https://domain.dev/v1",
1010
"description": "Base URL"
1111
},
1212
{
13-
"url": "http://127.0.0.1:8000/api/v1",
13+
"url": "http://127.0.0.1:8000/v1",
1414
"description": "Local Base URL"
1515
}
1616
],
@@ -132,6 +132,54 @@
132132
]
133133
}
134134
},
135+
"/auth/refresh-token": {
136+
"post": {
137+
"tags": [
138+
"Authentication"
139+
],
140+
"summary": "Refresh the authentication token",
141+
"description": "Revokes the existing token and generates a new token for the authenticated user.",
142+
"operationId": "0620fc3f15b3351c5a388cd1d0ee913f",
143+
"responses": {
144+
"200": {
145+
"description": "Token refreshed successfully",
146+
"content": {
147+
"application/json": {
148+
"schema": {
149+
"properties": {
150+
"status": {
151+
"type": "boolean",
152+
"example": "success"
153+
},
154+
"message": {
155+
"type": "string",
156+
"example": "Token refreshed"
157+
},
158+
"meta": {
159+
"properties": {
160+
"accessToken": {
161+
"type": "string",
162+
"example": "1|abc123..."
163+
},
164+
"expires_in": {
165+
"type": "integer",
166+
"example": 60
167+
}
168+
},
169+
"type": "object"
170+
}
171+
},
172+
"type": "object"
173+
}
174+
}
175+
}
176+
},
177+
"401": {
178+
"$ref": "#/components/responses/401"
179+
}
180+
}
181+
}
182+
},
135183
"/auth/email/verification-link": {
136184
"post": {
137185
"tags": [

tests/Feature/V1/Auth/AuthenticationTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
beforeEach(function (): void {
1010
$this->seed(RoleSeeder::class);
11-
User::factory()->create();
1211
});
1312

1413
it('logs in successfully with valid credentials and verified email', function (): void {
@@ -38,7 +37,7 @@
3837
]);
3938
});
4039

41-
test('users cannot authenticate with invalid credentials', function (): void {
40+
it('users cannot authenticate with invalid credentials', function (): void {
4241
$user = User::factory()->create();
4342

4443
$response = $this->post('/v1/auth/login', [
@@ -54,7 +53,7 @@
5453
]);
5554
});
5655

57-
test('users cannot authenticate with unverified email', function (): void {
56+
it('users cannot authenticate with unverified email', function (): void {
5857
$user = User::factory()->create([
5958
'email_verified_at' => null,
6059
]);
@@ -71,3 +70,20 @@
7170
'message' => 'Email not verified. Kindly verify your email',
7271
]);
7372
});
73+
74+
it('logs out successfully when authenticated', function () {
75+
$user = User::factory()->create();
76+
$token = $user->createToken('TestDevice')->plainTextToken;
77+
78+
$response = $this->postJson('/v1/auth/logout', [], ['Authorization' => "Bearer $token"]);
79+
80+
$response->assertStatus(204);
81+
82+
$this->assertCount(0, $user->tokens);
83+
});
84+
85+
it('returns error when not authenticated', function () {
86+
$response = $this->postJson('/v1/auth/logout');
87+
88+
$response->assertStatus(401);
89+
});

0 commit comments

Comments
 (0)