Skip to main content

18.07 Adding Sign In

Fix Burger.module.css

Remove the horisontal scroll.

src\components\Burger\Burger.module.css
.Burger {
width: 100%;
margin: auto;
height: 250px;
overflow: auto;
text-align: center;
font-weight: bold;
font-size: 1.2rem;
}

@media (min-width: 500px) and (min-height: 400px) {
.Burger {
width: 350px;
height: 300px;
}
}

@media (min-width: 500px) and (min-height: 401px) {
.Burger {
width: 450px;
height: 400px;
}
}

@media (min-width: 1000px) and (min-height: 700px) {
.Burger {
width: 700px;
height: 600px;
}
}

Fix Auth.module.css

Make two buttons vertical.

src\containers\Auth\Auth.module.css
.Auth {
margin: 20px auto;
width: 80%;
text-align: center;
box-shadow: 0 2px 3px #ccc;
border: 1px solid #eee;
padding: 10px;
box-sizing: border-box;
}
.Auth Button {
display: block;
margin: 0 auto !important;
}
@media (min-width: 600px) {
.Auth {
width: 500px;
}
}

Auth.js

src\containers\Auth\Auth.js
import React, { Component } from "react";
import { connect } from "react-redux";
import Button from "../../components/UI/Button/Button";
import Input from "../../components/UI/Input/Input";
import * as actions from "../../store/actions/index";
import classes from "./Auth.module.css";

class Auth extends Component {
state = {
controls: {
email: {
elementType: "input",
elementConfig: {
type: "email",
placeholder: "Mail Address",
},
value: "",
validation: {
required: true,
isEmail: true,
},
valid: false,
touched: false,
},
password: {
elementType: "input",
elementConfig: {
type: "password",
placeholder: "Password",
},
value: "",
validation: {
required: true,
minLength: 6,
},
valid: false,
touched: false,
},
},
isSignup: true,
};

checkValidity(value, rules) {
let isValid = true;
if (!rules) {
return true;
}

if (rules.required) {
isValid = value.trim() !== "" && isValid;
}

if (rules.minLength) {
isValid = value.length >= rules.minLength && isValid;
}

if (rules.maxLength) {
isValid = value.length <= rules.maxLength && isValid;
}

if (rules.isEmail) {
const pattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
isValid = pattern.test(value) && isValid;
}

if (rules.isNumeric) {
const pattern = /^\d+$/;
isValid = pattern.test(value) && isValid;
}

return isValid;
}

inputChangedHandler = (event, controlName) => {
const updatedControls = {
...this.state.controls,
[controlName]: {
...this.state.controls[controlName],
value: event.target.value,
valid: this.checkValidity(
event.target.value,
this.state.controls[controlName].validation
),
touched: true,
},
};
this.setState({ controls: updatedControls });
};

submitHandler = (event) => {
event.preventDefault();
this.props.onAuth(
this.state.controls.email.value,
this.state.controls.password.value,
this.state.isSignup
);
};

switchAuthModeHandler = () => {
this.setState((prevState) => {
return { isSignup: !prevState.isSignup };
});
};
render() {
const formElementsArray = [];
for (let key in this.state.controls) {
formElementsArray.push({
id: key,
config: this.state.controls[key],
});
}

const form = formElementsArray.map((formElement) => (
<Input
key={formElement.id}
elementType={formElement.config.elementType}
elementConfig={formElement.config.elementConfig}
value={formElement.config.value}
invalid={!formElement.config.valid}
shouldValidate={formElement.config.validation}
touched={formElement.config.touched}
changed={(event) => this.inputChangedHandler(event, formElement.id)}
/>
));
return (
<div className={classes.Auth}>
<form onSubmit={this.submitHandler}>
{form}
<Button btnType="Success">Submit</Button>
<Button clicked={this.switchAuthModeHandler} btnType="Danger">
SWITCH TO {this.state.isSignup ? "SIGNIN" : "SIGNUP"}
</Button>
</form>
</div>
);
}
}

const mapDispatchToProps = (dispatch) => {
return {
onAuth: (email, password, isSignup) =>
dispatch(actions.auth(email, password, isSignup)),
};
};
export default connect(null, mapDispatchToProps)(Auth);

auth.js

src\store\actions\auth.js
import axios from "axios";
import * as actionTypes from "./actionTypes";

export const authStart = () => {
return {
type: actionTypes.AUTH_START,
};
};

export const authSuccess = (authData) => {
return {
type: actionTypes.AUTH_SUCCESS,
authData: authData,
};
};

export const authFail = (error) => {
return {
type: actionTypes.AUTH_FAIL,
error: error,
};
};

export const auth = (email, password, isSignup) => {
return (dispatch) => {
dispatch(authStart());
const authData = {
email: email,
password: password,
returnSecureToken: true,
};
let url =
"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyDsmwPeH2yE7yqvoeYolCLgB9ju50rYivo";
if (!isSignup) {
url =
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyDsmwPeH2yE7yqvoeYolCLgB9ju50rYivo";
}
axios
.post(url, authData)
.then((response) => {
console.log(response);
dispatch(authSuccess(response.data));
})
.catch((err) => {
console.log(err);
dispatch(authFail(err));
});
};
};