Skip to content

Commit

Permalink
Merge pull request #467 from tulbadex/fix/google-auth-reamend
Browse files Browse the repository at this point in the history
Fix: google auth reamend
  • Loading branch information
Dev-Tonia authored Aug 9, 2024
2 parents a83a18a + ce1a8fe commit dd470aa
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 28 deletions.
63 changes: 36 additions & 27 deletions app/Http/Controllers/Api/V1/Auth/SocialAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Google_Client;

class SocialAuthController extends Controller
{
Expand Down Expand Up @@ -127,42 +128,49 @@ public function saveGoogleRequestPost(Request $request)
}

// Extract Google user data from the request
$google_token = $request->id_token;
$idToken = $request->id_token;

try {
// Retrieve user information from Google
$googleUser = Socialite::driver('google')->userFromToken($google_token);

// Create or update the user
$client = new Google_Client(['client_id' => env('GOOGLE_CLIENT_ID')]); // Replace with your Google Client ID
$payload = $client->verifyIdToken($idToken);

if ($payload) {
// Token is valid
$email = $payload['email'];
$firstName = $payload['given_name'];
$lastName = $payload['family_name'];
$avatarUrl = $payload['picture'] ?? null;

// Create or update user
$user = User::updateOrCreate(
['email' => $googleUser->email],
['email' => $email],
[
'password' => Hash::make(Str::random(12)), // Random password for social sign-ins
'social_id' => $googleUser->id,
'password' => Hash::make(Str::random(12)), // Generate a random password for the user
'social_id' => $idToken,
'is_verified' => true,
'signup_type' => 'Google',
'is_active' => true,
'role' => 'user',
]
);
// Handle profile update or creation

// Update or create user profile
if ($user->profile) {
$user->profile->update([
'first_name' => $googleUser->user['given_name'],
'last_name' => $googleUser->user['family_name'],
'avatar_url' => $googleUser->user['picture'],
'first_name' => $firstName,
'last_name' => $lastName,
'avatar_url' => $avatarUrl,
]);
} else {
$user->profile()->create([
'first_name' => $googleUser->user['given_name'],
'last_name' => $googleUser->user['family_name'],
'avatar_url' => $googleUser->user['picture'],
'first_name' => $firstName,
'last_name' => $lastName,
'avatar_url' => $avatarUrl,
]);
}

// Generate JWT token
$token = JWTAuth::fromUser($user);

return response()->json([
'status_code' => 200,
'message' => 'User Created Successfully',
Expand All @@ -171,18 +179,19 @@ public function saveGoogleRequestPost(Request $request)
'user' => [
'id' => $user->id,
'email' => $user->email,
'first_name' => $googleUser->user['given_name'],
'last_name' => $googleUser->user['family_name'],
'fullname' => $googleUser->user['given_name'].' '.$googleUser->user['family_name'],
'first_name' => $firstName,
'last_name' => $lastName,
'fullname' => $firstName.' '.$lastName,
'role' => $user->role,
]
]
], 200);
} catch (Exception $e) {
]);
} else {
// Invalid token
return response()->json([
'status_code' => 500,
'message' => $e->getMessage()
], 500);
'status_code' => 401,
'message' => 'Invalid Token'
], 401);
}
}

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"php": "^8.1",
"ext-zip": "*",
"doctrine/dbal": "^3.8",
"google/apiclient": "^2.17",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
Expand Down
175 changes: 174 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd470aa

Please sign in to comment.