Page 417 - MDP2022-2
P. 417

return  {
                        access_token:  this.jwtService.sign(payload),
                    };
                }


                async  signup(user:  PostSignupDTO)  {
                    if  (await  this.usersService.isExist(user.userId))  {
                        throw  new  HttpException('User  already  exists',  400);
                    }
                    const  salt  =  await  bcrypt.genSalt();
                    const  hashedPassword  =  await  bcrypt.hash(user.password,  salt);
                    this.usersService.create({
                        userId:  user.userId,
                        name:  user.name,
                        nickname:  user.nickname,
                        email:  user.email,
                        password:  hashedPassword,
                    });
                    return  true;
                }
            }




            src/auth/constants.ts
            export  const  jwtConstants  =  {
                secret:  '1!2@3#4$5%6^7&8*9(0)_-+=',
            };
            src/auth/strategies/local.strategy.ts
            import  {  Injectable,  UnauthorizedException  }  from  '@nestjs/common';
            import  {  PassportStrategy  }  from  '@nestjs/passport';
            import  {  Strategy  }  from  'passport-local';
            import  {  AuthService  }  from  '../auth.service';


            @Injectable()
            export  class  LocalStrategy  extends  PassportStrategy(Strategy)  {
                constructor(private  authService:  AuthService)  {
                    super({
                        usernameField:  'userId',
                    });
                }


                async  validate(username:  string,  password:  string):  Promise<any>  {
                    const  user  =  await  this.authService.validateUser(username,  password);
                    if  (!user)  {
                        throw  new  UnauthorizedException();
   412   413   414   415   416   417   418   419   420   421   422