Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------Schema---------------------------------------------------------
- const mongoose = require('mongoose');
- const Schema = mongoose.Schema;
- const webadminSchema = new Schema({
- accesswebadministrator: {type: mongoose.Schema.Types.ObjectId, ref: 'accesswebadministrator'},
- username: String,
- password: String,
- email: String,
- created_at: { type: Date, default: Date.now },
- modified_at: { type: Date, default: Date.now },
- deleted_at: Date,
- created_by: {type: String, default: 'Admin'},
- modified_by: {type: String, default: 'Admin'},
- deleted_by: String,
- flag: {type: Number, default: 1},
- });
- const Webadmin = mongoose.model('webadministrator', webadminSchema);
- module.exports = Webadmin;
- ---------------------------------------------------------------Controller---------------------------------------------------------
- login: async (req, res, next) => {
- 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 });
- if (result === null) {
- res.status(404).json({ status_code: -1, msg: 'Username / Password anda salah.' });
- } else {
- var cipher = aes256.createCipher(config.key);
- if (req.body.password === cipher.decrypt(result.password)) {
- log_action = "Login";
- var token = jwt.sign({ user: result }, config.key, { expiresIn: 7200 });
- var CryptoJS = require("crypto-js");
- var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(result.accesswebadministrator.accesslist), 'secret key 123');
- var userlogin = CryptoJS.AES.encrypt(JSON.stringify(result.username), 'secret key 123');
- var emaillogin = CryptoJS.AES.encrypt(JSON.stringify(result.email), 'secret key 123');
- await historyLog.findByIdAndUpdate(req.body.log_id, { $set: { action: log_action, output: 'Berhasil', user: result.username } });
- res.status(200).json({
- status_code: 1, msg: "Berhasil", token,
- akses: ciphertext.toString(),
- username: userlogin.toString(),
- email: emaillogin.toString(),
- });
- } else {
- res.status(404).json({ status_code: -1, msg: 'Username / Password anda salah.' });
- }
- }
- },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement