Advertisement
masquitos

Untitled

Oct 19th, 2020
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ROUTER
  2.  
  3. router.route("/").post(authRouter);
  4.  
  5. async function authRouter(req, res) {
  6.   let authData = req.body;
  7.   let body;
  8.   const myLogger = new MyLogger(req.reqId, fileName);
  9.   try {
  10.     myLogger.log("Авторизация: " + authData.email);
  11.     const authLogic = new AuthLogic(authData, req.reqId, res.locals.role);
  12.     const token = await authLogic.getToken();
  13.     myLogger.log("Успешаня авторизация: " + authData.email);
  14.     body = requester.createBody({ token });
  15.   } catch (error) {
  16.     myLogger.myErrorLogger(error);
  17.     body = requester.createError(error);
  18.   }
  19.  
  20.   res.json(body);
  21. }
  22.  
  23. module.exports = router;
  24.  
  25. //LOGIC
  26. /**
  27.  *
  28.  * @param authData
  29.  * @param logId
  30.  * @param role
  31.  */
  32. class AuthLogic {
  33.   constructor(authData, logId, role) {
  34.     this.authData = authData;
  35.     this.myLogger = new MyLogger(logId, this.constructor.name);
  36.     this.role = role;
  37.   }
  38.  
  39.   validate() {
  40.     try {
  41.       this.myLogger.debug("validate");
  42.       const isEmail = validator.isEmail(this.authData.email);
  43.       const isEmailLength = validator.isLength(this.authData.email, {
  44.         min: myConstants.MIN_EMAIL_LENGTH,
  45.         max: myConstants.MAX_EMAIL_LENGTH
  46.       });
  47.  
  48.       const isPasswordLength = validator.isLength(this.authData.password, {
  49.         min: myConstants.MIN_PASSWORD_LENGTH,
  50.         max: myConstants.MAX_PASSWORD_LENGTH
  51.       });
  52.       if (!isEmail || !isEmailLength || !isPasswordLength) {
  53.         throw new MyError(
  54.           errorCodes.INVALID_DATA,
  55.           this.authData.email +
  56.             " isEmail:" +
  57.             isEmail +
  58.             " isEmailLength:" +
  59.             isEmailLength +
  60.             " isPasswordLength:" +
  61.             isPasswordLength
  62.         );
  63.       }
  64.     } catch (e) {
  65.       if (e.message === "Expected string but received a number.") {
  66.         e = new MyError(errorCodes.INVALID_DATA, e.message);
  67.       }
  68.       this.myLogger.myErrorLogger(e);
  69.       throw e;
  70.     }
  71.   }
  72.  
  73.   async checkIsUserExist() {
  74.     try {
  75.       this.myLogger.debug("checkIsUserExist");
  76.       const dbUser = await db_helper.getUserByEmail(this.authData.email);
  77.       if (!dbUser) {
  78.         console.log("Не существует юзера", this.authData.email);
  79.         throw new MyError(
  80.           errorCodes.INVALID_USER_OR_PASSWORD,
  81.           this.authData.email
  82.         );
  83.       }
  84.       this.user = myParser.parseUser(dbUser);
  85.     } catch (e) {
  86.       this.myLogger.myErrorLogger(e);
  87.       throw e;
  88.     }
  89.   }
  90.  
  91.   checkRole() {
  92.     this.myLogger.debug("checkRole");
  93.     if (this.user.role !== this.role) {
  94.       const e = new MyError(
  95.         errorCodes.INVALID_USER_ROLE,
  96.         "role:" + this.user.role
  97.       );
  98.       this.myLogger.myErrorLogger(e);
  99.       throw e;
  100.     }
  101.   }
  102.  
  103.   async comparePassword() {
  104.     try {
  105.       this.myLogger.debug("comparePassword");
  106.       const isSuccess = await bcrypt.compare(
  107.         this.authData.password,
  108.         this.user.password
  109.       );
  110.       if (!isSuccess) {
  111.         throw new MyError(
  112.           errorCodes.INVALID_USER_OR_PASSWORD,
  113.           "isSuccess:" + isSuccess
  114.         );
  115.       }
  116.     } catch (e) {
  117.       this.myLogger.myErrorLogger(e);
  118.       throw e;
  119.     }
  120.   }
  121.  
  122.   async updateLastEnter() {
  123.     try {
  124.       this.myLogger.debug("updateLastEnter");
  125.       const dbUser = await db_helper.updateUserIdentity(this.user.id);
  126.       this.user = myParser.parseUser(dbUser);
  127.     } catch (e) {
  128.       this.myLogger.myErrorLogger(e);
  129.       throw e;
  130.     }
  131.   }
  132.  
  133.   async getToken() {
  134.     this.myLogger.debug("getToken");
  135.     this.validate();
  136.     await this.checkIsUserExist();
  137.     this.checkRole();
  138.     await this.comparePassword();
  139.     await this.updateLastEnter();
  140.     return tokenGenerator.getToken(this.user);
  141.   }
  142. }
  143.  
  144. module.exports = AuthLogic;
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement