-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(firebase): add google signin (#548)
- Loading branch information
1 parent
fc95250
commit ec8ba3a
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
packages/cerebral-provider-firebase/src/factories/signInWithGoogle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {convertObjectWithTemplates} from './utils' | ||
|
||
function signInWithGoogleFactory (options = {}) { | ||
function signInWithGoogle (context) { | ||
return context.firebase.signInWithGoogle(convertObjectWithTemplates(options, context)) | ||
.then(context.path.success) | ||
.catch(context.path.error) | ||
} | ||
|
||
return signInWithGoogle | ||
} | ||
|
||
export default signInWithGoogleFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/cerebral-provider-firebase/src/signInWithGoogle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import firebase from 'firebase' | ||
import {createUser} from './helpers' | ||
|
||
export default function signInWithGoogle (options) { | ||
const scopes = options.scopes || [] | ||
const redirect = options.redirect || false | ||
const provider = new firebase.auth.GoogleAuthProvider() | ||
|
||
scopes.forEach((scope) => { | ||
provider.addScope(scope) | ||
}) | ||
|
||
return new Promise((resolve, reject) => { | ||
if (redirect) { | ||
firebase.auth().signInWithRedirect(provider) | ||
resolve() | ||
} else { | ||
firebase.auth().signInWithPopup(provider).then((result) => { | ||
const user = createUser(result.user) | ||
|
||
user.accessToken = result.credential.accessToken | ||
resolve({ | ||
user: user | ||
}) | ||
}).catch((error) => { | ||
reject({ | ||
code: error.code, | ||
message: error.message, | ||
email: error.email | ||
}) | ||
}) | ||
} | ||
}) | ||
} |