-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: AuthUser 데코레이터 추가, 장소 컨트롤러에 가드 적용 #81
- Loading branch information
Showing
2 changed files
with
42 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { AuthenticationException } from './exception/AuthenticationException'; | ||
|
||
export const AuthUser = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext) => { | ||
const requestingUser = ctx.switchToHttp().getRequest().user; | ||
if (!requestingUser) { | ||
throw new AuthenticationException('인증 정보가 없습니다.'); | ||
} | ||
|
||
if (!isAuthUser(requestingUser)) { | ||
throw new AuthenticationException('유효하지 않은 인증 정보입니다.'); | ||
} | ||
|
||
return requestingUser as AuthUser; | ||
}, | ||
); | ||
|
||
export interface AuthUser { | ||
userId: string; | ||
role: string; | ||
} | ||
|
||
function isAuthUser(obj: any): obj is AuthUser { | ||
return ( | ||
typeof obj === 'object' && | ||
obj !== null && | ||
typeof obj.userId === 'number' && | ||
typeof obj.role === 'string' | ||
); | ||
} |
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