Implement login request

This commit is contained in:
rui hildt 2020-08-19 13:38:25 +02:00
parent e1641b32d1
commit 125a03f529
6 changed files with 39 additions and 51 deletions

View File

@ -29,8 +29,8 @@
"rsuite": "^4.8.0"
},
"scripts": {
"start": "PORT=8000 react-app-rewired start",
"dev": "BROWSER=firefox-developer-edition react-app-rewired start",
"start": "react-app-rewired start",
"dev": "PORT=8000 BROWSER=firefox-developer-edition react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"

1
src/constants.js Normal file
View File

@ -0,0 +1 @@
export const API_URL = 'http://localhost:3000/api';

View File

@ -1,10 +0,0 @@
import axios from 'axios';
export const API_URL = 'https://meeting-planner-backend.herokuapp.com';
export const my_app = axios.create({
baseURL: API_URL,
headers: {
'Content-type': 'application/json',
},
});

View File

@ -1,6 +1,6 @@
import React, { useState, useEffect } from 'react';
import React, { useState } from 'react';
import { useHistory } from 'react-router-dom';
import axios from 'axios';
import { backend } from '../utils/http-common';
import {
Panel,
Form,
@ -8,55 +8,38 @@ import {
FormControl,
ControlLabel,
Button,
Message,
} from 'rsuite';
import { setUserSession } from '../utils/common';
import NavBar from './../components/Navbar/NavBar';
export default function Login({ title }) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const history = useHistory();
const [error, setError] = useState(false);
const [credentials, setCredentials] = useState({
email: '',
password: '',
});
const history = useHistory();
// handle input fields
const handleChange = (value, evt) => {
console.log(value, evt.target.name);
setCredentials({
...credentials,
[evt.target.name]: value,
});
};
// handle button click of login form
const handleLogin = () => {
setError(null);
setLoading(true);
console.log(credentials.email, credentials.password);
axios
.post('http://localhost:3000/api/auth/login', credentials)
backend
.post('/auth/login', credentials)
.then((response) => {
setLoading(false);
console.log('RESPONSE', response);
localStorage.setItem(
'data',
JSON.stringify({
token: response.data.token,
user: response.data.user,
}),
);
// TODO: Working redirection
history.push('');
setUserSession(response.data.token, response.data.user);
history.push('/dashboard');
})
.catch((error) => {
console.log('ERROR', error);
setLoading(false);
if (error.response.status === 401)
setError(error.response.data.message);
setError('Incorrect credentials. Please try again.');
else setError('Something went wrong. Please try again later.');
});
};
@ -65,6 +48,7 @@ export default function Login({ title }) {
<>
<NavBar title={title} />
<Panel bordered style={boxStyle}>
{error && <Message type='error' description={error} />}
<Form>
<FormGroup>
<ControlLabel>Email</ControlLabel>

View File

@ -1,23 +1,27 @@
// return the user data from the session storage
// return the user data from the local storage
export const getUser = () => {
const userStr = sessionStorage.getItem('user');
if (userStr) return JSON.parse(userStr);
const user = JSON.parse(localStorage.getItem('user'));
if (user) return user;
else return null;
};
// return the token from the session storage
// return the token from the local storage
export const getToken = () => {
return sessionStorage.getItem('token') || null;
const token = JSON.parse(localStorage.getItem('token'));
if (token) return token;
else return null;
};
// remove the token and user from the session storage
// remove the token and user from the local storage
export const removeUserSession = () => {
sessionStorage.removeItem('token');
sessionStorage.removeItem('user');
localStorage.removeItem('token');
localStorage.removeItem('user');
};
// set the token and user from the session storage
// set the token and user from the local storage
export const setUserSession = (token, user) => {
sessionStorage.setItem('token', token);
sessionStorage.setItem('user', JSON.stringify(user));
localStorage.setItem('token', token);
localStorage.setItem('user', JSON.stringify(user));
};

9
src/utils/http-common.js Normal file
View File

@ -0,0 +1,9 @@
import axios from 'axios';
import { API_URL } from '../constants';
export const backend = axios.create({
baseURL: API_URL,
headers: {
'Content-type': 'application/json',
},
});