import React, { Component } from 'react'; import Icon from '@iconify/react'; import plusIcon from '@iconify/icons-fa-solid/plus'; import '../../css/Components/connection.css' import axios from 'axios'; import { Redirect } from 'react-router'; class Connection extends Component { constructor(props){ super(props) this.state={ identifyer:'', password:'', redirect:false, errors:[] } } handleIdentifyerChange = event => { this.setState({identifyer : event.target.value}) } handlePasswordChange = event => { this.setState({password : event.target.value}) } handleSubmit = (event) => { event.preventDefault() console.log(this.state.password); let bodyFormData = new FormData(); bodyFormData.set('identifyer',this.state.identifyer); bodyFormData.set('password',this.state.password) axios.post('https://etud.insa-toulouse.fr/~proximo/v2/api/login',bodyFormData) .then(res => { sessionStorage.setItem('token',res.data.api_token) this.setState({redirect:true}) console.log(res.data.api_token); }) .catch(error => { if(error.response.status === 401){ this.setState({errors:error.response.data.errors}) } console.log(error.response); }) } render() { if (this.state.redirect){ return () } return (
Connexion

{this.state.errors.identifyer}

{this.state.errors === 'bad_pwd'?

Mot de passe incorrect

:

{this.state.errors.password}

}
); } } export default Connection;