Skip to content

Commit d968636

Browse files
committed
feat: add endpoint to retrieve current authenticated user information
1 parent f381af9 commit d968636

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

contexts/Authorization/Application/Coordinators/AuthenticationCoordinator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ public function login(LoginDTO $dto)
3232
'token' => $token,
3333
];
3434
}
35+
36+
public function me()
37+
{
38+
$user = $this->userRepository->getCurrentUser();
39+
40+
return [
41+
'user' => [
42+
'id' => $user->getId()->getValue(),
43+
'email' => $user->getEmail()->getValue(),
44+
'display_name' => $user->getDisplayName(),
45+
],
46+
];
47+
}
3548
}

contexts/Authorization/Infrastructure/Routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
});
1515
});
1616

17+
Route::name('Authentication')->group(function () {
18+
Route::controller(AuthenticationController::class)->prefix('auth')->name('Auth.')->group(function () {
19+
Route::get('me', 'me')->name('me');
20+
});
21+
});
22+
1723
Route::middleware([])->name('Authorization.')->group(function () {
1824
Route::controller(UserIdentityController::class)->prefix('users')->name('User.')->group(function () {
1925
Route::get('{id}', 'getUser')->name('getUser');

contexts/Authorization/Presentation/Controllers/AuthenticationController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ public function login(LoginRequest $request)
1919

2020
return $this->success($result)->send();
2121
}
22+
23+
public function me()
24+
{
25+
$result = app(AuthenticationCoordinator::class)->me();
26+
27+
return $this->success($result)->send();
28+
}
2229
}

contexts/Authorization/Tests/Feature/AuthenticationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,36 @@
7878
'message' => 'Invalid login credentials or account access restricted',
7979
]);
8080
});
81+
82+
it('can get user info via api', function () {
83+
$user = UserRecord::factory()->create([
84+
'email' => 'test@email.com',
85+
'password' => password_hash('password', PASSWORD_ARGON2ID),
86+
'status' => UserRecord::mapStatusToRecord(UserStatus::active()),
87+
]);
88+
$this->actingAs($user);
89+
90+
$response = $this->getJson('auth/me');
91+
92+
$response->assertStatus(200);
93+
$response->assertJson([
94+
'data' => [
95+
'user' => [
96+
'id' => $user->id,
97+
'email' => $user->email,
98+
'display_name' => $user->display_name,
99+
],
100+
],
101+
]);
102+
});
103+
104+
it('cannot get user info without authentication', function () {
105+
$response = $this->getJson('auth/me');
106+
107+
$response->assertStatus(401);
108+
$response->assertJsonMissing([
109+
'data' => [
110+
'user' => [],
111+
],
112+
]);
113+
});

0 commit comments

Comments
 (0)