Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.   routes: {
  3.     'POST /user/login': async function (req, res) {
  4.       const { login, password } = req.body
  5.       const responseBody = await this.controllers.userLogin(login, password)
  6.       return res(responseBody {
  7.         type: 'application/json',
  8.         headers: [
  9.           'Api-Version': '1.0.1',
  10.         ]
  11.       })
  12.     }
  13.   },
  14.  
  15.   controllers: {
  16.     userLogin: async function (login, password) {
  17.       const userLoginDto = new UserLoginDto(login, password)
  18.       const requestBody = await this.services.useLoginService(userLoginDto)
  19.       return requestBody
  20.     }
  21.   }
  22.  
  23.   services: {
  24.     userLoginService: async function (userLoginDto) {
  25.       const sessionToken = await this.repositories.user.validateCredentials(userLoginDto)
  26.       return sessionToken
  27.     }
  28.   }
  29.  
  30.   repositories: {
  31.     user: {
  32.       validateCredentials: async function (login, password) {
  33.         const { login, password } = userLoginDto
  34.         const user = await this.entities.userEntity.findOne({ username: login })
  35.         if (user && user.methods.validatePassword(password)) {
  36.           return {
  37.             token: user.getSessionToken(login, password)
  38.           }
  39.         } else {
  40.           return null
  41.         }
  42.       }
  43.     }
  44.   }
  45.  
  46.   entities: {
  47.     userEntity: {
  48.       schema: {
  49.         id: {
  50.           type: number,
  51.           relation: 'Column'
  52.         },
  53.         likes: {
  54.           type: array,
  55.           relation: 'OneToMany'
  56.           relations: ...
  57.         },
  58.         password...
  59.         salt...
  60.       },
  61.       methods: {
  62.         validatePassword: async function (password) {
  63.           const hash = await bcrypt.hash(password, this....schema.salt)
  64.         }
  65.         return hash === this.[Symbol].schema.password
  66.       }
  67.     }
  68.   }
  69. }
  70.  
  71. // dariuszsikorski.pl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement