mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-11 19:16:39 +00:00
uses redux in sign in page
This commit is contained in:
parent
f6612b8cbd
commit
7a1c6b3477
3 changed files with 34 additions and 30 deletions
|
|
@ -1,11 +1,15 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { withRouter } from "react-router-dom";
|
import { 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 { useFirebase } from "../../services/auth";
|
|
||||||
import { SignUpLink } from "../SignUp";
|
import { SignUpLink } from "../SignUp";
|
||||||
import InputField from "../../components/InputField";
|
import InputField from "../../components/InputField";
|
||||||
import * as ROUTES from "../../constants/routes";
|
|
||||||
import { PasswordForgetLink } from "../PasswordForget";
|
import { PasswordForgetLink } from "../PasswordForget";
|
||||||
|
import {
|
||||||
|
signInAsync,
|
||||||
|
selectError,
|
||||||
|
createAuthUserWithGoogleAsync,
|
||||||
|
} from "../../store/sessionSlice";
|
||||||
|
|
||||||
const useStyles = () => ({
|
const useStyles = () => ({
|
||||||
root: {
|
root: {
|
||||||
|
|
@ -36,37 +40,18 @@ export default function SignInPage() {
|
||||||
const SignInFormBase = (props) => {
|
const SignInFormBase = (props) => {
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [error, setError] = useState(null);
|
|
||||||
|
|
||||||
const auth = useFirebase();
|
const error = useSelector(selectError);
|
||||||
|
const dispatch = useDispatch();
|
||||||
const cleanFields = () => {
|
|
||||||
setEmail("");
|
|
||||||
setPassword("");
|
|
||||||
setError(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = (e) => {
|
const handleSubmit = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
dispatch(signInAsync(email, password, props));
|
||||||
auth
|
|
||||||
.signinWithEmailAndPassword(email, password)
|
|
||||||
.then(() => {
|
|
||||||
cleanFields();
|
|
||||||
props.history.push(ROUTES.APP);
|
|
||||||
})
|
|
||||||
.catch((err) => setError(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClick = (e) => {
|
const loginWithGoogle = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
auth
|
dispatch(createAuthUserWithGoogleAsync(props));
|
||||||
.signInWithGoogle()
|
|
||||||
.then(() => {
|
|
||||||
cleanFields();
|
|
||||||
props.history.push(ROUTES.APP);
|
|
||||||
})
|
|
||||||
.catch((err) => setError(err));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const isInvalid = email === "" || password === "";
|
const isInvalid = email === "" || password === "";
|
||||||
|
|
@ -97,7 +82,12 @@ const SignInFormBase = (props) => {
|
||||||
>
|
>
|
||||||
Sign In
|
Sign In
|
||||||
</Button>
|
</Button>
|
||||||
<Button color="light" style={styles.button} block onClick={handleClick}>
|
<Button
|
||||||
|
color="light"
|
||||||
|
style={styles.button}
|
||||||
|
block
|
||||||
|
onClick={loginWithGoogle}
|
||||||
|
>
|
||||||
Sign In with Google
|
Sign In with Google
|
||||||
</Button>
|
</Button>
|
||||||
{error && <p>{error.message}</p>}
|
{error && <p>{error.message}</p>}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,11 @@ 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 InputField from "../../components/InputField";
|
import InputField from "../../components/InputField";
|
||||||
import { createAuthUserAsync, selectError } from "../../store/sessionSlice";
|
import {
|
||||||
|
createAuthUserAsync,
|
||||||
|
createAuthUserWithGoogleAsync,
|
||||||
|
selectError,
|
||||||
|
} from "../../store/sessionSlice";
|
||||||
|
|
||||||
const useStyles = () => ({
|
const useStyles = () => ({
|
||||||
root: {
|
root: {
|
||||||
|
|
@ -49,7 +53,7 @@ const SignUpFormBase = (props) => {
|
||||||
|
|
||||||
const loginWithGoogle = (e) => {
|
const loginWithGoogle = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
dispatch(createAuthUserAsync(props));
|
dispatch(createAuthUserWithGoogleAsync(props));
|
||||||
};
|
};
|
||||||
|
|
||||||
const isInvalid =
|
const isInvalid =
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ export const sessionSlice = createSlice({
|
||||||
...state,
|
...state,
|
||||||
...action.payload,
|
...action.payload,
|
||||||
}),
|
}),
|
||||||
createAuthUser: (state, action) => {},
|
|
||||||
getToken: (state, action) => ({
|
getToken: (state, action) => ({
|
||||||
...state,
|
...state,
|
||||||
token: action.payload,
|
token: action.payload,
|
||||||
|
|
@ -81,6 +80,17 @@ export const createAuthUserAsync = (email, password, props) => async (
|
||||||
.catch((err) => dispatch(newError(err)));
|
.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
|
* Create auth user with google
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue