mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-11 11:06:40 +00:00
use redux to obtain token on List and ItemModal
This commit is contained in:
parent
4174ad81fd
commit
cb72af1b37
5 changed files with 51 additions and 48 deletions
|
|
@ -1,20 +1,9 @@
|
||||||
import React, { useState, useEffect } from "react";
|
import React from "react";
|
||||||
import { BrowserRouter as Router } from "react-router-dom";
|
import { BrowserRouter as Router } from "react-router-dom";
|
||||||
import { useFirebase } from "../../services/auth";
|
|
||||||
import MainNavbar from "../MainNavbar";
|
import MainNavbar from "../MainNavbar";
|
||||||
import MainRouter from "../../routes";
|
import MainRouter from "../../routes";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
// for testing only. Transfer Session to store.
|
|
||||||
// const [authUser, setAuthUser] = useState(null);
|
|
||||||
// const firebase = useFirebase();
|
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// firebase.auth.onAuthStateChanged((authUser) =>
|
|
||||||
// authUser ? setAuthUser(authUser) : setAuthUser(null)
|
|
||||||
// );
|
|
||||||
// }, [firebase.auth]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
<MainNavbar />
|
<MainNavbar />
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Modal,
|
Modal,
|
||||||
|
|
@ -11,6 +11,7 @@ import {
|
||||||
Input,
|
Input,
|
||||||
} from "reactstrap";
|
} from "reactstrap";
|
||||||
import { addItemAsync } from "../../store/itemSlice";
|
import { addItemAsync } from "../../store/itemSlice";
|
||||||
|
import { selectToken } from "../../store/sessionSlice";
|
||||||
|
|
||||||
const useStyles = () => ({
|
const useStyles = () => ({
|
||||||
button: {
|
button: {
|
||||||
|
|
@ -22,12 +23,13 @@ const useStyles = () => ({
|
||||||
export default function ItemModal() {
|
export default function ItemModal() {
|
||||||
const [modal, setModal] = useState(false);
|
const [modal, setModal] = useState(false);
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
|
const token = useSelector(selectToken);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
const toggle = () => setModal(!modal);
|
const toggle = () => setModal(!modal);
|
||||||
const handleSubmit = (e) => {
|
const handleSubmit = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
dispatch(addItemAsync(name));
|
dispatch(addItemAsync(name, token));
|
||||||
toggle();
|
toggle();
|
||||||
};
|
};
|
||||||
const handleChange = (e) => setName(e.target.value);
|
const handleChange = (e) => setName(e.target.value);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import {
|
||||||
deleteItemAsync,
|
deleteItemAsync,
|
||||||
getItemsAsync,
|
getItemsAsync,
|
||||||
} from "../../store/itemSlice";
|
} from "../../store/itemSlice";
|
||||||
import { useFirebase } from "../../services/auth";
|
import { selectToken } from "../../store/sessionSlice";
|
||||||
|
|
||||||
const useStyles = () => ({
|
const useStyles = () => ({
|
||||||
removeBtn: {
|
removeBtn: {
|
||||||
|
|
@ -17,44 +17,42 @@ const useStyles = () => ({
|
||||||
export default function List() {
|
export default function List() {
|
||||||
// useSelector links the component to the store data.
|
// useSelector links the component to the store data.
|
||||||
const items = useSelector(selectItems);
|
const items = useSelector(selectItems);
|
||||||
|
const token = useSelector(selectToken);
|
||||||
// useDispatch allows to emit actions.
|
// useDispatch allows to emit actions.
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
const styles = useStyles();
|
const styles = useStyles();
|
||||||
const firebase = useFirebase();
|
|
||||||
// UseEffect loads items from the db when component renders.
|
// UseEffect loads items from the db when component renders.
|
||||||
// Precising dependencies so it doesn't render infinitely.
|
// Precising dependencies so it doesn't render infinitely.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
firebase.auth.onAuthStateChanged(async (user) => {
|
if (token) dispatch(getItemsAsync(token));
|
||||||
if (user) {
|
|
||||||
const token = await firebase.auth.currentUser.getIdToken();
|
|
||||||
dispatch(getItemsAsync(token));
|
|
||||||
|
|
||||||
// push to authslice ...
|
// push to authslice ...
|
||||||
//import axios from "axios";
|
//import axios from "axios";
|
||||||
// import * as URL from "../../constants/urls";
|
// import * as URL from "../../constants/urls";
|
||||||
// const u = await firebase.auth.currentUser;
|
// const u = await firebase.auth.currentUser;
|
||||||
// console.log(u);
|
// console.log(u);
|
||||||
// await axios.post(
|
// await axios.post(
|
||||||
// URL.USERS,
|
// URL.USERS,
|
||||||
// {
|
// {
|
||||||
// username: u.displayName,
|
// username: u.displayName,
|
||||||
// email: u.email,
|
// email: u.email,
|
||||||
// roles: {
|
// roles: {
|
||||||
// ADMIN: true,
|
// ADMIN: true,
|
||||||
// },
|
// },
|
||||||
// photoUrl: u.photoURL,
|
// photoUrl: u.photoURL,
|
||||||
// phoneNumber: u.phoneNumber,
|
// phoneNumber: u.phoneNumber,
|
||||||
// },
|
// },
|
||||||
// {
|
// {
|
||||||
// headers: {
|
// headers: {
|
||||||
// authorization: `Bearer ${token}`,
|
// authorization: `Bearer ${token}`,
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
// );
|
// );
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
}, [dispatch, firebase.auth]);
|
}, [dispatch, token]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ListGroup>
|
<ListGroup>
|
||||||
|
|
@ -64,7 +62,7 @@ export default function List() {
|
||||||
style={styles.removeBtn}
|
style={styles.removeBtn}
|
||||||
color="danger"
|
color="danger"
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => dispatch(deleteItemAsync(_id))}
|
onClick={() => dispatch(deleteItemAsync(_id, token))}
|
||||||
>
|
>
|
||||||
×
|
×
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,11 @@ import {
|
||||||
NavItem,
|
NavItem,
|
||||||
NavLink,
|
NavLink,
|
||||||
} from "reactstrap";
|
} from "reactstrap";
|
||||||
import { selectLoggedIn, getAuthUserAsync } from "../../store/sessionSlice";
|
import {
|
||||||
|
selectLoggedIn,
|
||||||
|
getAuthUserAsync,
|
||||||
|
getTokenAsync,
|
||||||
|
} from "../../store/sessionSlice";
|
||||||
import SignOutButton from "../SignOutButton";
|
import SignOutButton from "../SignOutButton";
|
||||||
import * as ROUTES from "../../constants/routes";
|
import * as ROUTES from "../../constants/routes";
|
||||||
|
|
||||||
|
|
@ -25,7 +29,8 @@ const MainNavbar = () => {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch(getAuthUserAsync());
|
dispatch(getAuthUserAsync());
|
||||||
}, [dispatch]);
|
if (isAuthenticated) dispatch(getTokenAsync());
|
||||||
|
}, [dispatch, isAuthenticated]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Navbar color="dark" dark expand="sm">
|
<Navbar color="dark" dark expand="sm">
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,19 @@ export const getAuthUserAsync = () => async (dispatch) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use auth provider to get jwt token to access backend
|
||||||
|
*/
|
||||||
|
export const getTokenAsync = () => async (dispatch) => {
|
||||||
|
const token = await firebase.auth().currentUser.getIdToken();
|
||||||
|
dispatch(getToken(token));
|
||||||
|
};
|
||||||
|
|
||||||
export const { getAuthUser, getToken, logIn, logOut } = sessionSlice.actions;
|
export const { getAuthUser, getToken, logIn, logOut } = sessionSlice.actions;
|
||||||
|
|
||||||
// selectors
|
// selectors
|
||||||
export const selectAuthUser = (state) => state.session;
|
export const selectAuthUser = (state) => state.session;
|
||||||
export const selectLoggedIn = (state) => state.session.loggedIn;
|
export const selectLoggedIn = (state) => state.session.loggedIn;
|
||||||
|
export const selectToken = (state) => state.session.token;
|
||||||
|
|
||||||
export default sessionSlice.reducer;
|
export default sessionSlice.reducer;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue