/* ============================================================================ *\
|| ########################################################################## ||
|| # Auction Software Marketplace Release: 0.6 Build 0.7 # ||
|| # ---------------------------------------------------------------------- # ||
|| # License # 35YAHCNR9344X6O666C123AB # ||
|| # ---------------------------------------------------------------------- # ||
|| # Copyright ©2014–2021 Develop Scripts LLC. All Rights Reserved # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ------------- AUCTION SOFTWARE IS NOT FREE SOFTWARE ------------------ # ||
|| # http://www.auctionsoftwaremarketplace.com|support@auctionsoftware.com # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */
const express = require('express')
const config = require('config').get('JwtToken')
const fs = require('fs')
const morgan = require('morgan')
const path = require('path')
const jwt = require('jsonwebtoken')
const user = require('./routes/user')
const product = require('./routes/product')
const bid = require('./routes/bid')
const common = require('./routes/common')
const cart = require('./routes/cart')
const buynow = require('./routes/buynow')
const appointment = require('./routes/appointment')
const credit = require('./routes/credit')
const offline = require('./routes/offline')
const returnProd = require('./routes/return')
const refund = require('./routes/refund')
const payment = require('./routes/payment')
const invoice = require('./routes/invoice')
const deposit = require('./routes/deposit')
const auction = require('./routes/auction')
const video = require('./routes/video')
const paymentThird = require('./routes/payment/index')
const shipping = require('./routes/shipping')
const tax = require('./routes/tax')
const chartmetric = require('./routes/chartmetric')
const ttwBid = require('./routes/ttwBid')
const commonFunction = require('../common/function').default
// const { CustomStatusError } = require('../../middleware/custom_error')
// const checkIPValidation = require('../../middleware/ip_whitelist');
const { accessLogStream, jsonResponse } = require('./controllers/logger')
// const checkip = new checkIPValidation();
const app = express.Router()
const logDirect = path.join(__dirname, '../../../../public/logs/api/error')
if (!fs.existsSync(logDirect)) {
fs.mkdirSync(logDirect)
}
app.use(
morgan(':remote-addr - :remote-user [:date[web]] ":method :url HTTP/:http-version" :reqbody', {
immediate: true,
stream: accessLogStream,
}),
)
app.use(
morgan(':status :res[content-length] - :response-time ms"', {
immediate: false,
stream: accessLogStream,
}),
)
app.use(
morgan(
':remote-addr - :remote-user [:date[web]] ":method :url HTTP/:http-version" :status :res[header] :req[header] - :response-time ms ":referrer" ":user-agent" :reqbody',
{
stream: fs.createWriteStream(
path.join(__dirname, '../../../../public/logs/api/error/access.log'),
{ flags: 'a' },
),
skip(req, res) {
return res.statusCode < 400
},
},
),
)
const NotAuthenticated = async (req, res, next) => {
const bearerHeader = req.headers.authorization
if (typeof bearerHeader !== 'undefined') {
const bearer = bearerHeader.split(' ')
const bearerToken = bearer[1]
try {
const decoded = await jwt.verify(bearerToken, config.get('secret'))
req.token = bearerToken
const [results] = await Promise.all([commonFunction.userDetails(decoded.id)])
const [userValue] = results
req.user = userValue
return next()
} catch (err) {
console.log('login error', err)
jsonResponse(res, 'error', {
responseType: 403,
message: 'Session timed out!',
})
return false
}
// finally {
// let ipvalidated = checkip.checkIpValidation(req);
// if(ipvalidated[0]){
// next();
// } else {
// throw new CustomStatusError('IP '+ipvalidated[1]+' is not whitelisted', 403);
// }
// }
} else {
return next()
}
}
const Authenticated = async (req, res, next) => {
const bearerHeader = req.headers.authorization
if (typeof bearerHeader !== 'undefined') {
const bearer = bearerHeader.split(' ')
const bearerToken = bearer[1]
try {
const decoded = await jwt.verify(bearerToken, config.get('secret'))
req.token = bearerToken
const [results] = await Promise.all([commonFunction.userDetails(decoded.id)])
const [userValue] = results
req.user = userValue
return next()
} catch (err) {
console.log('login error', err)
jsonResponse(res, 'error', {
responseType: 403,
message: 'Session timed out!',
})
return false
}
// finally {
// let ipvalidated = checkip.checkIpValidation(req);
// if(ipvalidated[0]){
// next();
// } else {
// throw new CustomStatusError('IP '+ipvalidated[1]+' is not whitelisted', 403);
// }
// }
} else if (req.body.authenticate) {
try {
if (req.originalUrl === '/api/user/updateProfile') {
const [results] = await Promise.all([commonFunction.userDetails(req.body.user_id)])
const [userValue] = results
req.user = userValue
return next()
}
jsonResponse(res, 'error', {
responseType: 403,
message: 'No Bearer Token Available!',
})
return false
} catch (err) {
console.log('login error', err)
jsonResponse(res, 'error', {
responseType: 403,
message: 'Session timed out!',
})
return false
}
} else if (
req.originalUrl === '/api/user/checkValidation' ||
req.originalUrl === '/api/user/login' ||
req.originalUrl === '/api/user/register' ||
req.originalUrl === '/api/user/forgotPassword' ||
req.originalUrl === '/api/user/resetPassword' ||
req.originalUrl === '/api/user/sendPhoneVerifyCode' ||
req.originalUrl === '/api/user/reSendPhoneVerifyCode' ||
req.originalUrl === '/api/user/verifyPhoneVerifyCode' ||
req.originalUrl === '/api/common/getDefault' ||
req.originalUrl === '/api/auction/search' ||
req.originalUrl === '/api/common/getStaticPage' ||
req.originalUrl === '/api/user/sendEmailVerifyLink' ||
req.originalUrl === '/api/user/verifyEmail' ||
req.originalUrl === '/api/user/submitContactUs' ||
req.originalUrl === '/api/video/videoStream' ||
req.originalUrl === '/api/video/newVideoStream' ||
req.originalUrl === '/api/payment/rosoom_response'
) {
return next()
} else {
jsonResponse(res, 'error', {
responseType: 403,
message: 'No Bearer Token Available!',
})
return false
}
}
/**
* Operations for Frontend.
*
* @namespace frontend
*/
app.use('/user', Authenticated, user)
app.use('/product', NotAuthenticated, product)
app.use('/bid', Authenticated, bid)
app.use('/ttwbid', Authenticated, ttwBid)
app.use('/common', Authenticated, common)
app.use('/cart', Authenticated, cart)
app.use('/buynow', Authenticated, buynow)
app.use('/appointment', Authenticated, appointment)
app.use('/credit', Authenticated, credit)
app.use('/offline', Authenticated, offline)
app.use('/return', Authenticated, returnProd)
app.use('/refund', Authenticated, refund)
app.use('/payment', Authenticated, payment)
app.use('/invoice', Authenticated, invoice)
app.use('/deposit', Authenticated, deposit)
app.use('/auction', Authenticated, auction)
app.use('/video', Authenticated, video)
app.use('/paymentThird', NotAuthenticated, paymentThird)
app.use('/shipping', Authenticated, shipping)
app.use('/tax', Authenticated, tax)
app.use('/chartmetric', chartmetric)
module.exports = app