Advertisement
Guest User

Untitled

a guest
Jul 4th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *  @description Default Imports
  3.  */
  4.  
  5. import { Request, Response } from 'express';
  6. import chalk from 'chalk';
  7.  
  8. /**
  9.  *  @description Schema Imports
  10.  */
  11.  
  12. import { Credentials } from './../schemas/credentials.schema';
  13.  
  14. export class AuthenticationController {
  15.     public authenticate(req: Request, res: Response) {
  16.         try {
  17.             Credentials.authenticate()(req.body.username, req.body.password, (err: any, user: any, error: any) => {
  18.                 if (error) {
  19.                     switch (error.name) {
  20.                         case 'IncorrectUsernameError':
  21.                             {
  22.                                 res.status(422).json({ message: error.message });
  23.                             }
  24.                             break;
  25.                         case 'IncorrectPasswordError':
  26.                             {
  27.                                 res.status(422).json({ message: error.message });
  28.                             }
  29.                             break;
  30.                         default:
  31.                             break;
  32.                     }
  33.                 } else {
  34.                     res.status(200).json({ status: 200, message: 'Authentication Successfull!', admin: user.admin });
  35.                 }
  36.             });
  37.         } catch (error) {
  38.             console.error(chalk.redBright(error));
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement