smilejakdu / node-watcha

0 stars 0 forks source link

login Missing credentials #2

Open smilejakdu opened 3 years ago

smilejakdu commented 3 years ago

Missing credentials

users tables

nickname , password , createdAt , updatedAt

회원가입은 정상적으로 되었다.

router.post('/login', async (req, res, next) => {
  passport.authenticate('local', (err: Error, user: User, info: { message: string }) => {
    console.log("req.body: ", req.body); // { nickname: 'ash3', password: '123123' }
    if (err) {
        console.error(err);
        return next(err);
    }
    console.log("info : " , info); // { message: 'Missing credentials' }
    if (info) {
        return res.status(401).send(info.message);
    }

이부분에서 if(info) 분기문에 타고 들어가게 된다 . 에러메세지를 읽어보면 Missing credentials 로 나온다.

import * as passport from 'passport';
import * as bcrypt from 'bcrypt';
import { Strategy } from 'passport-local';

import User from '../models/user';

export default () => {
  passport.use('local', new Strategy({ // IStrategyOptions
    nicknameField: 'nickname',
    passwordField: 'password',
  }, async (nickname, password, done) => { // VerifyFunction
    console.log("local nickname : " , nickname);
    console.log("local password : " , password); 
    try {
      const user = await User.findOne({ where: { nickname } });
      if (!user) {
        return done(null, false, { message: '존재하지 않는 사용자입니다!' });
      }
      const result = await bcrypt.compare(password, user.password);
      if (result) {
        return done(null, user);
      }
      return done(null, false, { message: '비밀번호가 틀립니다.' });
    } catch (err) {
      console.error(err);
      return done(err);
    }
  }))
};

postman 으로 테스트 localhost:3065/users/login

body : { "nickname" : "ash", "password" : "123123" }

response = > Missing credentials

smilejakdu commented 3 years ago

node_modules/@types/passport-local 에 들어있는

// Type definitions for passport-local 1.0.0
// Project: https://github.com/jaredhanson/passport-local
// Definitions by: Maxime LUCE <https://github.com/SomaticIT>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

/// <reference types="passport"/>

import { Strategy as PassportStrategy } from "passport-strategy";
import express = require("express");

interface IStrategyOptions {
    usernameField?: string;
    passwordField?: string;
    passReqToCallback?: false;
}

interface IStrategyOptionsWithRequest {
    usernameField?: string;
    passwordField?: string;
    passReqToCallback: true;
}

interface IVerifyOptions {
    message: string;
}

interface VerifyFunctionWithRequest {
    (
        req: express.Request,
        username: string,
        password: string,
        done: (error: any, user?: any, options?: IVerifyOptions) => void
    ): void;
}

interface VerifyFunction {
    (
        username: string,
        password: string,
        done: (error: any, user?: any, options?: IVerifyOptions) => void
    ): void;
}

declare class Strategy extends PassportStrategy {
    constructor(
        options: IStrategyOptionsWithRequest,
        verify: VerifyFunctionWithRequest
    );
    constructor(options: IStrategyOptions, verify: VerifyFunction);
    constructor(verify: VerifyFunction);

    name: string;
}

이부분에 있는 usernameField 와

import * as passport from 'passport';
import * as bcrypt from 'bcrypt';
import { Strategy } from 'passport-local';

import User from '../models/user';

export default () => {
  passport.use('local', new Strategy({ // IStrategyOptions
    usernameField: 'nickname',
    passwordField: 'password',
  }, async (nickname, password, done) => { // VerifyFunction
    console.log("local nickname : " , nickname);
    console.log("local password : " , password); 
    try {
      const user = await User.findOne({ where: { nickname } });
      console.log("user : " , user);
      if (!user) {
        return done(null, false, { message: '존재하지 않는 사용자입니다!' });
      }
      const result = await bcrypt.compare(password, user.password);
      if (result) {
        return done(null, user);
      }
      return done(null, false, { message: '비밀번호가 틀립니다.' });
    } catch (err) {
      console.error(err);
      return done(err);
    }
  }))
};

에 있는 usernameField 를 둘다 nickname 으로 바꿔주면 안될까 ?? 해서 바꿨는데 이거때문에 되지않았다.