feat: Set the session token in Application
This commit is contained in:
parent
fbb27b8947
commit
286927663a
@ -22,6 +22,8 @@ import BookIcon from "@material-ui/icons/Book";
|
||||
import ViewWeekIcon from "@material-ui/icons/ViewWeek";
|
||||
|
||||
interface IProps {
|
||||
logout: () => void;
|
||||
|
||||
open: boolean;
|
||||
showButton: boolean;
|
||||
authenticated: boolean;
|
||||
@ -121,9 +123,8 @@ export default class Drawer extends React.Component<IProps> {
|
||||
<Divider />
|
||||
|
||||
<ListItem button onClick={() => {
|
||||
// Remove the auth token from the SessionStorage
|
||||
// window.sessionStorage.removeItem("authKey");
|
||||
// TODO: Perform logout
|
||||
this.closeDrawer();
|
||||
this.props.logout();
|
||||
}}>
|
||||
<ListItemText>
|
||||
Abmelden
|
||||
|
@ -3,6 +3,7 @@ import * as React from "react";
|
||||
import { BrowserRouter, Route, Redirect } from "react-router-dom";
|
||||
|
||||
import AuthRoute from "../security/AuthRoute";
|
||||
import { setSessionToken, removeSessionToken } from "../security/Token";
|
||||
|
||||
import Dashboard from "../pages/dashboard";
|
||||
import LoginPage from "../containers/LoginPage";
|
||||
@ -23,6 +24,8 @@ import { IReviewMetadata, ReviewType } from "../models/review";
|
||||
import { IUser } from "../models/user";
|
||||
|
||||
interface IProps {
|
||||
authenticated: boolean;
|
||||
|
||||
setAuthenticated: (status: boolean) => void;
|
||||
setUser: (user: IUser) => void;
|
||||
};
|
||||
@ -209,8 +212,9 @@ export default class Application extends React.Component<IProps> {
|
||||
.then(resp => {
|
||||
if (resp.error === "0") {
|
||||
// Successful login
|
||||
// TODO: Set the auth token here
|
||||
this.props.setUser(resp.data);
|
||||
setSessionToken(window, resp.data.sessionToken);
|
||||
|
||||
res(resp.data);
|
||||
} else {
|
||||
rej({});
|
||||
@ -219,18 +223,26 @@ export default class Application extends React.Component<IProps> {
|
||||
});
|
||||
}
|
||||
|
||||
logout = () => {
|
||||
// TODO: Tell the server that we're logging ourselves out
|
||||
removeSessionToken(window);
|
||||
this.props.setAuthenticated(false);
|
||||
}
|
||||
|
||||
// Checks whether the user is logged in
|
||||
isAuthenticated = () => {
|
||||
// TODO: Security?
|
||||
// TODO: Implement
|
||||
return true;
|
||||
return this.props.authenticated;
|
||||
}
|
||||
|
||||
render() {
|
||||
// TODO: Show a spinner before mounting the routes, so that we can
|
||||
// check if were authenticated before doing any requests
|
||||
return <BrowserRouter
|
||||
basename="/app/">
|
||||
< div className="flex" >
|
||||
<Drawer />
|
||||
<div className="flex" >
|
||||
<Drawer logout={this.logout} />
|
||||
<div className="content">
|
||||
<Route exact path="/" component={() => <Redirect to="/login" />} />
|
||||
<Route exact path="/login" component={() => {
|
||||
|
@ -7,7 +7,9 @@ import Application from "../components/app";
|
||||
import { setAuthenticated, setUser } from "../actions";
|
||||
|
||||
const mapStateToProps = state => {
|
||||
return {};
|
||||
return {
|
||||
authenticated: state.authenticated,
|
||||
};
|
||||
};
|
||||
const mapDispatchToProps = dispatch => {
|
||||
return {
|
||||
|
@ -35,8 +35,6 @@ const LoginPageWithRouter = withRouter(
|
||||
const username = this.usernameRef.value || "";
|
||||
const password = this.passwordRef.value || "";
|
||||
this.props.login(username, password).then((res: IUser) => {
|
||||
// Set the session key
|
||||
window.sessionStorage.setItem("sessionToken", res.sessionToken);
|
||||
this.props.history.push("/dashboard");
|
||||
}, (err) => {
|
||||
this.props.setLoading(false);
|
||||
|
7
src/security/Token.ts
Normal file
7
src/security/Token.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export function setSessionToken(window: Window, token: string) {
|
||||
window.sessionStorage.setItem("sessionToken", token);
|
||||
};
|
||||
|
||||
export function removeSessionToken(window: Window) {
|
||||
window.sessionStorage.removeItem("sessionToken");
|
||||
}
|
Reference in New Issue
Block a user