kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
995 stars 161 forks source link

Is there a way to create a User with Google Signin? #20

Closed zerubabbel closed 4 years ago

zerubabbel commented 4 years ago

Hello,

I have an existing Laravel API that currently uses standard JWT to authenticate users. I'm trying to add Google and Twitter SignIn functionality using this Firebase library, and I'm having trouble understanding the methods which I would use to create a user using third party signin. I am looking at the linkProviderThroughAccessToken in the Auth.php file, but I am still not understanding how I would initially create a user using the third party providers. Any help would be greatly appreciated!

jeromegamez commented 4 years ago

In theory it should work like this:

$result = $auth->linkProviderThroughAccessToken('twitter', $twitterAccessTokenString)`;
$userRecord = $result->userRecord;

$result = $auth->linkProviderThroughAccessToken('google', $googleAccessTokenString)`;
$userRecord = $result->userRecord;

When sent to the Firebase API, the access token will be parsed by them - if a matching user already exists (I don't know for sure how users would be matched), the credentials will be linked to the existing user. If no matching user exists, a new one will be created. Either way, you should receive a user record and consider the user signed in.

The method is poorly named and undocumented because I added it in a weak moment when someone needed it, but I'm not able to reliably test it, and it's a functionality that would better be implemented in the frontend of an application.

The flow that I would suggest in a web application would be to let the user sign in to Firebase in the browser (e.g. with https://firebase.google.com/docs/auth/web/firebaseui). There, you can get an ID token for the currently signed-in user as described in https://firebase.google.com/docs/auth/admin/verify-id-tokens#retrieve_id_tokens_on_clients

This ID token can then be sent to your Laravel App and be verified by the SDK with

$auth = app('firebase.auth');
$token = $auth->verifyIdToken($receivedIdTokenString);
$userRecord = $auth->getUser($token->getClaim('sub'));
zerubabbel commented 4 years ago

Great, thanks so much.