Created
May 4, 2017 13:10
-
-
Save dataday/c24a426adfcfec2f465d704429acd28c to your computer and use it in GitHub Desktop.
Firebase PaaS - Auth Actions (storage and authentication)
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
/** | |
* # auth-actions.js | |
* | |
* Auth Actions | |
* | |
* https://firebase.google.com/docs/auth/web/password-auth | |
* | |
* @flow | |
*/ | |
'use strict' | |
/* eslint new-cap: ["error", { "newIsCapExceptionPattern": "^Firebase\.." }] */ | |
/** | |
* ## Imports | |
* | |
* Necessary components | |
*/ | |
/** | |
* ### Firebase | |
* | |
* ```Firebase``` connects PaaS ```storage``` and ```authentication``` | |
* | |
*/ | |
import Firebase from 'firebase' | |
import _ from 'lodash' | |
import { ACTIONS, NAV } from '../../constants' | |
import * as NavActions from '../../modules/navigation/navigation-actions' | |
import * as DataActions from '../data/data-actions' | |
const SIGN_IN_MODAL = NAV.MODALS.MODAL_AUTH | |
const PASSWORD_RESET_MODAL = NAV.MODALS.MODAL_AUTH_THREE | |
// Only expose a subset of properties | |
const selectedAuthKeys = [ | |
'uid', | |
'email', | |
'emailVerified', | |
'displayName', | |
'photoURL' | |
] | |
export const setOnboard = (onboard: string): Object => ({ | |
type: ACTIONS.AUTH_ONBOARD_STATE_CHANGE, | |
payload: { onboard } | |
}) | |
const signInAction = (): Object => ({ | |
type: ACTIONS.AUTH_SIGN_IN, | |
payload: { isInit: true } | |
}) | |
const signInActionRejected = (error: Object): Object => ({ | |
type: ACTIONS.AUTH_SIGN_IN_FAILED, | |
payload: { error } | |
}) | |
const signInActionResolved = (user: Object): Object => ({ | |
type: ACTIONS.AUTH_SIGN_IN_FULFILLED, | |
payload: { isActive: true, user } | |
}) | |
const signOutAction = (): Object => ({ | |
type: ACTIONS.AUTH_SIGN_OUT, | |
payload: { isInit: true } | |
}) | |
const signOutActionRejected = (error: Object): Object => ({ | |
type: ACTIONS.AUTH_SIGN_OUT_FAILED, | |
payload: { error } | |
}) | |
const signOutActionResolved = (): Object => ({ | |
type: ACTIONS.AUTH_SIGN_OUT_FULFILLED, | |
payload: { isActive: false } | |
}) | |
const signUpAction = (): Object => ({ | |
type: ACTIONS.AUTH_SIGN_UP, | |
payload: { isInit: true } | |
}) | |
const signUpActionResolved = (user: Object): Object => ({ | |
type: ACTIONS.AUTH_SIGN_UP_FULFILLED, | |
payload: { isActive: true, user } | |
}) | |
const signUpActionRejected = (error: Object): Object => ({ | |
type: ACTIONS.AUTH_SIGN_UP_FAILED, | |
payload: { error } | |
}) | |
const passwordResetAction = (): Object => ({ | |
type: ACTIONS.AUTH_PASSWORD_RESET, | |
payload: { isInit: true } | |
}) | |
const passwordResetActionResolved = (user: Object): Object => ({ | |
type: ACTIONS.AUTH_PASSWORD_RESET_FULFILLED, | |
payload: { isActive: false, user } | |
}) | |
const passwordResetActionRejected = (error: Object): Object => ({ | |
type: ACTIONS.AUTH_PASSWORD_RESET_FAILED, | |
payload: { error } | |
}) | |
export function initialise () { | |
const { onNavigate, onData } = this.props | |
onNavigate(NavActions.toggleOverlay()) | |
return (dispatch: Function): Function => { | |
const firebase = new Promise((resolve, reject) => { | |
return Firebase.auth().onAuthStateChanged(auth => { | |
const obj = _.pick(auth, selectedAuthKeys) | |
return _.isEmpty(obj) ? reject(obj) : resolve(obj) | |
}) | |
}) | |
firebase.then( | |
// Succeded | |
auth => { | |
dispatch(signInActionResolved(auth)) | |
onData(DataActions.getInitialData.call(this, auth.uid)) | |
}, | |
// Failed | |
error => { | |
dispatch(signInActionRejected(error)) | |
onNavigate(NavActions.toggleModal(SIGN_IN_MODAL)) | |
} | |
) | |
return dispatch(signInAction()) | |
} | |
} | |
export function signIn (email: string, password: string) { | |
const { onNavigate } = this.props | |
return (dispatch: Function): Function => { | |
const firebase = new Promise((resolve, reject) => { | |
return Firebase.auth().signInWithEmailAndPassword(email, password) | |
.then(auth => resolve(auth)) | |
.catch(error => reject(error)) | |
}) | |
firebase.then( | |
// Succeded | |
auth => { | |
dispatch(signInActionResolved(_.pick(auth, selectedAuthKeys))) | |
onNavigate(NavActions.toggleOverlay()) | |
this.onSuccess(auth) | |
}, | |
// Failed | |
error => { | |
dispatch(signInActionRejected(error)) | |
this.onError(error) | |
} | |
) | |
return dispatch(signInAction()) | |
} | |
} | |
export function signOut () { | |
const { onNavigate } = this.props | |
return (dispatch: Function): Function => { | |
const firebase = new Promise((resolve, reject) => { | |
return Firebase.auth().signOut() | |
.then(() => resolve()) | |
.catch(error => reject(error)) | |
}) | |
firebase.then( | |
// Succeded | |
() => { | |
dispatch(signOutActionResolved()) | |
onNavigate(NavActions.toggleOverlay()) | |
onNavigate(NavActions.toggleModal(SIGN_IN_MODAL)) | |
}, | |
// Failed | |
error => dispatch(signOutActionRejected(error)) | |
) | |
return dispatch(signOutAction()) | |
} | |
} | |
export function signUp (displayName: string, email: string, password: string) { | |
const { onNavigate } = this.props | |
return (dispatch: Function): Function => { | |
const firebase = new Promise((resolve, reject) => { | |
return Firebase.auth().createUserWithEmailAndPassword(email, password) | |
.then(auth => resolve(auth)) | |
.catch(error => reject(error)) | |
}) | |
firebase.then( | |
// Succeded | |
auth => { | |
auth.updateProfile({ displayName }).then( | |
update => { | |
dispatch(signUpActionResolved(_.pick(auth, selectedAuthKeys))) | |
auth.sendEmailVerification() | |
onNavigate(NavActions.toggleOverlay()) | |
this.onSuccess(auth) | |
}, | |
error => { | |
dispatch(signUpActionRejected(error)) | |
this.onError(error) | |
} | |
) | |
}, | |
// Failed | |
error => { | |
dispatch(signUpActionRejected(error)) | |
this.onError(error) | |
} | |
) | |
return dispatch(signUpAction()) | |
} | |
} | |
export function passwordReset (email: string) { | |
const { onNavigate } = this.props | |
return (dispatch: Function): Function => { | |
const firebase = new Promise((resolve, reject) => { | |
return Firebase.auth().sendPasswordResetEmail(email) | |
.then(auth => resolve(auth)) | |
.catch(error => reject(error)) | |
}) | |
firebase.then( | |
// Succeded | |
auth => { | |
dispatch(passwordResetActionResolved(auth)) | |
onNavigate(NavActions.toggleModal(PASSWORD_RESET_MODAL)) | |
this.onSuccess(auth) | |
}, | |
// Failed | |
error => { | |
dispatch(passwordResetActionRejected(error)) | |
this.onError(error) | |
} | |
) | |
return dispatch(passwordResetAction()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment