-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
95 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
import { ExecutionContext, createParamDecorator } from '@nestjs/common'; | ||
import { ExecutionContext, SetMetadata, UseGuards, applyDecorators, createParamDecorator } from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
|
||
import { UserDocument } from '../user/user.model'; | ||
|
||
export const User = createParamDecorator( | ||
function getRequestUserDecorator(data: void, ctx: ExecutionContext): UserDocument { | ||
const request = ctx.switchToHttp().getRequest(); | ||
import { AuthGuard } from './auth.guard'; | ||
|
||
return request.user; | ||
export type AcceptedUserTypes = 'web' | 'plugin' | 'any'; | ||
|
||
export function Auth(usertype: AcceptedUserTypes = 'any') { | ||
return applyDecorators( | ||
SetMetadata('usertype', usertype), | ||
UseGuards(AuthGuard), | ||
); | ||
} | ||
|
||
export const User = createParamDecorator<AcceptedUserTypes | undefined>( | ||
function getRequestUserDecorator(data: AcceptedUserTypes | undefined, ctx: ExecutionContext): UserDocument | undefined { | ||
const request = ctx.switchToHttp().getRequest<Request>(); | ||
|
||
if (data === 'web') { | ||
return request.webUser; | ||
} | ||
if (data === 'plugin') { | ||
return request.pluginUser; | ||
} | ||
|
||
return request.user || request.webUser || request.pluginUser; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import User from '@/shared/interfaces/user.interface'; | ||
import { UserDocument } from './user/user.model'; | ||
|
||
declare global { | ||
namespace Express { | ||
interface Request { | ||
user?: User; | ||
webUser?: UserDocument; | ||
pluginUser?: UserDocument; | ||
user?: UserDocument; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters