feat: Set the session token in Application

This commit is contained in:
Alexander Polynomdivision 2018-09-19 18:18:32 +02:00
parent fbb27b8947
commit 286927663a
5 changed files with 30 additions and 10 deletions

View File

@ -22,6 +22,8 @@ import BookIcon from "@material-ui/icons/Book";
import ViewWeekIcon from "@material-ui/icons/ViewWeek"; import ViewWeekIcon from "@material-ui/icons/ViewWeek";
interface IProps { interface IProps {
logout: () => void;
open: boolean; open: boolean;
showButton: boolean; showButton: boolean;
authenticated: boolean; authenticated: boolean;
@ -121,9 +123,8 @@ export default class Drawer extends React.Component<IProps> {
<Divider /> <Divider />
<ListItem button onClick={() => { <ListItem button onClick={() => {
// Remove the auth token from the SessionStorage this.closeDrawer();
// window.sessionStorage.removeItem("authKey"); this.props.logout();
// TODO: Perform logout
}}> }}>
<ListItemText> <ListItemText>
Abmelden Abmelden

View File

@ -3,6 +3,7 @@ import * as React from "react";
import { BrowserRouter, Route, Redirect } from "react-router-dom"; import { BrowserRouter, Route, Redirect } from "react-router-dom";
import AuthRoute from "../security/AuthRoute"; import AuthRoute from "../security/AuthRoute";
import { setSessionToken, removeSessionToken } from "../security/Token";
import Dashboard from "../pages/dashboard"; import Dashboard from "../pages/dashboard";
import LoginPage from "../containers/LoginPage"; import LoginPage from "../containers/LoginPage";
@ -23,6 +24,8 @@ import { IReviewMetadata, ReviewType } from "../models/review";
import { IUser } from "../models/user"; import { IUser } from "../models/user";
interface IProps { interface IProps {
authenticated: boolean;
setAuthenticated: (status: boolean) => void; setAuthenticated: (status: boolean) => void;
setUser: (user: IUser) => void; setUser: (user: IUser) => void;
}; };
@ -209,8 +212,9 @@ export default class Application extends React.Component<IProps> {
.then(resp => { .then(resp => {
if (resp.error === "0") { if (resp.error === "0") {
// Successful login // Successful login
// TODO: Set the auth token here
this.props.setUser(resp.data); this.props.setUser(resp.data);
setSessionToken(window, resp.data.sessionToken);
res(resp.data); res(resp.data);
} else { } else {
rej({}); 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 // Checks whether the user is logged in
isAuthenticated = () => { isAuthenticated = () => {
// TODO: Security? // TODO: Security?
// TODO: Implement // TODO: Implement
return true; return this.props.authenticated;
} }
render() { render() {
// TODO: Show a spinner before mounting the routes, so that we can
// check if were authenticated before doing any requests
return <BrowserRouter return <BrowserRouter
basename="/app/"> basename="/app/">
<div className="flex" > <div className="flex" >
<Drawer /> <Drawer logout={this.logout} />
<div className="content"> <div className="content">
<Route exact path="/" component={() => <Redirect to="/login" />} /> <Route exact path="/" component={() => <Redirect to="/login" />} />
<Route exact path="/login" component={() => { <Route exact path="/login" component={() => {

View File

@ -7,7 +7,9 @@ import Application from "../components/app";
import { setAuthenticated, setUser } from "../actions"; import { setAuthenticated, setUser } from "../actions";
const mapStateToProps = state => { const mapStateToProps = state => {
return {}; return {
authenticated: state.authenticated,
};
}; };
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {

View File

@ -35,8 +35,6 @@ const LoginPageWithRouter = withRouter(
const username = this.usernameRef.value || ""; const username = this.usernameRef.value || "";
const password = this.passwordRef.value || ""; const password = this.passwordRef.value || "";
this.props.login(username, password).then((res: IUser) => { this.props.login(username, password).then((res: IUser) => {
// Set the session key
window.sessionStorage.setItem("sessionToken", res.sessionToken);
this.props.history.push("/dashboard"); this.props.history.push("/dashboard");
}, (err) => { }, (err) => {
this.props.setLoading(false); this.props.setLoading(false);

7
src/security/Token.ts Normal file
View 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");
}