How to get user's information in Nest.js?

前端 未结 3 2068
小鲜肉
小鲜肉 2021-01-20 22:56

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).

3条回答
  •  遥遥无期
    2021-01-20 23:45

    If you got totally different approach in mind, let me know - will try to help.

    Will try to serve a bit more detailed example, which includes passport under the hood. It assumes that passport is used and that Authorization token is being sent.

    • const RegisteredPassportModule = PassportModule.register({ defaultStrategy: 'bearer' })
    • adding HttpStrategy to some AuthModule
    • adding PassportModule.register({ defaultStrategy: 'bearer' }) to imports to AuthModule

    Then:

    AuthService is a service (and a part of AuthModule) that allows to find given user via token sent via token passed via Authorization header, directly from the database.

    import { Strategy } from 'passport-http-bearer';
    import { PassportStrategy } from '@nestjs/passport';
    import { Injectable, UnauthorizedException } from '@nestjs/common';
    import { AuthService } from './auth.service';
    
    @Injectable()
    export class HttpStrategy extends PassportStrategy(Strategy) {
      constructor(private readonly authService: AuthService) {
        super()
      }
    
      async validate(token: string) {
        const user = await this.authService.findUserByToken(token);
        if (!user) {
          throw new UnauthorizedException();
        }
        return user;
      }
    }
    
    

    Usage is after all quite simple (you can put the guard for either method of controller):

    @UseGuards(AuthGuard())
    @Get()
    someMethod(@Req() request: RequestWithUser, ...) {
      // ...
    }
    
    

    Where RequestWithUser is just:

    import { User as UserEntity } from '../../models/user.entity'
    
    export type RequestWithUser = Request & { user: UserEntity }
    

    and the /user endpoint would be just returning request.user

    I hope this helps!

提交回复
热议问题