mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
support publicRoutes & improve no_session handling
This commit is contained in:
parent
258d2cfff6
commit
e3edad8a40
3
packages/app/constants/publicRoutes.json
Normal file
3
packages/app/constants/publicRoutes.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[
|
||||||
|
"/post/*"
|
||||||
|
]
|
@ -133,6 +133,17 @@ class App extends React.Component {
|
|||||||
panel: true,
|
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 () => {
|
"session.logout": async () => {
|
||||||
await this.sessionController.logout()
|
await this.sessionController.logout()
|
||||||
},
|
},
|
||||||
@ -366,21 +377,6 @@ class App extends React.Component {
|
|||||||
await this.initialization()
|
await this.initialization()
|
||||||
|
|
||||||
app.eventBus.emit("app.initialization.finish")
|
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 () => {
|
initialization = async () => {
|
||||||
@ -457,7 +453,7 @@ class App extends React.Component {
|
|||||||
const token = await Session.token
|
const token = await Session.token
|
||||||
|
|
||||||
if (!token || token == null) {
|
if (!token || token == null) {
|
||||||
app.eventBus.emit("app.forceToLogin")
|
app.eventBus.emit("app.no_session")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import progressBar from "nprogress"
|
|||||||
import config from "config"
|
import config from "config"
|
||||||
|
|
||||||
import routes from "schemas/routes"
|
import routes from "schemas/routes"
|
||||||
|
import publicRoutes from "schemas/publicRoutes"
|
||||||
|
|
||||||
import Layouts from "layouts"
|
import Layouts from "layouts"
|
||||||
|
|
||||||
@ -100,6 +101,26 @@ export default class Layout extends React.Component {
|
|||||||
let layoutType = this.state.layoutType
|
let layoutType = this.state.layoutType
|
||||||
const InitializationComponent = this.props.staticRenders?.Initialization ? React.createElement(this.props.staticRenders.Initialization) : null
|
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.state.renderError) {
|
||||||
if (this.props.staticRenders?.RenderError) {
|
if (this.props.staticRenders?.RenderError) {
|
||||||
return React.createElement(this.props.staticRenders?.RenderError, { error: this.state.renderError })
|
return React.createElement(this.props.staticRenders?.RenderError, { error: this.state.renderError })
|
||||||
|
@ -2,13 +2,12 @@ import React from "react"
|
|||||||
import config from "config"
|
import config from "config"
|
||||||
|
|
||||||
export default (props) => {
|
export default (props) => {
|
||||||
const isLogged = typeof props.user === "object"
|
if (!props.session) {
|
||||||
|
|
||||||
if (!isLogged) {
|
|
||||||
window.app.setLocation(config.app?.authPath ?? "/login")
|
window.app.setLocation(config.app?.authPath ?? "/login")
|
||||||
return <div />
|
return <div />
|
||||||
}
|
}
|
||||||
|
|
||||||
window.app.setLocation(config.app?.mainPath ?? "/home")
|
window.app.setLocation(config.app?.mainPath ?? "/home")
|
||||||
|
|
||||||
return <div />
|
return <div />
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user