Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import firebase from 'firebase';
- const FACEBOOK = 'facebook';
- const GOOGLE = 'google';
- // just to make sure that you'll always return a valid prodiver.
- function getProviderObject(provider) {
- return {
- [FACEBOOK]: new firebase.auth.FacebookAuthProvider(),
- [GOOGLE]: new firebase.auth.GoogleAuthProvider()
- }[provider] || null;
- }
- // this function receives a provider object and return a User, no matter what
- // provider, Google or Facebook
- function signInWithProvider(provider) {
- const providerObject = getProviderObject(provider);
- if(!providerObject) {
- throw new Error('Invalid provider');
- }
- try {
- const response = await FirebaseApp.auth().signInWithPopup(provider);
- return response.user;
- } catch(error) {
- // handle error...
- throw error;
- }
- }
- function signInWithFirebase({ email, password }) {
- return FirebaseApp.auth().signInAndRetrieveDataWithEmailAndPassword(email, password);
- }
- // ##########################################################################
- // ACTIONS ##################################################################
- // ##########################################################################
- // this action acts like a router, which will decide to sign in with an
- // external provider (GOOGLE or FACEBOOK) or sign in with email/password
- const signIn = param => async dispatch => {
- try {
- const user = await (
- [GOOGLE, FACEBOOK].includes(param) ?
- signInWithProvider(param) :
- signInWithFirebase(param)
- );
- dispatch({
- type: 'CREATE_SESSION',
- payload: user
- });
- return user;
- } catch(error) {
- console.log(error);
- }
- };
- // ##########################################################################
- // REDUCERS #################################################################
- // ##########################################################################
- const initialState = {
- authenticated: false,
- user: null,
- // etc...
- };
- function sessionReducer(state = initialState, action = {}) {
- switch(action.type) {
- case 'CREATE_SESSION': return {
- ...action.payload,
- authenticated: true,
- // whatever props you need here...
- };
- case 'DESTROY_SESSION': return initialState;
- default: return state;
- }
- }
- // ##########################################################################
- // EXAMPLE ##################################################################
- // ##########################################################################
- class Auth extends React.Component {
- state = {
- email: null,
- password: null
- };
- render() {
- const { email, password } = this.state;
- return (
- <div>
- <p><a href="#" onClick={this.signInWith(FACEBOOK)}>SIGN IN WITH FACEBOOK</a></p>
- <p><a href="#" onClick={this.signInWith(GOOGLE)}>SIGN IN WITH GOOGLE</a></p>
- <form onSubmit={this.onSubmit}>
- <input
- type="email"
- onChange={this.onChange('email')}
- value={email} placeholder="Email"
- />
- <input
- type="password"
- onChange={this.onChange('email')}
- value={password}
- placeholder="Password"
- />
- <button>Sign In</button>
- </form>
- </div>
- )
- }
- signInWith = provider => event => {
- event.preventDefault();
- this.props.signIn(provider);
- }
- onChange = field => value => this.setState({ [field]: value });
- onSubmit = () => {
- const { email, password } = this.state;
- return this.props.signIn({ email, password });
- };
- }
- export default connect(null, { signIn })(Auth);
Add Comment
Please, Sign In to add comment