From 34a54ead88690c14a60dd98aeda472948e9cc38d Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 2 Aug 2022 13:31:17 -0400 Subject: [PATCH] store login details in localStorage (#17) --- ui/src/App.js | 10 +++++++++- ui/src/Login.js | 12 ++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ui/src/App.js b/ui/src/App.js index 739d5628..c8156c3a 100644 --- a/ui/src/App.js +++ b/ui/src/App.js @@ -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 ( { 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')