support publicRoutes & improve no_session handling

This commit is contained in:
srgooglo 2022-10-08 23:54:56 +02:00
parent 258d2cfff6
commit e3edad8a40
4 changed files with 38 additions and 19 deletions

View File

@ -0,0 +1,3 @@
[
"/post/*"
]

View File

@ -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: <antd.Button type="primary" onClick={() => app.goAuth()}>Login</antd.Button>,
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: <antd.Button type="primary" onClick={() => app.goAuth()}>Login</antd.Button>,
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
}

View File

@ -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 <div />
}
window.location.href = config.app?.authPath ?? "/login"
return <div />
}
}
if (this.state.renderError) {
if (this.props.staticRenders?.RenderError) {
return React.createElement(this.props.staticRenders?.RenderError, { error: this.state.renderError })

View File

@ -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 <div />
}
window.app.setLocation(config.app?.mainPath ?? "/home")
return <div />
}