mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-12 11:36:39 +00:00
cleans PrivateRoute component; use redux in signup page
This commit is contained in:
parent
cb72af1b37
commit
f6612b8cbd
3 changed files with 60 additions and 51 deletions
|
|
@ -1,9 +1,10 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { Link, withRouter } from "react-router-dom";
|
import { Link, withRouter } from "react-router-dom";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { Button, Form, FormGroup, Container } from "reactstrap";
|
import { Button, Form, FormGroup, Container } from "reactstrap";
|
||||||
import * as ROUTES from "../../constants/routes";
|
import * as ROUTES from "../../constants/routes";
|
||||||
import { useFirebase } from "../../services/auth";
|
|
||||||
import InputField from "../../components/InputField";
|
import InputField from "../../components/InputField";
|
||||||
|
import { createAuthUserAsync, selectError } from "../../store/sessionSlice";
|
||||||
|
|
||||||
const useStyles = () => ({
|
const useStyles = () => ({
|
||||||
root: {
|
root: {
|
||||||
|
|
@ -37,38 +38,18 @@ const SignUpFormBase = (props) => {
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [password1, setPassword1] = useState("");
|
const [password1, setPassword1] = useState("");
|
||||||
const [password2, setPassword2] = useState("");
|
const [password2, setPassword2] = useState("");
|
||||||
const [error, setError] = useState(null);
|
const error = useSelector(selectError);
|
||||||
|
|
||||||
const auth = useFirebase();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
const cleanFields = () => {
|
|
||||||
setUserName("");
|
|
||||||
setEmail("");
|
|
||||||
setPassword1("");
|
|
||||||
setPassword2("");
|
|
||||||
setError(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = (e) => {
|
const handleSubmit = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
auth
|
dispatch(createAuthUserAsync(email, password1, props));
|
||||||
.createUserWithEmailAndPassword(email, password1)
|
|
||||||
.then(() => {
|
|
||||||
cleanFields();
|
|
||||||
props.history.push(ROUTES.APP);
|
|
||||||
})
|
|
||||||
.catch((err) => setError(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClick = (e) => {
|
const loginWithGoogle = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
auth
|
dispatch(createAuthUserAsync(props));
|
||||||
.signInWithGoogle()
|
|
||||||
.then(() => {
|
|
||||||
cleanFields();
|
|
||||||
props.history.push(ROUTES.APP);
|
|
||||||
})
|
|
||||||
.catch((err) => setError(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const isInvalid =
|
const isInvalid =
|
||||||
|
|
@ -110,7 +91,12 @@ const SignUpFormBase = (props) => {
|
||||||
>
|
>
|
||||||
Sign Up
|
Sign Up
|
||||||
</Button>
|
</Button>
|
||||||
<Button color="light" style={styles.button} block onClick={handleClick}>
|
<Button
|
||||||
|
color="light"
|
||||||
|
style={styles.button}
|
||||||
|
block
|
||||||
|
onClick={loginWithGoogle}
|
||||||
|
>
|
||||||
Sign Up with Google
|
Sign Up with Google
|
||||||
</Button>
|
</Button>
|
||||||
{error && <p>{error.message}</p>}
|
{error && <p>{error.message}</p>}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,8 @@
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect } from "react";
|
||||||
import { withRouter, Route } from "react-router-dom";
|
import { withRouter, Route } from "react-router-dom";
|
||||||
import { useFirebase } from "../services/auth";
|
|
||||||
import * as ROUTES from "../constants/routes";
|
import * as ROUTES from "../constants/routes";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import {
|
import { selectAuthUser } from "../store/sessionSlice";
|
||||||
selectAuthUser,
|
|
||||||
getAuthUserAsync,
|
|
||||||
selectLoggedIn,
|
|
||||||
} from "../store/sessionSlice";
|
|
||||||
|
|
||||||
const PrivateRoute = ({
|
const PrivateRoute = ({
|
||||||
component: Component,
|
component: Component,
|
||||||
|
|
@ -16,26 +11,13 @@ const PrivateRoute = ({
|
||||||
history,
|
history,
|
||||||
...rest
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
// const [authUser, setAuthUser] = useState(null);
|
|
||||||
// const firebase = useFirebase();
|
|
||||||
const authUser = useSelector(selectAuthUser);
|
const authUser = useSelector(selectAuthUser);
|
||||||
|
|
||||||
const dispatch = useDispatch();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// dispatch(getAuthUserAsync())
|
|
||||||
|
|
||||||
if (!condition(authUser)) {
|
if (!condition(authUser)) {
|
||||||
history.push(ROUTES.SIGN_IN);
|
history.push(ROUTES.SIGN_IN);
|
||||||
}
|
}
|
||||||
// else {
|
}, [condition, history, authUser]);
|
||||||
// setAuthUser(authUser);
|
|
||||||
// }
|
|
||||||
}, [
|
|
||||||
// firebase.auth,
|
|
||||||
condition,
|
|
||||||
history,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const render = (props) => <Component {...props} />;
|
const render = (props) => <Component {...props} />;
|
||||||
return condition(authUser) ? (
|
return condition(authUser) ? (
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { createSlice } from "@reduxjs/toolkit";
|
import { createSlice } from "@reduxjs/toolkit";
|
||||||
import { firebase } from "../services/auth";
|
import { firebase } from "../services/auth";
|
||||||
|
import * as ROUTES from "../constants/routes";
|
||||||
|
|
||||||
export const sessionSlice = createSlice({
|
export const sessionSlice = createSlice({
|
||||||
name: "session",
|
name: "session",
|
||||||
|
|
@ -18,7 +19,7 @@ export const sessionSlice = createSlice({
|
||||||
...state,
|
...state,
|
||||||
...action.payload,
|
...action.payload,
|
||||||
}),
|
}),
|
||||||
addAuthUser: (state, action) => {},
|
createAuthUser: (state, action) => {},
|
||||||
getToken: (state, action) => ({
|
getToken: (state, action) => ({
|
||||||
...state,
|
...state,
|
||||||
token: action.payload,
|
token: action.payload,
|
||||||
|
|
@ -31,6 +32,10 @@ export const sessionSlice = createSlice({
|
||||||
...state,
|
...state,
|
||||||
loggedIn: false,
|
loggedIn: false,
|
||||||
}),
|
}),
|
||||||
|
newError: (state, action) => ({
|
||||||
|
...state,
|
||||||
|
error: action.payload,
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -63,11 +68,47 @@ export const getTokenAsync = () => async (dispatch) => {
|
||||||
dispatch(getToken(token));
|
dispatch(getToken(token));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const { getAuthUser, getToken, logIn, logOut } = sessionSlice.actions;
|
/**
|
||||||
|
* Create auth user with email and password
|
||||||
|
*/
|
||||||
|
export const createAuthUserAsync = (email, password, props) => async (
|
||||||
|
dispatch
|
||||||
|
) => {
|
||||||
|
firebase
|
||||||
|
.auth()
|
||||||
|
.createUserWithEmailAndPassword(email, password)
|
||||||
|
.then(() => props.history.push(ROUTES.APP))
|
||||||
|
.catch((err) => dispatch(newError(err)));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create auth user with google
|
||||||
|
*/
|
||||||
|
export const createAuthUserWithGoogleAsync = (props) => async (dispatch) => {
|
||||||
|
const provider = new firebase.auth.GoogleAuthProvider();
|
||||||
|
firebase
|
||||||
|
.auth()
|
||||||
|
// signInWithRedirect(this.provider);
|
||||||
|
.signInWithPopup(provider)
|
||||||
|
.then(() => props.history.push(ROUTES.APP))
|
||||||
|
.catch((err) => dispatch(newError(err)));
|
||||||
|
};
|
||||||
|
|
||||||
|
// actions
|
||||||
|
export const {
|
||||||
|
getAuthUser,
|
||||||
|
createAuthUser,
|
||||||
|
getToken,
|
||||||
|
logIn,
|
||||||
|
logOut,
|
||||||
|
newError,
|
||||||
|
} = 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 const selectToken = (state) => state.session.token;
|
||||||
|
export const selectError = (state) => state.session.error;
|
||||||
|
|
||||||
|
// reducer
|
||||||
export default sessionSlice.reducer;
|
export default sessionSlice.reducer;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue