Advertisement
AhmadSayadi

Untitled

Aug 8th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. ---------------------------------------------------------------Schema---------------------------------------------------------
  2.  
  3. const mongoose = require('mongoose');
  4. const Schema = mongoose.Schema;
  5.  
  6. const webadminSchema = new Schema({
  7. accesswebadministrator: {type: mongoose.Schema.Types.ObjectId, ref: 'accesswebadministrator'},
  8. username: String,
  9. password: String,
  10. email: String,
  11. created_at: { type: Date, default: Date.now },
  12. modified_at: { type: Date, default: Date.now },
  13. deleted_at: Date,
  14. created_by: {type: String, default: 'Admin'},
  15. modified_by: {type: String, default: 'Admin'},
  16. deleted_by: String,
  17. flag: {type: Number, default: 1},
  18. });
  19.  
  20. const Webadmin = mongoose.model('webadministrator', webadminSchema);
  21.  
  22. module.exports = Webadmin;
  23.  
  24. ---------------------------------------------------------------Controller---------------------------------------------------------
  25.  
  26.  
  27.  
  28.  
  29. login: async (req, res, next) => {
  30. const result = await Webadministrator.findOne({ username: req.body.username, flag: 1 }).populate('accesswebadministrator', { _id: 0, created_at: 0, created_by: 0, modified_at: 0, modified_by: 0, flag: 0, name: 0 }, { flag: 1 });
  31. if (result === null) {
  32.  
  33. res.status(404).json({ status_code: -1, msg: 'Username / Password anda salah.' });
  34. } else {
  35. var cipher = aes256.createCipher(config.key);
  36. if (req.body.password === cipher.decrypt(result.password)) {
  37. log_action = "Login";
  38. var token = jwt.sign({ user: result }, config.key, { expiresIn: 7200 });
  39. var CryptoJS = require("crypto-js");
  40. var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(result.accesswebadministrator.accesslist), 'secret key 123');
  41. var userlogin = CryptoJS.AES.encrypt(JSON.stringify(result.username), 'secret key 123');
  42. var emaillogin = CryptoJS.AES.encrypt(JSON.stringify(result.email), 'secret key 123');
  43. await historyLog.findByIdAndUpdate(req.body.log_id, { $set: { action: log_action, output: 'Berhasil', user: result.username } });
  44. res.status(200).json({
  45. status_code: 1, msg: "Berhasil", token,
  46. akses: ciphertext.toString(),
  47. username: userlogin.toString(),
  48. email: emaillogin.toString(),
  49. });
  50. } else {
  51. res.status(404).json({ status_code: -1, msg: 'Username / Password anda salah.' });
  52. }
  53. }
  54. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement