From 7a1c6b3477792ae0e45bbb27e54c95f74949bebc Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Sat, 25 Apr 2020 10:38:52 +0200 Subject: [PATCH] uses redux in sign in page --- client/src/pages/SignIn/index.jsx | 44 ++++++++++++------------------- client/src/pages/SignUp/index.jsx | 8 ++++-- client/src/store/sessionSlice.js | 12 ++++++++- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/client/src/pages/SignIn/index.jsx b/client/src/pages/SignIn/index.jsx index 39e59ce..336cd29 100644 --- a/client/src/pages/SignIn/index.jsx +++ b/client/src/pages/SignIn/index.jsx @@ -1,11 +1,15 @@ import React, { useState } from "react"; import { withRouter } from "react-router-dom"; +import { useDispatch, useSelector } from "react-redux"; import { Button, Form, FormGroup, Container } from "reactstrap"; -import { useFirebase } from "../../services/auth"; import { SignUpLink } from "../SignUp"; import InputField from "../../components/InputField"; -import * as ROUTES from "../../constants/routes"; import { PasswordForgetLink } from "../PasswordForget"; +import { + signInAsync, + selectError, + createAuthUserWithGoogleAsync, +} from "../../store/sessionSlice"; const useStyles = () => ({ root: { @@ -36,37 +40,18 @@ export default function SignInPage() { const SignInFormBase = (props) => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); - const [error, setError] = useState(null); - const auth = useFirebase(); - - const cleanFields = () => { - setEmail(""); - setPassword(""); - setError(null); - }; + const error = useSelector(selectError); + const dispatch = useDispatch(); const handleSubmit = (e) => { e.preventDefault(); - - auth - .signinWithEmailAndPassword(email, password) - .then(() => { - cleanFields(); - props.history.push(ROUTES.APP); - }) - .catch((err) => setError(err)); + dispatch(signInAsync(email, password, props)); }; - const handleClick = (e) => { + const loginWithGoogle = (e) => { e.preventDefault(); - auth - .signInWithGoogle() - .then(() => { - cleanFields(); - props.history.push(ROUTES.APP); - }) - .catch((err) => setError(err)); + dispatch(createAuthUserWithGoogleAsync(props)); }; const isInvalid = email === "" || password === ""; @@ -97,7 +82,12 @@ const SignInFormBase = (props) => { > Sign In - {error &&

{error.message}

} diff --git a/client/src/pages/SignUp/index.jsx b/client/src/pages/SignUp/index.jsx index c387c99..3951520 100644 --- a/client/src/pages/SignUp/index.jsx +++ b/client/src/pages/SignUp/index.jsx @@ -4,7 +4,11 @@ import { useDispatch, useSelector } from "react-redux"; import { Button, Form, FormGroup, Container } from "reactstrap"; import * as ROUTES from "../../constants/routes"; import InputField from "../../components/InputField"; -import { createAuthUserAsync, selectError } from "../../store/sessionSlice"; +import { + createAuthUserAsync, + createAuthUserWithGoogleAsync, + selectError, +} from "../../store/sessionSlice"; const useStyles = () => ({ root: { @@ -49,7 +53,7 @@ const SignUpFormBase = (props) => { const loginWithGoogle = (e) => { e.preventDefault(); - dispatch(createAuthUserAsync(props)); + dispatch(createAuthUserWithGoogleAsync(props)); }; const isInvalid = diff --git a/client/src/store/sessionSlice.js b/client/src/store/sessionSlice.js index be6007a..6719a77 100644 --- a/client/src/store/sessionSlice.js +++ b/client/src/store/sessionSlice.js @@ -19,7 +19,6 @@ export const sessionSlice = createSlice({ ...state, ...action.payload, }), - createAuthUser: (state, action) => {}, getToken: (state, action) => ({ ...state, token: action.payload, @@ -81,6 +80,17 @@ export const createAuthUserAsync = (email, password, props) => async ( .catch((err) => dispatch(newError(err))); }; +/** + * Sign in with email and password + */ +export const signInAsync = (email, password, props) => async (dispatch) => { + firebase + .auth() + .signInWithEmailAndPassword(email, password) + .then(() => props.history.push(ROUTES.APP)) + .catch((err) => dispatch(newError(err))); +}; + /** * Create auth user with google */