File tree Expand file tree Collapse file tree 5 files changed +101
-10
lines changed
tests/Feature/API/V1/User Expand file tree Collapse file tree 5 files changed +101
-10
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace App \Http \Controllers \Api \V1 \User ;
6+
7+ use App \Http \Controllers \Controller ;
8+ use App \Http \Resources \V1 \Auth \UserResource ;
9+ use Dedoc \Scramble \Attributes \Group ;
10+ use Illuminate \Http \JsonResponse ;
11+ use Illuminate \Http \Request ;
12+
13+ #[Group('User ' , weight: 0 )]
14+ class MeController extends Controller
15+ {
16+ /**
17+ * User Profile API
18+ *
19+ * Handle the incoming request to get the authenticated user.
20+ *
21+ * @response array{status: true, message: string, data: UserResource}
22+ */
23+ public function __invoke (Request $ request ): JsonResponse
24+ {
25+ /**
26+ * Successful response
27+ */
28+
29+ /** @var \App\Models\User $user */
30+ $ user = $ request ->user ();
31+ $ user ->load (['roles.permissions ' ]);
32+
33+ return response ()->apiSuccess (
34+ new \App \Http \Resources \V1 \Auth \UserResource ($ user ),
35+ __ ('common.success ' )
36+ );
37+ }
38+ }
Original file line number Diff line number Diff line change @@ -49,13 +49,16 @@ public function toArray(Request $request): array
4949
5050 return array_values (array_unique ($ permissionSlugs ));
5151 }),
52- $ this ->mergeWhen (isset ($ this ->resource ->access_token ), [
53- 'access_token ' => $ this ->resource ->access_token ,
54- 'refresh_token ' => $ this ->resource ->refresh_token ,
55- 'access_token_expires_at ' => $ this ->resource ->access_token_expires_at ?->toISOString(),
56- 'refresh_token_expires_at ' => $ this ->resource ->refresh_token_expires_at ?->toISOString(),
57- 'token_type ' => 'Bearer ' ,
58- ]),
52+ $ this ->mergeWhen (
53+ array_key_exists ('access_token ' , $ this ->resource ->getAttributes ()),
54+ fn () => [
55+ 'access_token ' => $ this ->resource ->getAttributes ()['access_token ' ],
56+ 'refresh_token ' => $ this ->resource ->getAttributes ()['refresh_token ' ] ?? null ,
57+ 'access_token_expires_at ' => optional ($ this ->resource ->getAttributes ()['access_token_expires_at ' ] ?? null )?->toISOString(),
58+ 'refresh_token_expires_at ' => optional ($ this ->resource ->getAttributes ()['refresh_token_expires_at ' ] ?? null )?->toISOString(),
59+ 'token_type ' => 'Bearer ' ,
60+ ]
61+ ),
5962 ];
6063 }
6164}
Original file line number Diff line number Diff line change 1111 */
1212
1313 'something_went_wrong ' => 'Something went wrong! Try again later. ' ,
14+ 'success ' => 'Response returned successfully. ' ,
1415];
Original file line number Diff line number Diff line change 1818
1919 // User Routes
2020 Route::middleware (['auth:sanctum ' , 'ability:access-api ' ])->group (function () {
21- Route::get ('/me ' , function (Request $ request ) {
22- return auth ()->user ();
23- });
21+ Route::get ('/me ' , \App \Http \Controllers \Api \V1 \User \MeController::class);
2422 });
2523});
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ use App \Models \User ;
6+ use Laravel \Sanctum \Sanctum ;
7+
8+ describe ('API/V1/User/MeController ' , function () {
9+ it ('returns authenticated user profile with roles and permissions ' , function () {
10+ // Create a test user
11+ $ user = User::factory ()->create ([
12+ 'name ' => 'John Doe ' ,
13+ 14+ ]);
15+
16+ // Authenticate the user with Sanctum
17+ Sanctum::actingAs ($ user , ['access-api ' ]);
18+
19+ // Make request to /me endpoint
20+ $ response = $ this ->getJson ('/api/v1/me ' );
21+
22+ // Assert response structure
23+ $ response
24+ ->assertStatus (200 )
25+ ->assertJsonStructure ([
26+ 'status ' ,
27+ 'message ' ,
28+ 'data ' => [
29+ 'id ' ,
30+ 'name ' ,
31+ 'email ' ,
32+ 'email_verified_at ' ,
33+ 'bio ' ,
34+ 'avatar_url ' ,
35+ 'twitter ' ,
36+ 'facebook ' ,
37+ 'linkedin ' ,
38+ 'github ' ,
39+ 'website ' ,
40+ ],
41+ ])
42+ ->assertJson ([
43+ 'status ' => true ,
44+ 'data ' => [
45+ 'id ' => $ user ->id ,
46+ 'name ' => 'John Doe ' ,
47+ 48+ ],
49+ ]);
50+ });
51+ });
You can’t perform that action at this time.
0 commit comments