use redux update and reset password and signout button"

This commit is contained in:
Ruidy Nemausat 2020-04-25 11:51:43 +02:00
parent 7a1c6b3477
commit 00c71eefa6
6 changed files with 46 additions and 27 deletions

View file

@ -1,7 +1,8 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Button, Form, FormGroup } from "reactstrap"; import { Button, Form, FormGroup } from "reactstrap";
import { useFirebase } from "../../services/auth";
import InputField from "../InputField"; import InputField from "../InputField";
import { updatePasswordAsync, selectError } from "../../store/sessionSlice";
const useStyles = () => ({ const useStyles = () => ({
root: { root: {
@ -17,19 +18,16 @@ const useStyles = () => ({
const PasswordChangeForm = () => { const PasswordChangeForm = () => {
const [password1, setPassword1] = useState(""); const [password1, setPassword1] = useState("");
const [password2, setPassword2] = useState(""); const [password2, setPassword2] = useState("");
const [error, setError] = useState(null);
const auth = useFirebase(); const error = useSelector(selectError);
const dispatch = useDispatch();
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault(); e.preventDefault();
auth setPassword1("");
.updatePassword(password1) setPassword2("");
.then(() => { dispatch(updatePasswordAsync(password1));
setPassword1("");
setPassword2("");
})
.catch((err) => setError(err));
}; };
const isInvalid = password1 === password2 || password1 === ""; const isInvalid = password1 === password2 || password1 === "";

View file

@ -1,7 +1,8 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Button, Form, FormGroup } from "reactstrap"; import { Button, Form, FormGroup } from "reactstrap";
import { useFirebase } from "../../services/auth";
import InputField from "../../components/InputField"; import InputField from "../../components/InputField";
import { resetPasswordAsync, selectError } from "../../store/sessionSlice";
const useStyles = () => ({ const useStyles = () => ({
button: { button: {
@ -12,19 +13,14 @@ const useStyles = () => ({
const PasswordForgetForm = () => { const PasswordForgetForm = () => {
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [error, setError] = useState(null); const error = useSelector(selectError);
const auth = useFirebase(); const dispatch = useDispatch();
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault(); e.preventDefault();
auth setEmail("");
.resetPassword(email) dispatch(resetPasswordAsync(email));
.then(() => {
setEmail("");
setError(null);
})
.catch((err) => setError(err));
}; };
const isInvalid = email === ""; const isInvalid = email === "";

View file

@ -1,9 +1,11 @@
import React from "react"; import React from "react";
import { withRouter } from "react-router-dom";
import { useDispatch } from "react-redux";
import { Button } from "reactstrap"; import { Button } from "reactstrap";
import { useFirebase } from "../../services/auth"; import { signOutAsync } from "../../store/sessionSlice";
const SignOutButton = () => { const SignOutButton = () => {
const auth = useFirebase(); const dispatch = useDispatch();
return <Button onClick={auth.signOut}>Sign Out</Button>; return <Button onClick={() => dispatch(signOutAsync())}>Sign Out</Button>;
}; };
export default SignOutButton; export default withRouter(SignOutButton);

View file

@ -1,4 +1,4 @@
import * as ROLES from "./roles"; import * as ROLES from "./roles";
export const AUTHENTICATED = (authUser) => !!authUser; export const AUTHENTICATED = (authUser) => !!authUser.loggedIn;
export const ADMIN = (authUser) => authUser && !!authUser.roles[ROLES.ADMIN]; export const ADMIN = (authUser) => authUser && !!authUser.roles[ROLES.ADMIN];

View file

@ -15,7 +15,7 @@ const PrivateRoute = ({
useEffect(() => { useEffect(() => {
if (!condition(authUser)) { if (!condition(authUser)) {
history.push(ROUTES.SIGN_IN); setTimeout(() => history.push(ROUTES.SIGN_IN), 0);
} }
}, [condition, history, authUser]); }, [condition, history, authUser]);

View file

@ -87,10 +87,33 @@ export const signInAsync = (email, password, props) => async (dispatch) => {
firebase firebase
.auth() .auth()
.signInWithEmailAndPassword(email, password) .signInWithEmailAndPassword(email, password)
.then(() => props.history.push(ROUTES.APP)) .then(() => {
dispatch(logIn());
props.history.push(ROUTES.APP);
})
.catch((err) => dispatch(newError(err))); .catch((err) => dispatch(newError(err)));
}; };
/**
* Sign out
*/
export const signOutAsync = () => async (dispatch) => {
firebase
.auth()
.signOut()
.then(() => {
dispatch(logOut());
});
};
export const resetPasswordAsync = (email) => async (dispatch) => {
firebase.auth().sendPasswordResetEmail(email);
};
export const updatePasswordAsync = (password) => async (dispatch) => {
firebase.auth().currentUser.updatePassword(password);
};
/** /**
* Create auth user with google * Create auth user with google
*/ */