Skip to content

Commit

Permalink
Merge pull request #383 from tulbadex/fix/auth-url-response
Browse files Browse the repository at this point in the history
fix: auth url response
  • Loading branch information
timiajayi authored Aug 6, 2024
2 parents 4fde060 + be223f5 commit 28d590f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 64 deletions.
22 changes: 17 additions & 5 deletions app/Http/Controllers/Api/V1/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function store(Request $request)

// Check if validation fails
if ($validator->fails()) {
return $this->apiResponse($validator->errors(), 422);
return $this->apiResponse(message: $validator->errors(), status_code: 400);
}

try {
Expand All @@ -70,7 +70,7 @@ public function store(Request $request)
'password' => Hash::make($request->password),
]);

$user->profile()->create([
$profile = $user->profile()->create([
'first_name' => $request->first_name,
'last_name' => $request->last_name
]);
Expand All @@ -80,10 +80,22 @@ public function store(Request $request)

DB::commit();
$data = [
'accessToken' => $token,
'user' => $user,
'user' => [
'id' => $user->id,
'first_name' => $profile->first_name,
'last_name' => $profile->last_name,
'email' => $user->email,
'avatar_url' => $profile->avatar_url,
'role' => $user->role
],
];
return $this->apiResponse('Registration successful', Response::HTTP_CREATED, $data);

return $this->apiResponse(
message: 'User Created Successfully',
status_code: Response::HTTP_CREATED,
data: $data,
token: $token
);
} catch (\Exception $e) {
DB::rollBack();
Log::error('Registration error: ' . $e->getMessage());
Expand Down
36 changes: 16 additions & 20 deletions app/Http/Controllers/Api/V1/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public function login(Request $request)

if ($validator->fails()) {
return response()->json([
'message' => 'Validation Error',
'errors' => $validator->errors()
], 422);
'status_code' => 400,
'message' => $validator->errors(),
'errors' => 'Bad Request'
], 400);
}

/* $key = 'login_attempts_' . $request->ip();
Expand All @@ -41,9 +42,8 @@ public function login(Request $request)
$key = 'login_attempts_'.request()->ip();
RateLimiter::hit($key,3600);
return response()->json([
'message' => 'Invalid credentials',
'error' => 'authentication_failed',
'status_code' => 401
'status_code' => 401,
'message' => 'Invalid credentials'
], 401);
}

Expand All @@ -61,25 +61,21 @@ public function login(Request $request)
} else {
$last_name = "";
}
$profile = $user->profile();

return response()->json([
'status_code' => 200,
'message' => 'Login successful',
'access_token' => $token,
'data' => [
'user' => [
'id' => $user->id,
'first_name' => $first_name,
'last_name' => $last_name,
'first_name' => $profile->first_name ?? null,
'last_name' => $profile->last_name ?? null,
'email' => $user->email,
'role' => $user->role,
'signup_type' => $user->signup_type,
'is_active' => $user->is_active,
'is_verified' => $user->is_verified,
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
// 'last_login_at' => $user->last_login_at,
],
'access_token' => $token,
'refresh_token' => null // JWT does not inherently support refresh tokens; you might need to implement this yourself
"avatar_url" => $profile->avatar_url ?? null
]
]
], 200);
}
Expand All @@ -89,14 +85,14 @@ public function logout()
try {
JWTAuth::parseToken()->invalidate(true);
return response()->json([
'status_code' => 200,
'message' => 'Logout successful',
'status_code' => 200
], 200);
} catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json([
'status_code' => 401,
'message' => $e->getMessage(),
'error' => $this->getErrorCode($e),
'status_code' => 401
'error' => $this->getErrorCode($e)
], 401);
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Api/V1/Auth/SocialAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function handleGoogleCallback()
$token = JWTAuth::fromUser($user);

$response = [
'status' => 'success',
'status_code' => 200,
'message' => 'User successfully authenticated',
'access_token' => $token,
'data' => [
Expand Down Expand Up @@ -97,7 +97,7 @@ public function saveGoogleRequest(Request $request)
$token = JWTAuth::fromUser($user);

$response = [
'status' => 'success',
'status_code' => 201,
'message' => 'User successfully authenticated',
'access_token' => $token,
'data' => [
Expand Down Expand Up @@ -148,7 +148,7 @@ public function callbackFromFacebook()
$token = JWTAuth::fromUser($user);

$response = [
'status' => 'success',
'status_code' => 200,
'message' => 'User successfully authenticated',
'access_token' => $token,
'data' => [
Expand Down Expand Up @@ -195,7 +195,7 @@ public function saveFacebookRequest(Request $request)
$token = JWTAuth::fromUser($user);

$response = [
'status' => 'success',
'status' => 201,
'message' => 'User successfully authenticated',
'access_token' => $token,
'data' => [
Expand Down
8 changes: 6 additions & 2 deletions app/Traits/HttpResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
trait HttpResponses
{

protected function apiResponse($message = '', $status_code = 200, $data = null): JsonResponse
protected function apiResponse($message = '', $status_code = 200, $data = null, $token = null): JsonResponse
{
$response = [
'message' => $message,
'status_code' => $status_code,
'message' => $message,
];

if ($data !== null) {
$response['access_token'] = $token;
}

// Conditionally add the 'data' key if $data is not null
if ($data !== null) {
$response['data'] = $data;
Expand Down
10 changes: 3 additions & 7 deletions tests/Feature/CookiePreferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,18 @@ public function test_user_cookie_preferences_workflow()

$loginResponse->assertStatus(200)
->assertJsonStructure([
'status_code',
'message',
'access_token',
'data' => [
'user' => [
'id',
'first_name',
'last_name',
'email',
'role',
'signup_type',
'is_active',
'is_verified',
'created_at',
'updated_at'
'avatar_url',
],
'access_token',
'refresh_token'
]
]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/InvitationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setUp(): void
'password' => 'Ed8M7s*)?e:hTb^#&;C!<y',
]);

$this->accessToken = $loginResponse['data']['access_token'];
$this->accessToken = $loginResponse['access_token'];

// Create an organisation
$orgResponse = $this->postJson('/api/v1/organizations', [
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testUserLogin()
$response->assertStatus(200);
$response->assertJsonStructure([
'message',
'data' => ['user' => ['id', 'email', 'role', 'signup_type', 'is_active', 'is_verified', 'created_at', 'updated_at'], 'access_token', 'refresh_token']
'data' => ['user' => ['id', 'first_name', 'last_name', 'email', 'role', 'avatar_url']]
]);
}
}
4 changes: 2 additions & 2 deletions tests/Feature/ProductDeletionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function test_authenticated_user_can_delete_product()
$response->assertStatus(201);

// Retrieve the JWT token from the registration response
$token = $response->json('data.accessToken');
$token = $response->json('access_token');

$this->assertNotEmpty($token);

Expand Down Expand Up @@ -95,7 +95,7 @@ public function test_authenticated_user_cannot_delete_non_existent_product()
$response->assertStatus(201);

// Retrieve the JWT token from the registration response
$token = $response->json('data.accessToken');
$token = $response->json('access_token');

$this->assertNotEmpty($token);

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_authenticated_user_can_retrieve_products_with_pagination()
$response->assertStatus(201);

// Retrieve the JWT token from the registration response
$token = $response->json('data.accessToken');
$token = $response->json('access_token');

$this->assertNotEmpty($token);

Expand Down Expand Up @@ -91,7 +91,7 @@ public function test_authenticated_user_receives_bad_request_for_invalid_paginat
$response->assertStatus(201);

// Retrieve the JWT token from the registration response
$token = $response->json('data.accessToken');
$token = $response->json('access_token');

$this->assertNotEmpty($token);

Expand Down
15 changes: 6 additions & 9 deletions tests/Unit/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ public function test_login_with_valid_credentials()

$response->assertStatus(200)
->assertJsonStructure([
'status_code',
'message',
'access_token',
'data' => [
'user' => [
'id',
'first_name',
'last_name',
'email',
'role',
'signup_type',
'is_active',
'is_verified',
'created_at',
'updated_at',
'avatar_url',
],
'access_token',
'refresh_token',
],
]);
}
Expand Down Expand Up @@ -76,7 +74,6 @@ public function test_login_with_missing_fields()
'email' => '[email protected]',
]);

$response->assertStatus(422)
->assertJsonValidationErrors(['password']);
$response->assertStatus(400);
}
}
24 changes: 13 additions & 11 deletions tests/Unit/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ public function test_registration_returns_jwt_token()
];

$response = $this->postJson('/api/v1/auth/register', $registrationData);

// Check the status code
$response->assertStatus(201);

// Check the response structure
$response->assertJsonStructure([
'message',
'status_code',
'message',
'access_token',
'data' => [
'accessToken',
'user' => [
'name',
'email',
'id',
'updated_at',
'created_at',
'first_name',
'last_name',
'email',
'avatar_url',
'role',
]
]
]);

// Optionally, decode and verify the token
$token = $response->json('data.accessToken');
$token = $response->json('access_token');
$this->assertNotEmpty($token);
}

Expand All @@ -66,14 +68,14 @@ public function test_fails_if_email_is_not_passed()

$response = $this->postJson('/api/v1/auth/register', $registrationData);
// Check the status code
$response->assertStatus(422);
$response->assertStatus(400);
$response->assertJson([
'status_code' => 400,
'message' => [
'email' => [
'The email field is required.'
]
],
'status_code' => 422,
]);
}

Expand Down Expand Up @@ -105,7 +107,7 @@ public function google_login_creates_or_updates_user_and_profile()
// Check for success response
$response->assertStatus(200)
->assertJson([
'status' => 'success',
'status_code' => 200,
'message' => 'User successfully authenticated',
]);

Expand Down Expand Up @@ -195,7 +197,7 @@ public function facebook_login_creates_or_updates_user_and_profile()
// Check for success response
$response->assertStatus(200)
->assertJson([
'status' => 'success',
'status_code' => 200,
'message' => 'User successfully authenticated',
]);

Expand Down

0 comments on commit 28d590f

Please sign in to comment.