src/components/login/Login.js
/**
     * HTTP POST request is sent to the backend.
     * If the request is successful, a new user is returned to the front-end
     * and its token is stored in the localStorage.
     */
HTTP POST request is sent to the backend.
If the request is successful, a new user is returned to the front-end
and its token is stored in the localStorage.
HTTP POST request is sent to the backend . If the request is successful a new user is returned to the front - end and its token is stored in the localStorage .
async login() {
        try {
            const requestBody = JSON.stringify({
                username: this.state.username,
                password: this.state.password
            });
            const response = await api.put('/login', requestBody);
            const user = new User(response.data);
            if (localStorage.getItem('userId')) {
                api.put('/logout/' + localStorage.getItem('userId'));
            }
            localStorage.setItem('userId', user.id);
            this.props.history.push(`/map`);
        } catch (error) {
            this.handleErrorDesigned(error);
            this.setState({hasErrorMessage: true});
        }
    }
async login ( ) { try { const requestBody = JSON . stringify ( { username : this . state . username , password : this . state . password } ) ; const response = await api . put ( '/login' , requestBody ) ; const user = new User ( response . data ) ; if ( localStorage . getItem ( 'userId' ) ) { api . put ( '/logout/' + localStorage . getItem ( 'userId' ) ) ; } localStorage . setItem ( 'userId' , user . id ) ; this . props . history . push ( ` ` ) ; } catch ( error ) { this . handleErrorDesigned ( error ) ; this . setState ( { hasErrorMessage : true } ) ; } }
HTTP POST request is sent to the backend.
HTTP POST request is sent to the backend .
// Get the returned user and update a new object. //check whether there already was a user in the local storage, if yes, logout this user // Store the id into the local storage. // Login successfully worked --> navigate to the the route /map in the GameRouter //alert(`Something went wrong during the login: \n${handleError2(error)}`);