Add isLoggedIn middleware
This commit is contained in:
parent
379acad8c5
commit
ff2e9c8b82
@ -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") } />;
|
||||
}
|
||||
|
@ -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/> },
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user