I am using Angular+Nest to develop a website. I have created a service(Angular) so that client can get user's information from server when project start up(the same as fresh). Some actions don't need to login, so the login is optional.
What I want is if user has logined, then client should send a request to get user's information.
Server code as below:
export const RequestUser = createParamDecorator((data, req): RequestUserDTO => {
return req.user;
});
@Controller('auth')
export class AuthController {
@Get('getUserInfoByToken')
async getUserInfoByToken(@RequestUser() user: User): Promise<any> {
if (user) {
return {
nickname: user.nickname,
level: user.level
};
}
}
}
Howerver, I find there's nothing be return if I don't add @UseGuards(AuthGuard())
as decorator. But if I add it, when project start, this request return 401
as status code. Then the web will turn to login page.
What should I do to avoid this situation? Not every action need login.