store login details in localStorage (#17)

This commit is contained in:
Michael Quigley 2022-08-02 13:31:17 -04:00
parent e699994ca5
commit 34a54ead88
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 17 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import Login from './Login';
import Logout from './Logout';
import Version from './Version';
import * as gateway from "./api/gateway";
import {useState} from "react";
import {useEffect, useState} from "react";
gateway.init({
url: '/api/v1'
@ -11,6 +11,14 @@ gateway.init({
const App = () => {
const [user, setUser] = useState();
useEffect(() => {
const localUser = localStorage.getItem("user")
if(localUser) {
setUser(JSON.parse(localUser))
console.log('reloaded user', localUser)
}
}, []);
if (!user) {
return (
<Login

View File

@ -7,13 +7,17 @@ const Login = (props) => {
const handleSubmit = async e => {
e.preventDefault()
identity.login({body: {"email": email, "password": password}})
.then(resp => {
if(!resp.error) {
props.loginSuccess({
email: email,
token: resp.token
})
let user = {
"email": email,
"token": resp.data
}
props.loginSuccess(user)
localStorage.setItem('user', JSON.stringify(user))
console.log(user)
console.log('login succeeded', resp)
} else {
console.log('login failed')