getAuthUser becomes async.

This commit is contained in:
John Livingston 2021-05-05 15:55:38 +02:00
parent 07353bd327
commit 3658ee797a
4 changed files with 11 additions and 7 deletions

View File

@ -84,12 +84,15 @@ enum UserRole {
USER = 2
}
interface MUserAccountUrl { // FIXME: this interface is not complete
interface MUserDefault { // FIXME: this interface is not complete
id?: string
username: string
email: string
blocked: boolean
role: UserRole
Account: {
name: string
}
}
interface VideoBlacklistCreate {
@ -128,7 +131,7 @@ interface PeerTubeHelpers {
}
// Added in Peertube 3.2.0
user?: {
getAuthUser: (res: express.Response) => MUserAccountUrl | undefined
getAuthUser: (res: express.Response) => Promise<MUserDefault | undefined>
}
}

View File

@ -23,8 +23,8 @@ function getBaseStaticRoute (): string {
// Peertube <= 3.1.0 has no way to test that current user is admin
// Peertube >= 3.2.0 has getAuthUser helper
function isUserAdmin (options: RegisterServerOptions, res: Response): boolean {
const user = getAuthUser(options, res)
async function isUserAdmin (options: RegisterServerOptions, res: Response): Promise<boolean> {
const user = await getAuthUser(options, res)
if (!user) {
return false
}
@ -40,7 +40,8 @@ function isUserAdmin (options: RegisterServerOptions, res: Response): boolean {
// Peertube <= 3.1.0 has no way to get user informations.
// This is a hack.
// Peertube >= 3.2.0 has getAuthUser helper
function getAuthUser ({ peertubeHelpers }: RegisterServerOptions, res: Response): MUserAccountUrl | undefined {
async function getAuthUser (options: RegisterServerOptions, res: Response): Promise<MUserDefault | undefined> {
const peertubeHelpers = options.peertubeHelpers
if (peertubeHelpers.user?.getAuthUser) {
return peertubeHelpers.user.getAuthUser(res)
}

View File

@ -89,7 +89,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
router.get('/auth', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
const user = getAuthUser(options, res)
const user = await getAuthUser(options, res)
if (!user) {
res.sendStatus(403)
return

View File

@ -24,7 +24,7 @@ async function initSettingsRouter (options: RegisterServerOptions): Promise<Rout
res.sendStatus(403)
return
}
if (!isUserAdmin(options, res)) {
if (!await isUserAdmin(options, res)) {
res.sendStatus(403)
return
}