You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
806 B
26 lines
806 B
import CommonError from '../error/CommonError'
|
|
import AuthError from '../error/AuthError'
|
|
|
|
export default function RouteAuth(options = {}) {
|
|
const { auth = true } = options
|
|
return async (ctx, next) => {
|
|
// 当 auth 为 false 时,已登录用户不能访问
|
|
if (auth === false) {
|
|
if (ctx.state.user) {
|
|
throw new CommonError("该接口不能登录查看")
|
|
}
|
|
return await next()
|
|
}
|
|
|
|
// 当 auth 为 true 时,必须登录才能访问
|
|
if (auth === true) {
|
|
if (!ctx.state.user) {
|
|
throw new AuthError("该接口必须登录查看")
|
|
}
|
|
return await next()
|
|
}
|
|
|
|
// 其他自定义模式(如角色检查等)
|
|
return await next()
|
|
}
|
|
}
|
|
|