Guest User

Untitled

a guest
Jan 25th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import React from 'react';
  2. import firebase from 'firebase';
  3.  
  4. const FACEBOOK = 'facebook';
  5. const GOOGLE = 'google';
  6.  
  7. // just to make sure that you'll always return a valid prodiver.
  8. function getProviderObject(provider) {
  9. return {
  10. [FACEBOOK]: new firebase.auth.FacebookAuthProvider(),
  11. [GOOGLE]: new firebase.auth.GoogleAuthProvider()
  12. }[provider] || null;
  13. }
  14.  
  15. // this function receives a provider object and return a User, no matter what
  16. // provider, Google or Facebook
  17. function signInWithProvider(provider) {
  18. const providerObject = getProviderObject(provider);
  19.  
  20. if(!providerObject) {
  21. throw new Error('Invalid provider');
  22. }
  23.  
  24. try {
  25. const response = await FirebaseApp.auth().signInWithPopup(provider);
  26.  
  27. return response.user;
  28. } catch(error) {
  29. // handle error...
  30. throw error;
  31. }
  32. }
  33.  
  34. function signInWithFirebase({ email, password }) {
  35. return FirebaseApp.auth().signInAndRetrieveDataWithEmailAndPassword(email, password);
  36. }
  37.  
  38.  
  39. // ##########################################################################
  40. // ACTIONS ##################################################################
  41. // ##########################################################################
  42.  
  43. // this action acts like a router, which will decide to sign in with an
  44. // external provider (GOOGLE or FACEBOOK) or sign in with email/password
  45. const signIn = param => async dispatch => {
  46. try {
  47. const user = await (
  48. [GOOGLE, FACEBOOK].includes(param) ?
  49. signInWithProvider(param) :
  50. signInWithFirebase(param)
  51. );
  52.  
  53. dispatch({
  54. type: 'CREATE_SESSION',
  55. payload: user
  56. });
  57.  
  58. return user;
  59. } catch(error) {
  60. console.log(error);
  61. }
  62. };
  63.  
  64. // ##########################################################################
  65. // REDUCERS #################################################################
  66. // ##########################################################################
  67.  
  68. const initialState = {
  69. authenticated: false,
  70. user: null,
  71. // etc...
  72. };
  73.  
  74. function sessionReducer(state = initialState, action = {}) {
  75. switch(action.type) {
  76. case 'CREATE_SESSION': return {
  77. ...action.payload,
  78. authenticated: true,
  79. // whatever props you need here...
  80. };
  81.  
  82. case 'DESTROY_SESSION': return initialState;
  83.  
  84. default: return state;
  85. }
  86. }
  87.  
  88. // ##########################################################################
  89. // EXAMPLE ##################################################################
  90. // ##########################################################################
  91.  
  92. class Auth extends React.Component {
  93. state = {
  94. email: null,
  95. password: null
  96. };
  97.  
  98. render() {
  99. const { email, password } = this.state;
  100.  
  101. return (
  102. <div>
  103. <p><a href="#" onClick={this.signInWith(FACEBOOK)}>SIGN IN WITH FACEBOOK</a></p>
  104. <p><a href="#" onClick={this.signInWith(GOOGLE)}>SIGN IN WITH GOOGLE</a></p>
  105.  
  106. <form onSubmit={this.onSubmit}>
  107. <input
  108. type="email"
  109. onChange={this.onChange('email')}
  110. value={email} placeholder="Email"
  111. />
  112.  
  113. <input
  114. type="password"
  115. onChange={this.onChange('email')}
  116. value={password}
  117. placeholder="Password"
  118. />
  119.  
  120. <button>Sign In</button>
  121. </form>
  122. </div>
  123. )
  124. }
  125.  
  126. signInWith = provider => event => {
  127. event.preventDefault();
  128. this.props.signIn(provider);
  129. }
  130.  
  131. onChange = field => value => this.setState({ [field]: value });
  132.  
  133. onSubmit = () => {
  134. const { email, password } = this.state;
  135. return this.props.signIn({ email, password });
  136. };
  137. }
  138.  
  139. export default connect(null, { signIn })(Auth);
Add Comment
Please, Sign In to add comment