From 1994958cf6c7fec08a4d2096a63aaad51dd749cd Mon Sep 17 00:00:00 2001 From: Ismail <msiamil.official@gmail.com> Date: Wed, 19 Aug 2020 21:52:42 +0500 Subject: [PATCH] root reducer created, auth action modified, firebase config modified! --- .../src/components/auth/SignIn.js | 14 +++++++- .../src/config/firebaseConfig.js | 2 +- employeemanagement/src/index.js | 35 ++++++++++++++++--- .../src/store/actions/authAction.js | 13 +++++-- .../src/store/reducers/authReducer.js | 19 +++++++--- .../src/store/reducers/rootReducer.js | 12 +++++++ 6 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 employeemanagement/src/store/reducers/rootReducer.js diff --git a/employeemanagement/src/components/auth/SignIn.js b/employeemanagement/src/components/auth/SignIn.js index 400a47e..4f5acfe 100644 --- a/employeemanagement/src/components/auth/SignIn.js +++ b/employeemanagement/src/components/auth/SignIn.js @@ -2,6 +2,7 @@ import React, { Component } from 'react'; import Logo from '../images/header-logo.png'; import { connect } from 'react-redux'; import { signIn } from '../../store/actions/authAction'; +import { Redirect } from 'react-router-dom'; class Signin extends Component { state = { @@ -20,6 +21,7 @@ class Signin extends Component { this.props.signIn(this.state); } render(){ + console.log(this.props); return( <div className="Login"> <div className="login-wrap"> @@ -36,16 +38,26 @@ class Signin extends Component { <input type="password" className="form-control" id="password" onChange={this.handleChange} placeholder="Enter password" name="pwd" /> </div> <button className="btn btn-primary">Submit</button> + </form> </div> </div> ) } } + +const mapStateToProps = (state) => { + console.log(state); + return { + //authError: state.auth.authError, + auth: state.firebase.auth + } +} + const mapDisToProps = (dispatch) => { return { signIn: (credentials) => dispatch(signIn(credentials)) } } -export default connect(null, mapDisToProps)(Signin); \ No newline at end of file +export default connect(mapStateToProps, mapDisToProps)(Signin); \ No newline at end of file diff --git a/employeemanagement/src/config/firebaseConfig.js b/employeemanagement/src/config/firebaseConfig.js index ea6d7b3..50bcf3e 100644 --- a/employeemanagement/src/config/firebaseConfig.js +++ b/employeemanagement/src/config/firebaseConfig.js @@ -15,6 +15,6 @@ var firebaseConfig = { }; // Initialize Firebase firebase.initializeApp(firebaseConfig); - firebase.analytics(); + //firebase.analytics(); export default firebase; \ No newline at end of file diff --git a/employeemanagement/src/index.js b/employeemanagement/src/index.js index 9c95988..de54e34 100644 --- a/employeemanagement/src/index.js +++ b/employeemanagement/src/index.js @@ -3,17 +3,42 @@ import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import * as serviceWorker from './serviceWorker'; -import { createStore, applyMiddleware } from 'redux'; -import authReducer from './store/reducers/authReducer'; +import { createStore, applyMiddleware, compose } from 'redux'; +import rootReducer from './store/reducers/authReducer'; import { Provider } from 'react-redux'; import thunk from 'redux-thunk'; +import { reduxFirestore, getFirestore, createFirestoreInstance } from 'redux-firestore'; +import { ReactReduxFirebaseProvider, getFirebase } from 'react-redux-firebase'; +import fbConfig from './config/firebaseConfig'; +import firebase from 'firebase/app'; -const store = createStore(authReducer, applyMiddleware(thunk)); +const store = createStore(rootReducer, + compose( + applyMiddleware(thunk.withExtraArgument({getFirebase, getFirestore})), + reduxFirestore(fbConfig) + )); + +const profileSpecificProps = { + userProfile: 'Employees', + useFirestoreForProfile: true, + enableRedirectHandling: false, + resetBeforeLogin: false +} + +const rrfProps = { + firebase, + config: fbConfig, + config: profileSpecificProps, + dispatch: store.dispatch, + createFirestoreInstance +}; ReactDOM.render( <React.StrictMode> - <Provider store = { store }> - <App /> + <Provider store = {store}> + <ReactReduxFirebaseProvider {...rrfProps}> + <App /> + </ReactReduxFirebaseProvider> </Provider> </React.StrictMode>, document.getElementById('root') diff --git a/employeemanagement/src/store/actions/authAction.js b/employeemanagement/src/store/actions/authAction.js index 04195f3..759d978 100644 --- a/employeemanagement/src/store/actions/authAction.js +++ b/employeemanagement/src/store/actions/authAction.js @@ -1,5 +1,14 @@ export const signIn = (credentials) => { - return (dispatch, getState) => { - dispatch({ type: 'LOGIN', credentials}) + return (dispatch, getState, { getFirebase}) => { + const firebase = getFirebase(); + + firebase.auth().signInWithEmailAndPassword( + credentials.email, + credentials.password + ).then(() => { + dispatch({ type: 'LOGIN_SUCCESS'}) + }).catch((err) => { + dispatch({ type: 'LOGIN_FAIL', err}) + }) } } \ No newline at end of file diff --git a/employeemanagement/src/store/reducers/authReducer.js b/employeemanagement/src/store/reducers/authReducer.js index 6b88a43..3994be2 100644 --- a/employeemanagement/src/store/reducers/authReducer.js +++ b/employeemanagement/src/store/reducers/authReducer.js @@ -1,12 +1,21 @@ -const initialState = {} +const initState = {} -const authReducer = (state = initialState, action) => { +const authReducer = (state = initState, action) => { switch (action.type) { - case 'LOGIN': - console.log('Login check', action.credentials); + case 'LOGIN_FAIL': + //console.log('Login error'); + return { + ...state, + authError: 'Login Failed' + } + case 'LOGIN_SUCCESS': + //console.log('Login success'); + return { + ...state, + authError: null + } default: return state; } - return state; } export default authReducer \ No newline at end of file diff --git a/employeemanagement/src/store/reducers/rootReducer.js b/employeemanagement/src/store/reducers/rootReducer.js new file mode 100644 index 0000000..3d7ca29 --- /dev/null +++ b/employeemanagement/src/store/reducers/rootReducer.js @@ -0,0 +1,12 @@ +import authReducer from './authReducer' +import { combineReducers } from 'redux' +import { firestoreReducer } from 'redux-firestore' +import { firebaseReducer } from 'react-redux-firebase' + +const rootReducer = combineReducers({ + auth: authReducer, + firestore: firestoreReducer, + firebase: firebaseReducer +}); + +export default rootReducer \ No newline at end of file -- GitLab