feat: BaseApi 兩個 Token 檢查方法
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import EventCallbackFunction from "../typedef/EventCallbackFunction.ts"
|
import EventCallbackFunction from "../typedef/EventCallbackFunction.ts"
|
||||||
import ApiManager from "./ApiManager.ts"
|
import ApiManager from "./ApiManager.ts"
|
||||||
import { CallMethod } from './ApiDeclare.ts'
|
import { CallMethod } from './ApiDeclare.ts'
|
||||||
|
import User from "../data/User.ts"
|
||||||
|
import Token from "./Token.ts"
|
||||||
|
|
||||||
export default abstract class BaseApi {
|
export default abstract class BaseApi {
|
||||||
abstract getName(): string
|
abstract getName(): string
|
||||||
@@ -20,6 +22,16 @@ export default abstract class BaseApi {
|
|||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
checkUserToken(user: User, token: Token) {
|
||||||
|
if (!this.checkToken(token)) return false
|
||||||
|
if (token.author != user.bean.id) return false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
checkToken(token: Token) {
|
||||||
|
if (token.expired_time < Date.now()) return false
|
||||||
|
if (!User.findById(token.author)) return false
|
||||||
|
return true
|
||||||
|
}
|
||||||
registerEvent(name: CallMethod, func: EventCallbackFunction) {
|
registerEvent(name: CallMethod, func: EventCallbackFunction) {
|
||||||
if (!name.startsWith(this.getName() + ".")) throw Error("注冊的事件應該與接口集合命名空間相匹配: " + name)
|
if (!name.startsWith(this.getName() + ".")) throw Error("注冊的事件應該與接口集合命名空間相匹配: " + name)
|
||||||
ApiManager.addEventListener(name, func)
|
ApiManager.addEventListener(name, func)
|
||||||
|
|||||||
Reference in New Issue
Block a user