Add isLoggedIn middleware

This commit is contained in:
Kacper Donat 2020-10-03 13:47:58 +02:00
parent 379acad8c5
commit ff2e9c8b82
3 changed files with 19 additions and 9 deletions

View File

@ -1,10 +1,11 @@
import { Middleware, route } from "@/routing";
import { useSelector } from "react-redux";
import { isReady } from "@/state/reducer";
import { AppState, isReady } from "@/state/reducer";
import { Redirect } from "react-router-dom";
import React from "react";
import { UserState } from "@/state/reducer/user";
export const isReadyMiddleware: Middleware<any, any> = next => {
export const isReadyMiddleware: Middleware<any, any> = next => isLoggedInMiddleware(() => {
const ready = useSelector(isReady);
if (ready) {
@ -12,4 +13,14 @@ export const isReadyMiddleware: Middleware<any, any> = next => {
}
return <Redirect to={ route("edition_pick") } />;
})
export const isLoggedInMiddleware: Middleware<any, any> = next => {
const user = useSelector<AppState>(state => state.user) as UserState;
if (user.loggedIn) {
return next();
}
return <Redirect to={ route("login") } />;
}

View File

@ -7,7 +7,7 @@ import SubmitPlanPage from "@/pages/internship/plan";
import { UserLoginPage } from "@/pages/user/login";
import { RegisterEditionPage } from "@/pages/edition/register";
import PickEditionPage from "@/pages/edition/pick";
import { isReadyMiddleware } from "@/middleware";
import { isLoggedInMiddleware, isReadyMiddleware } from "@/middleware";
type Route = {
name?: string;
@ -30,16 +30,16 @@ export function processMiddlewares<TArgs extends any[]>(middleware: Middleware<a
}
export const routes: Route[] = [
{ name: "home", path: "/", exact: true, content: () => <MainPage/>, middlewares: [ isReadyMiddleware ] },
{ name: "home", path: "/", exact: true, content: () => <MainPage/>, middlewares: [ isLoggedInMiddleware ] },
// edition
{ name: "edition_register", path: "/edition/register", exact: true, content: () => <RegisterEditionPage/> },
{ name: "edition_pick", path: "/edition/pick", exact: true, content: () => <PickEditionPage/> },
{ name: "edition_register", path: "/edition/register", exact: true, content: () => <RegisterEditionPage/>, middlewares: [ isLoggedInMiddleware ] },
{ name: "edition_pick", path: "/edition/pick", exact: true, content: () => <PickEditionPage/>, middlewares: [ isLoggedInMiddleware ] },
// internship
{ name: "internship_proposal", path: "/internship/proposal", exact: true, content: () => <InternshipProposalFormPage/>, middlewares: [ isReadyMiddleware ] },
{ name: "internship_proposal_preview", path: "/internship/preview/proposal", exact: true, content: () => <InternshipProposalPreviewPage/>, middlewares: [ isReadyMiddleware ] },
{ name: "internship_plan", path: "/internship/plan", exact: true, content: () => <SubmitPlanPage/> },
{ name: "internship_plan", path: "/internship/plan", exact: true, content: () => <SubmitPlanPage/>, middlewares: [ isReadyMiddleware ] },
// user
{ name: "user_login", path: "/user/login", content: () => <UserLoginPage/> },

View File

@ -1,4 +1,3 @@
import { Reducer } from "react";
import { UserAction, UserActions } from "@/state/actions/user";
export type UserState = {
@ -10,7 +9,7 @@ const initialUserState: UserState = {
loggedIn: false,
}
const userReducer: Reducer<UserState, UserAction> = (state = initialUserState, action) => {
const userReducer = (state: UserState = initialUserState, action: UserAction): UserState => {
switch (action.type) {
case UserActions.Login:
return {