controllers/userController.js
/**
   * Handles user login.
   *
   * Retrieves user email and password from the request body.
   *
   * User details are queried from data store, submitted password is hashed and
   * compared with stored password.
   *
   * If authentication is successful json-web-token is set and 200 reply is
   * sent.
   *
   * If authentication is unsuccessful then response is 402 with error message.
   *
   * @param {fastify.Request} req Fastify request instance.
   * @param {fastify.Reply} rep Fastify reply instance.
   */
Handles user login.
Retrieves user email and password from the request body.

User details are queried from data store, submitted password is hashed and
compared with stored password.

If authentication is successful json-web-token is set and 200 reply is
sent.

If authentication is unsuccessful then response is 402 with error message.

@param {fastify.Request} req Fastify request instance.
@param {fastify.Reply} rep Fastify reply instance.
Handles user login . Retrieves user email and password from the request body . User details are queried from data store submitted password is hashed and compared with stored password . If authentication is successful json - web - token is set and 200 reply is sent . If authentication is unsuccessful then response is 402 with error message . @param { fastify . Request } req Fastify request instance . @param { fastify . Reply } rep Fastify reply instance .
static async handleUserAuthentication(req, rep) {
    const email = req.body.email;
    const password = req.body.password;
    const user = await this.models.user.findOne({
      where: {
        email: email
      }
    });
    if(user === null) {
      return await rep.code(402).send(new APIError({
        source: { parameter: 'email' },
        title: 'Account Not Found',
        detail: 'No account exists associated with that email.'
      }));
    }
    const passwordValid = await bcrypt.compare(password, user.password.toString());
    if(passwordValid) {
      const token = await rep.jwtSign({
        id: user.id,
        email: user.email
      });
      const serializedUser = UserSerializer.serialize(user);
      await rep.setCookie('token', token, {
        path: '/',
        httpOnly: true,
        sameSite: true
      }).code(200).send(serializedUser);
    } else {
      await rep.code(402).send(new APIError({
        source: { parameter: 'password' },
        title: 'Incorrect Password',
        detail: 'The password provided was incorrect.'
      }));
    }
  }
static async handleUserAuthentication ( req , rep ) { const email = req . body . email ; const password = req . body . password ; const user = await this . models . user . findOne ( { where : { email : email } } ) ; if ( user === null ) { return await rep . code ( 402 ) . send ( new APIError ( { source : { parameter : 'email' } , title : 'Account Not Found' , detail : 'No account exists associated with that email.' } ) ) ; } const passwordValid = await bcrypt . compare ( password , user . password . toString ( ) ) ; if ( passwordValid ) { const token = await rep . jwtSign ( { id : user . id , email : user . email } ) ; const serializedUser = UserSerializer . serialize ( user ) ; await rep . setCookie ( 'token' , token , { path : '/' , httpOnly : true , sameSite : true } ) . code ( 200 ) . send ( serializedUser ) ; } else { await rep . code ( 402 ) . send ( new APIError ( { source : { parameter : 'password' } , title : 'Incorrect Password' , detail : 'The password provided was incorrect.' } ) ) ; } }
Handles user login.
Handles user login .