= 6 лет назад
Родитель
Сommit
6f2da8bea2

+ 10 - 1
src/actions/auth/signIn/index.js

@@ -14,4 +14,13 @@ export const signInRequestSucces = payload => ({
 export const signInRequestFailure = error => ({
     type: types.SIGN_IN_REQUEST_FAILURE,
     error
-})
+})
+
+export const userIsSignedIn = (payload) => ({
+    type: types.USER_IS_SIGNED_IN,
+    payload
+})
+
+export const userIsNotSignedIn = () => ({
+    type: types.USER_IS_NOT_SIGNED_IN
+})

+ 4 - 1
src/constants/index.js

@@ -6,4 +6,7 @@ export const SIGN_IN_REQUEST_FAILURE = 'SIGN_IN_REQUEST_FAILURE';
 export const SIGN_UP_URL = 'https://projectbasetest.firebaseio.com/users.json';
 export const SIGN_UP_REQUEST = 'SIGN_UP_REQUEST';
 export const SIGN_UP_REQUEST_SUCCESS = 'SIGN_UP_REQUEST_SUCCESS';
-export const SIGN_UP_REQUEST_FAILURE = 'SIGN_UP_REQUEST_FAILURE';
+export const SIGN_UP_REQUEST_FAILURE = 'SIGN_UP_REQUEST_FAILURE';
+
+export const USER_IS_SIGNED_IN = 'USER_IS_SIGNED_IN';
+export const USER_IS_NOT_SIGNED_IN = 'USER_IS_NOT_SIGNED_IN';

+ 10 - 29
src/containers/AuthPage/index.js

@@ -5,42 +5,23 @@ import { bindActionCreators } from 'redux';
 import { signInRequest } from './../../actions/auth/signIn'
 import { signUpRequest } from './../../actions/auth/signUp'
 
-import { auth, googleAuthProvider } from '../../firebase_config'
+import { auth } from '../../firebase_config'
 
 class AuthPage extends Component {
     constructor(props) {
         super(props);
-        this.state = {
-            user: null
-        }
-    }
-
-    componentDidMount() {
-        auth.onAuthStateChanged(user => this.setState({ user }))
-    }
-
-    authRequest = () => {
-        auth.signInWithPopup(googleAuthProvider)
-            .then(res => {
-                console.log(res);
-
-                this.setState({
-                    user: res.user
-                })
-            })
     }
 
     render() {
-        const { } = this.props; // 
-        const { user } = this.state; // 
+        const { isFetching, error, user } = this.props.auth;
+        const { signInRequest } = this.props; 
 
         console.log("<AuthPage> - props", this.props);
 
-
         return (
             <div className="auth-page">
-                <h1>{user && user.displayName}</h1>
-                <button onClick={this.authRequest} disabled={user ? true : false}>Auth</button>
+                <button onClick={signInRequest} disabled={user ? true : false}>Auth</button>
+                <h2>{isFetching ? 'wait a moment...' : error ? 'Looks like we\'ve in trouble...' : user && user.displayName}</h2>
             </div>
         )
     }
@@ -48,13 +29,13 @@ class AuthPage extends Component {
 
 const
     mapStateToProps = state => ({
-        signInState: {
+        auth: {
             ...state.signIn
-        },
-        signUpState: {
-            ...state.signUp
         }
     }),
-    mapDispatchToProps = dispatch => bindActionCreators({ signInRequest, signUpRequest }, dispatch);
+    mapDispatchToProps = dispatch => bindActionCreators({ 
+        signInRequest,
+        signUpRequest 
+    }, dispatch);
 
 export default connect(mapStateToProps, mapDispatchToProps)(AuthPage)

+ 6 - 6
src/firebase_config.js

@@ -2,12 +2,12 @@ import firebase from "firebase"
 
 // Initialize Firebase
 var config = {
-    apiKey: "AIzaSyB8VX9OPxTh6CWIZW5JmK5XZJPVk46w5Lk",
-    authDomain: "someshit-7a282.firebaseapp.com",
-    databaseURL: "https://someshit-7a282.firebaseio.com",
-    projectId: "someshit-7a282",
-    storageBucket: "someshit-7a282.appspot.com",
-    messagingSenderId: "76495252861"
+    apiKey: "AIzaSyBtE3XNH2Hbj-2LiGrmNOGLcxVC-YbmIqU",
+    authDomain: "projectbasetest.firebaseapp.com",
+    databaseURL: "https://projectbasetest.firebaseio.com",
+    projectId: "projectbasetest",
+    storageBucket: "projectbasetest.appspot.com",
+    messagingSenderId: "706084402090"
 };
 
 firebase.initializeApp(config);

+ 15 - 8
src/reducers/auth/signIn/index.js

@@ -1,12 +1,7 @@
 import * as types from "../../../constants";
+import initialState from './../../initialState';
 
-const initialState = {
-    isFetching: false,
-    payload: null,
-    error: null
-}
-
-export default function(state = initialState, {type, payload, error}) {
+export default function signIn(state = initialState.signIn, {type, payload: user, error}) {
     switch (type) {
         case types.SIGN_IN_REQUEST: {
             return {
@@ -17,7 +12,7 @@ export default function(state = initialState, {type, payload, error}) {
         case types.SIGN_IN_REQUEST_SUCCESS: {
             return {
                 ...state,
-                payload,
+                user,
                 isFetching: false
             }
         }
@@ -28,6 +23,18 @@ export default function(state = initialState, {type, payload, error}) {
                 isFetching: false
             }
         }
+        case types.USER_IS_SIGNED_IN: {
+            return {
+                ...state,
+                user
+            }
+        }
+        case types.USER_IS_NOT_SIGNED_IN: {
+            return {
+                ...state,
+                user: null
+            }
+        }
         default: {
             return state
         }

+ 2 - 7
src/reducers/auth/signUp/index.js

@@ -1,12 +1,7 @@
 import * as types from "../../../constants";
+import initialState from './../../initialState'
 
-const initialState = {
-    isFetching: false,
-    payload: null,
-    error: null
-}
-
-export default function(state = initialState, {type, payload, error}) {
+export default function signUp(state = initialState.signUp, {type, payload, error}) {
     switch (type) {
         case types.SIGN_UP_REQUEST: {
             return {

+ 1 - 1
src/reducers/index.js

@@ -3,7 +3,7 @@
 
 import { combineReducers } from 'redux';
 import signIn from './auth/signIn';
-import signUp from './auth/signUp'
+import signUp from './auth/signUp';
 
 const combinedReducers = combineReducers({
     signIn,

+ 7 - 0
src/reducers/initialState/index.js

@@ -0,0 +1,7 @@
+export default {
+    signIn: {
+        isFetching: false,
+        user: null
+    },
+    signUp: {},
+}

+ 8 - 5
src/saga/auth/signIn/index.js

@@ -1,16 +1,19 @@
 import { put, call } from "redux-saga/effects";
 import * as actions from './../../../actions/auth/signIn'
-
-import { SIGN_IN_URL } from '../../../constants'
+import { auth, googleAuthProvider } from './../../../firebase_config'
 
 // worker-saga for signing in
 
 export default function* () {
     try {
         const payload = yield call(() => {
-            fetch(SIGN_IN_URL).then(res => res.json())
-        })
-        yield put(actions.signInRequestSucces(payload))
+            return (
+                auth.signInWithPopup(googleAuthProvider)
+                    .then(res => res.user)
+            )
+        });
+        console.log("Payload", payload);
+        yield put(actions.signInRequestSucces(payload));
     }
     catch (exception) {
         yield put(actions.signInRequestFailure(exception.message))

+ 2 - 4
src/saga/auth/signUp/index.js

@@ -7,10 +7,8 @@ import { SIGN_UP_URL } from '../../../constants'
 
 export default function* () {
     try {
-        const payload = yield call(() => {
-            fetch(SIGN_UP_URL).then(res => res.json())
-        })
-        yield put(actions.signUpRequestSucces(payload))
+        const payload = yield call( () => fetch(SIGN_UP_URL).then(res => res.json()) );
+        yield put(actions.signUpRequestSucces(payload));
     }
     catch (exception) {
         yield put(actions.signUpRequestFailure(exception.message))

+ 15 - 3
src/state/index.js

@@ -3,13 +3,25 @@ import { logger } from 'redux-logger'
 import createSagaMiddleware from "redux-saga";
 
 import combinedReducers from './../reducers';
+import initialState from './../reducers/initialState'
 import saga from './../saga'
+import { auth } from './../firebase_config'
+import { userIsNotSignedIn, userIsSignedIn } from './../actions/auth/signIn'
 
 const sagaMiddleware = createSagaMiddleware();
 
-export default createStore(
-    combinedReducers, 
+const store = createStore(
+    combinedReducers,
+    initialState,
     applyMiddleware(sagaMiddleware, logger)
 );
 
-sagaMiddleware.run(saga);
+// action
+auth.onAuthStateChanged(user => user
+    ? store.dispatch(userIsSignedIn(user)) 
+    : store.dispatch(userIsNotSignedIn())
+);
+
+export default store;
+
+sagaMiddleware.run(saga);