mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 19:06:40 +00:00
Split auth handlers and sanitize authentication logs
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
const helper = require('../../helpers/helper');
|
||||
const winston = require('../../helpers/winston.logger');
|
||||
const { validationResult } = require('express-validator');
|
||||
const model = require('./auth.model');
|
||||
const bcrypt = require('bcrypt');
|
||||
const moment = require('moment');
|
||||
|
||||
// Registrasi terminal menyimpan hash password setelah validasi terminal.
|
||||
const register = async (req, res) => {
|
||||
let uniqueCode = req.id ?? 'unknown';
|
||||
try {
|
||||
uniqueCode = await helper.getUniqueCode();
|
||||
let result = {};
|
||||
|
||||
const { mid, tid, password } = req.body;
|
||||
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ REQUEST REGISTER ]`
|
||||
);
|
||||
|
||||
// validate
|
||||
const err = validationResult(req);
|
||||
if (!err.isEmpty()) {
|
||||
result = {
|
||||
code: '400',
|
||||
message: err.errors[0].msg,
|
||||
data: {},
|
||||
};
|
||||
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} [ RESPONSE REGISTER ] code=${result.code}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// check mid dan tid yang di request sudah terdaftar di master terminal sistem CMS atau belum dan status nya aktif
|
||||
const checkMidTid = await model.checkMidTid(mid, tid);
|
||||
if (!checkMidTid) {
|
||||
result = {
|
||||
code: '410',
|
||||
message: 'MID or TID is not registered.',
|
||||
data: {},
|
||||
};
|
||||
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} [ RESPONSE REGISTER ] code=${result.code}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// check data login, apaka mid dan tid yang di request sudah terdaftar di terminal login atau belum
|
||||
const checkDataLogin = await model.checkDataLogin(mid, tid);
|
||||
|
||||
// jika mid dan tid sudah terdaftar di terminal login
|
||||
if (checkDataLogin) {
|
||||
result = {
|
||||
code: '411',
|
||||
message: 'Terminal Login already exists.',
|
||||
data: {},
|
||||
};
|
||||
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} [ RESPONSE REGISTER ] code=${result.code}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// encrypt password
|
||||
const saltRounds = 10;
|
||||
const salt = bcrypt.genSaltSync(saltRounds);
|
||||
const passwordHash = bcrypt.hashSync(password, salt);
|
||||
|
||||
// binding data
|
||||
const data = {
|
||||
c_mid: mid,
|
||||
c_tid: tid,
|
||||
c_password: password,
|
||||
c_password_hash: passwordHash,
|
||||
c_status: 'A',
|
||||
c_status_name: 'ACTIVE',
|
||||
d_create: moment().format('YYYY-MM-DD HH:mm:ss'),
|
||||
i_create: null,
|
||||
c_create_username: null,
|
||||
c_create_fullname: null,
|
||||
};
|
||||
|
||||
// insert data login
|
||||
result = await model.insertDataLogin(data);
|
||||
|
||||
result = {
|
||||
code: '00',
|
||||
message: 'Data login saved successfully.',
|
||||
data: result.command,
|
||||
};
|
||||
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ RESPONSE REGISTER ] code=${result.code}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
winston.logger.error(
|
||||
`${uniqueCode} [ RESPONSE REGISTER ] code=500 error_type=${error?.name || 'Error'}`
|
||||
);
|
||||
|
||||
return res.status(200).json({
|
||||
code: '500',
|
||||
message:
|
||||
process.env.NODE_ENV != 'production'
|
||||
? error.message
|
||||
: '500 internal server error - backend server.',
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { register };
|
||||
Reference in New Issue
Block a user