diff --git a/packages/app/constants/publicRoutes.json b/packages/app/constants/publicRoutes.json new file mode 100644 index 00000000..07e32b95 --- /dev/null +++ b/packages/app/constants/publicRoutes.json @@ -0,0 +1,3 @@ +[ + "/post/*" +] \ No newline at end of file diff --git a/packages/app/src/App.jsx b/packages/app/src/App.jsx index 161e197c..9f37821f 100644 --- a/packages/app/src/App.jsx +++ b/packages/app/src/App.jsx @@ -133,6 +133,17 @@ class App extends React.Component { panel: true, }) }, + "app.no_session": async () => { + const location = window.location.pathname + + if (location !== "/login" && location !== "/register") { + antd.notification.info({ + message: "You are not logged in, to use some features you will need to log in.", + btn: app.goAuth()}>Login, + duration: 15, + }) + } + }, "session.logout": async () => { await this.sessionController.logout() }, @@ -366,21 +377,6 @@ class App extends React.Component { await this.initialization() app.eventBus.emit("app.initialization.finish") - - // post initialization - - // check if user is navigating outside login, advise to login - if (!this.state.user) { - const location = window.location.pathname - - if (location !== "/login" && location !== "/register") { - antd.notification.info({ - message: "You are not logged in, to use some features you will need to log in.", - btn: app.goAuth()}>Login, - duration: 15, - }) - } - } } initialization = async () => { @@ -457,7 +453,7 @@ class App extends React.Component { const token = await Session.token if (!token || token == null) { - app.eventBus.emit("app.forceToLogin") + app.eventBus.emit("app.no_session") return false } diff --git a/packages/app/src/layout.jsx b/packages/app/src/layout.jsx index 6fefd89c..50a73ebf 100644 --- a/packages/app/src/layout.jsx +++ b/packages/app/src/layout.jsx @@ -5,6 +5,7 @@ import progressBar from "nprogress" import config from "config" import routes from "schemas/routes" +import publicRoutes from "schemas/publicRoutes" import Layouts from "layouts" @@ -100,6 +101,26 @@ export default class Layout extends React.Component { let layoutType = this.state.layoutType const InitializationComponent = this.props.staticRenders?.Initialization ? React.createElement(this.props.staticRenders.Initialization) : null + const currentRoute = window.location.pathname + + if (!this.props.user && currentRoute !== config.app?.authPath && currentRoute !== "/") { + const isPublicRoute = publicRoutes.some((route) => { + const regex = new RegExp(route.replace("*", ".*")) + return regex.test(currentRoute) + }) + + if (!isPublicRoute) { + if (typeof window.app.setLocation === "function") { + window.app.setLocation(config.app?.authPath ?? "/login") + return
+ } + + window.location.href = config.app?.authPath ?? "/login" + + return
+ } + } + if (this.state.renderError) { if (this.props.staticRenders?.RenderError) { return React.createElement(this.props.staticRenders?.RenderError, { error: this.state.renderError }) diff --git a/packages/app/src/pages/index.jsx b/packages/app/src/pages/index.jsx index 28dff12a..7c04ba89 100644 --- a/packages/app/src/pages/index.jsx +++ b/packages/app/src/pages/index.jsx @@ -2,13 +2,12 @@ import React from "react" import config from "config" export default (props) => { - const isLogged = typeof props.user === "object" - - if (!isLogged) { + if (!props.session) { window.app.setLocation(config.app?.authPath ?? "/login") return
} window.app.setLocation(config.app?.mainPath ?? "/home") + return
} \ No newline at end of file