Skip to content

Commit

Permalink
feat: send cookie with role when logging by provider and send only ro…
Browse files Browse the repository at this point in the history
…le name to frontend
  • Loading branch information
Finnick223 committed Feb 3, 2025
1 parent 8df35df commit 45bc4d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/GoogleAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public function handleGoogleCallback()
],
);

$roles = $authUser->roles->pluck('name')->toArray();
$token = $authUser->createToken('authToken')->plainTextToken;

$frontendUrl = config('app.frontend_url');

EncryptCookies::except('auth_token');
EncryptCookies::except('roles');

return redirect("{$frontendUrl}/api/login/google")->withCookie(cookie('auth_token', $token, 1, '/', null, false, true));
return redirect("{$frontendUrl}/api/login/google")->withCookie(cookie('auth_token', $token, 0, '/', null, false, true))->withCookie(cookie('roles', json_encode($roles, JSON_UNESCAPED_SLASHES), 0, '/', null, false, true));

} catch (Exception $e) {
return response()->json([
Expand Down
16 changes: 15 additions & 1 deletion app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ class UserResource extends JsonResource
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
'last_name' => $this->last_name,
'email' => $this->email,
'phone_number' => $this->phone_number,
'voivodship' => $this->voivodship,
'city' => $this->city,
'zip_code' => $this->zip_code,
'street' => $this->street,
'house_number' => $this->house_number,
'roles' => $this->roles->pluck('name'), // Extracts only role names
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}

0 comments on commit 45bc4d2

Please sign in to comment.