Skip to content

Commit

Permalink
feat(firebase): add google signin (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni authored and Guria committed Dec 11, 2016
1 parent fc95250 commit ec8ba3a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions packages/cerebral-provider-firebase/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {default as sendPasswordResetEmail} from './factories/sendPasswordResetEm
export {default as signInAnonymously} from './factories/signInAnonymously'
export {default as signInWithEmailAndPassword} from './factories/signInWithEmailAndPassword'
export {default as signInWithFacebook} from './factories/signInWithFacebook'
export {default as signInWithGoogle} from './factories/signInWithGoogle'
export {default as signOut} from './factories/signOut'
export {default as task} from './factories/task'
export {default as value} from './factories/value'
Expand Down
34 changes: 34 additions & 0 deletions packages/cerebral-provider-firebase/src/signInWithGoogle.js
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
})
})
}
})
}

0 comments on commit ec8ba3a

Please sign in to comment.