Auth.js:
import React, { Component } from 'react'
import './Auth.scss'
import is from 'is_js'
import firebaseconfig from '../firebase/firebaseConfig'
import firebase from 'firebase'
export default class Auth extends Component {
state = {
isAuth: false,
isFormValid: false,
formControls: {
email: {
value: '',
type: 'email',
label: 'email',
errorMessage: 'Введите корректный email',
valid: true,
touched: false,
validation: {
required: true,
email: true
}
},
password: {
value: '',
type: 'password',
label: 'Пароль',
errorMessage: 'Введите корректный пароль',
valid: true,
touched: false,
validation: {
required: true,
minLength: 6
}
}
}
}
validateControl(value, validation) {
if (!validation) {
return true
}
let isValid = true
if (validation.required) {
isValid = value.trim() !== '' && isValid
}
if (validation.email) {
isValid = is.email(value) && isValid
}
if (validation.minLength) {
isValid = value.length >= validation.minLength && isValid
}
return isValid
}
onChangeHandler = (event, controlName) => {
const formControls = { ...this.state.formControls }
const control = { ...formControls[controlName] }
control.value = event.target.value
control.touched = true
control.valid = this.validateControl(control.value, control.validation)
formControls[controlName] = control
let isFormValid = true
Object.keys(formControls).forEach(name => {
isFormValid = formControls[name].valid && isFormValid && formControls[name].touched
})
this.setState({
formControls, isFormValid
})
}
submitHandler = (event) => {
event.preventDefault()
}
renderError(controlName) {
const control = this.state.formControls[controlName]
if(control.touched) {
return(
control.valid
? <div style={{ color: 'green' }}>{control.label}: все отлично!</div>
: <div style={{ color: 'red' }}>{control.label}: введите верное значение поля</div>
)
} else {
return (
<div>Введите значение</div>
)
}
}
renderButtons() {
return (
<>
<button
disabled={!this.state.isFormValid}
onClick={this.loginHandler}
>
Войти
</button>
<button
disabled={!this.state.isFormValid}
onClick={this.registerHandler}
>
Зарегистрироваться
</button>
</>
)
}
loginHandler = () => {
firebase.auth().signInWithEmailAndPassword(this.state.formControls.email.value, this.state.formControls.password.value)
.then((user) => {
console.log(user)
console.log("Вы вошли")
})
.catch((error) => {
console.log(error.message)
})
}
registerHandler = () => {
firebase.auth().createUserWithEmailAndPassword(this.state.formControls.email.value, this.state.formControls.password.value)
.then((user) => {
console.log(user)
console.log("Успешно зарегестрирован")
})
.catch((error) => {
console.log(error.message)
})
}
renderInputs() {
return Object.keys(this.state.formControls).map((controlName, index) => {
const control = this.state.formControls[controlName]
const htmlFor = `${control.type}-${Math.random()}`
return (
<>
<label htmlFor="">{control.label}: </label>
<input
type={control.type}
id={htmlFor}
value={control.value}
onChange={event => this.onChangeHandler(event, controlName)}
/>
{this.renderError(controlName)}
</>
)
})
}
render() {
return (
<div className="auth">
<h1 className="auth__header">Авторизация</h1>
<form
onSubmit={this.submitHandler}
className="auth__form"
>
{this.renderInputs()}
{this.renderButtons()}
</form>
</div>
)
}
}