From 93b32e901e2887793b68a92959f055cb64dce740 Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Fri, 24 Apr 2020 20:25:26 +0200 Subject: [PATCH] adds sessionSlice; adds sessionReducer to store; use redux in MainNavBar; --- client/src/components/MainNavbar/index.jsx | 14 ++++- client/src/services/auth/firebase.js | 17 ++++-- client/src/services/auth/index.js | 4 +- client/src/store/index.js | 2 + client/src/store/sessionSlice.js | 63 ++++++++++++++++++++++ 5 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 client/src/store/sessionSlice.js diff --git a/client/src/components/MainNavbar/index.jsx b/client/src/components/MainNavbar/index.jsx index 57068cc..8626803 100644 --- a/client/src/components/MainNavbar/index.jsx +++ b/client/src/components/MainNavbar/index.jsx @@ -1,5 +1,6 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { Link } from "react-router-dom"; +import { useSelector, useDispatch } from "react-redux"; import PropTypes from "prop-types"; import { Collapse, @@ -13,11 +14,20 @@ import { } from "reactstrap"; import SignOutButton from "../SignOutButton"; import * as ROUTES from "../../constants/routes"; +import { selectLoggedIn, getAuthUserAsync } from "../../store/sessionSlice"; -const MainNavbar = ({ authUser }) => { +//{ authUser } +const MainNavbar = () => { const [isOpen, setIsOpen] = useState(false); const toggle = () => setIsOpen(!isOpen); + const authUser = useSelector(selectLoggedIn); + const dispatch = useDispatch(); + + useEffect(() => { + dispatch(getAuthUserAsync()); + }, [dispatch]); + return ( diff --git a/client/src/services/auth/firebase.js b/client/src/services/auth/firebase.js index e5a598c..b949ad6 100644 --- a/client/src/services/auth/firebase.js +++ b/client/src/services/auth/firebase.js @@ -1,4 +1,4 @@ -import app from "firebase/app"; +import * as firebase from "firebase/app"; // import "firebase/firestore"; import "firebase/auth"; import config from "./config.json"; @@ -18,11 +18,11 @@ const CONFIG = { // Firebase services as auth and firestore. export default class Firebase { constructor() { - app.initializeApp(CONFIG); - this.auth = app.auth(); + // firebase.initializeApp(CONFIG); + this.auth = firebase.auth(); } - provider = new app.auth.GoogleAuthProvider(); + provider = new firebase.auth.GoogleAuthProvider(); signInWithGoogle = () => this.auth @@ -40,4 +40,13 @@ export default class Firebase { resetPassword = (email) => this.auth.sendPasswordResetEmail(email); updatePassword = (password) => this.auth.currentUser.updatePassword(password); + + // getAuthState = async () => + // this.auth.onAuthStateChanged((authUser) => + // authUser ? setAuthUser(authUser) : setAuthUser(null) + // ); } + +firebase.initializeApp(CONFIG); + +export { firebase }; diff --git a/client/src/services/auth/index.js b/client/src/services/auth/index.js index 1182665..da72021 100644 --- a/client/src/services/auth/index.js +++ b/client/src/services/auth/index.js @@ -1,6 +1,6 @@ // This file centralize all Firebase related exports -import Firebase from "./firebase"; +import Firebase, { firebase } from "./firebase"; import FirebaseContext, { useFirebase } from "./context"; export default Firebase; -export { FirebaseContext, useFirebase }; +export { FirebaseContext, useFirebase, firebase }; diff --git a/client/src/store/index.js b/client/src/store/index.js index c5cd012..2ae458f 100644 --- a/client/src/store/index.js +++ b/client/src/store/index.js @@ -1,8 +1,10 @@ import { configureStore } from "@reduxjs/toolkit"; import itemReducer from "./itemSlice"; +import sessionReducer from "./sessionSlice"; export default configureStore({ reducer: { item: itemReducer, + session: sessionReducer, }, }); diff --git a/client/src/store/sessionSlice.js b/client/src/store/sessionSlice.js new file mode 100644 index 0000000..92e4480 --- /dev/null +++ b/client/src/store/sessionSlice.js @@ -0,0 +1,63 @@ +import { createSlice } from "@reduxjs/toolkit"; +import { firebase } from "../services/auth"; + +export const sessionSlice = createSlice({ + name: "session", + initialState: { + username: "", + email: "", + photoURL: "", + phoneNumber: "", + roles: {}, + token: "", + loggedIn: false, + error: null, + }, + reducers: { + getAuthUser: (state, action) => ({ + ...state, + ...action.payload, + }), + addAuthUser: (state, action) => {}, + getToken: (state, action) => ({ + ...state, + token: action.payload, + }), + logIn: (state) => ({ + ...state, + loggedIn: true, + }), + logOut: (state) => ({ + ...state, + loggedIn: false, + }), + }, +}); + +/** + * Use Auth provider to get credentials + */ +export const getAuthUserAsync = () => async (dispatch) => { + firebase.auth().onAuthStateChanged((user) => { + if (user) { + const newUser = { + username: user.displayName, + email: user.email, + photoURL: user.photoURL, + phoneNumber: user.phoneNumber, + loggedIn: true, + error: null, + }; + dispatch(getAuthUser(newUser)); + } else { + dispatch(logOut); + } + }); +}; + +export const { getAuthUser, getToken, logIn, logOut } = sessionSlice.actions; + +export const selectAuthUser = (state) => state.session; +export const selectLoggedIn = (state) => state.session.loggedIn; + +export default sessionSlice.reducer;