Server IP : 162.213.251.212 / Your IP : 18.118.49.198 [ Web Server : LiteSpeed System : Linux business55.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : allssztx ( 535) PHP Version : 8.1.31 Disable Function : NONE Domains : 1 Domains MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/allssztx/needapair.com/routes/ |
Upload File : |
const express = require('express'); const router = express.Router(); const User = require('../schema/User') const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const JWT_SECRET = 'B!llHe$$'; const sendMail = require('./email/sendMail') const validateResetToken = require('../middleware/validateResetToken') // register a new user router.post('/register', async (req, res) => { try { var { firstName, lastName, email, password, confirmpassword, city, state, zip, ageverified, agreement } = req.body email = email.toLowerCase() // return console.log(req.body) if (!agreement || !ageverified) { return res.status(409).redirect('/register?error=' + encodeURIComponent('Check all the checkbox to proceed...')) } if (ageverified == 'on') { ageverified = true; } if (agreement == 'on') { agreement = true; } if (password !== confirmpassword) { return res.status(409).redirect('/register?error=' + encodeURIComponent('Password does\'nt match with Confirm Password...')) } const checkUser = await User.findOne({ email: email }) if (checkUser) { return res.status(409).redirect('/register?error=' + encodeURIComponent('Email Address already registered...')) } const salt = await bcrypt.genSalt(10) const hashPassword = await bcrypt.hash(password, salt) user = await User.create({ firstName, lastName, email, password: hashPassword, ageverified, agreement, city, state, zip }) res.status(200).redirect('/login?message=user created successfully...') const emailTable = `<table style="width: 100%; max-width: 500px"> <thead> <tr> <th style="padding: 5px 15px">Field</th> <th style="padding: 5px 15px">Value</th> </tr> </thead> <tbody> <tr><td style="padding: 5px 15px">First Name</td><td style="padding: 5px 15px">${firstName}</td></tr> <tr><td style="padding: 5px 15px">Last Name</td><td style="padding: 5px 15px">${lastName}</td></tr> <tr><td style="padding: 5px 15px">Email</td><td style="padding: 5px 15px">${email}</td></tr> <tr><td style="padding: 5px 15px">Password</td><td style="padding: 5px 15px">${password}</td></tr> <tr><td style="padding: 5px 15px">Age Verified</td><td style="padding: 5px 15px">${ageverified}</td></tr> <tr><td style="padding: 5px 15px">Agreement</td><td style="padding: 5px 15px">${agreement}</td></tr> <tr><td style="padding: 5px 15px">City</td><td style="padding: 5px 15px">${city}</td></tr> <tr><td style="padding: 5px 15px">State</td><td style="padding: 5px 15px">${state}</td></tr> <tr><td style="padding: 5px 15px">Zip</td><td style="padding: 5px 15px">${zip}</td></tr> </tbody> </table>` sendMail('info@needapair.com', 'New User Registered...', emailTable,"info") } catch (err) { console.log(err); return res.status(409).redirect('/register?error=' + encodeURIComponent(err.message)) } }) // login a user router.post('/login', async (req, res) => { try { const url = req.query.url console.log("url", url) let { email, password } = req.body email = email.toLowerCase() const checkUser = await User.findOne({ email: email }) if (checkUser) { const passwordCompare = await bcrypt.compare(password, checkUser.password); if (passwordCompare) { const { _id, firstName, lastName, email, ageverified, agreement, city, state, zip, verified, credits, role, status } = checkUser if(status == 'Deactive'){ return res.status(409).redirect('/contact-us?error=User for this email is Deactive contact Us!') } const user = { id: _id, firstName, lastName, email, ageverified, agreement, city, state, zip, verified, credits, role } const authtoken = jwt.sign(user, JWT_SECRET); const expirationDate = new Date(Date.now() + 15 * 60 * 1000); // 15 minutes in milliseconds if (url) { return res.status(200).cookie('authtoken', authtoken, { expires: expirationDate }).redirect(url + '?message=User Logged In Successfully...') } else if(role == "admin") { return res.status(200).cookie('authtoken', authtoken, { expires: expirationDate }).redirect('/admin/dashboard?message=Admin Logged In Successfully...') }else{ return res.status(200).cookie('authtoken', authtoken, { expires: expirationDate }).redirect('/send-now?message=User Logged In Successfully...') } } else { return res.status(409).redirect('/login?error=Invalid Credentials') } } else { return res.status(409).redirect('/login?error=Invalid Credentials') } } catch (err) { console.log(err); return res.status(409).redirect('/login?error=' + encodeURIComponent(err.message)) } }) // edit a user router.post('/update', async (req, res) => { try { const url = req.query.url const { firstName, lastName, city, state, zip, password } = req.body const checkUser = await User.findById(req.user.id) // return console.log(checkUser) if (checkUser) { const passwordCompare = await bcrypt.compare(password, checkUser.password); if (passwordCompare) { checkUser.firstName = firstName checkUser.lastName = lastName checkUser.city = city checkUser.state = state checkUser.zip = zip await checkUser.save() const user = { id: checkUser._id, firstName: checkUser.firstName, lastName: checkUser.lastName, email: checkUser.email, ageverified: checkUser.ageverified, agreement: checkUser.agreement, city: checkUser.city, state: checkUser.state, zip: checkUser.zip, verified: checkUser.verified, credits: checkUser.credits } const authtoken = jwt.sign(user, JWT_SECRET); return res.status(200).cookie('authtoken', authtoken, { expires: expirationDate }).redirect('/send-now?message=Accounts Details Changed...') } else { return res.status(409).redirect('/send-now?error=Invalid Password') } } else { return res.status(409).redirect('/login?error=Invalid Credentials') } } catch (err) { console.log(err); return res.status(409).redirect('/send-now?error=' + encodeURIComponent(err.message)) } }) // change password router.post('/change-password', async (req, res) => { try { const { password, newPassword, confirmPassword } = req.body const checkUser = await User.findById(req.user.id) // return console.log(checkUser) if (checkUser) { const passwordCompare = await bcrypt.compare(password, checkUser.password); if (passwordCompare) { if (newPassword == confirmPassword) { const salt = await bcrypt.genSalt(10) const hashPassword = await bcrypt.hash(newPassword, salt) checkUser.password = hashPassword await checkUser.save() return res.status(200).redirect('/send-now?message=Password Changed...!') } return res.status(200).redirect('/send-now?message=New Password & Confirm Password should be same...') } else { return res.status(409).redirect('/send-now?error=Invalid Password') } } else { return res.status(409).redirect('/login?error=Invalid Credentials') } } catch (err) { console.log(err); return res.status(409).redirect('/send-now?error=' + encodeURIComponent(err.message)) } }) // logout a user router.get('/logout', async (req, res) => { res.clearCookie('authtoken') return res.status(200).redirect('/?message=User Logged Out...') }) // reset-password router.post('/reset-password', async (req, res) => { try { const to = req.body.email const user = await User.findOne({ email: to }) if (!user) { return res.redirect('/login?error="User not Found"') } const verificationToken = jwt.sign({ email: to }, JWT_SECRET, { expiresIn: '5M' }); const subject = 'Reset Password' const html = `<table role="presentation" style="width: 100%; border-collapse: collapse; border: 0px; border-spacing: 0px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(239, 239, 239);"> <tbody> <tr> <td align="center" style="padding: 1rem 2rem; vertical-align: top; width: 100%;"> <table role="presentation" style="max-width: 600px; border-collapse: collapse; border: 0px; border-spacing: 0px; text-align: left;"> <tbody> <tr> <td style="padding: 40px 0px 0px;"> <div style="padding: 20px; background-color: rgb(255, 255, 255);"> <div style="color: rgb(0, 0, 0); text-align: left;"> <img src="https://needapair.com/images/dashboard-logo.png" alt="Company" style="width: 60px;"> <h1 style="margin: 1rem 0">Final step...</h1> <p style="padding-bottom: 16px">Follow this link to Reset your Password... </p> <p style="padding-bottom: 16px"> <a href="https://needapair.com/new-password?token=${verificationToken}" style="padding: 12px 24px; border-radius: 4px; color: #FFF; background: #2B52F5;display: inline-block;margin: 0.5rem 0;">Reset Password</a> <br /> or click link below <br /> <a href="https://needapair.com/new-password?token=${verificationToken}">https://needapair.com/new-password?token=${verificationToken}</a> </p> <p style="padding-bottom: 16px">If you didn’t ask to reset your password, you can ignore this email.</p> <p style="padding-bottom: 16px">Thanks,<br> Bill Hess</p> </div> </div> <div style="padding-top: 20px; color: rgb(153, 153, 153); text-align: center;"> <p style="padding-bottom: 16px">Bill Hess</p> </div> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table>` // res.send(html) // Send the email sendMail(to, subject, html, "noreply").then((success) => { if (success) { return res.status(200).redirect('/login?message=Email Send to ' + to) } else { console.log('error from mailsend') return res.status(200).redirect('/login?error=Something went wrong...') } }) } catch (err) { return res.status(200).redirect('/login?error=' + err.message) } }) router.post('/new-password', validateResetToken, async (req, res) => { try { const { email, password, confirmpassword } = req.body; if (password != confirmpassword && password.length < 6) { return res.status(200).redirect('/login?error=password not valid...') } const user = await User.findOne({ email }) if (!user) { return res.status(200).redirect('/login?error=User not Found...') } const salt = await bcrypt.genSalt(10) const hashPassword = await bcrypt.hash(password, salt) user.password = hashPassword await user.save(); return res.status(200).redirect('/login?message=Passord Changed...') } catch (err) { return res.status(200).redirect('/login?error=' + err.message) } }) router.post('/add-to-cart', async (req, res) => { try { const { info } = req.body; const tempUser = req.user if(tempUser){ const cartData = JSON.parse(Buffer.from(info, 'base64').toString('utf-8')) const user = await User.findById(tempUser.id) user.cart.push(cartData) await user.save(); return res.status(200).redirect('/cart?message=Item has beed added to cart') } return res.status(200).redirect('/login?message=Login to Purchase') } catch (err) { return res.status(200).redirect('/login?error=' + err.message) } }) router.post('/remove-from-cart', async (req, res) => { try { const { ind } = req.body; const tempUser = req.user if(tempUser){ const user = await User.findById(tempUser.id) user.cart.splice(ind,1) await user.save(); if(user.cart.length > 0){ return res.status(200).redirect('/cart?message=Item has beed removed from cart') }else{ return res.status(200).redirect('/cart?message=Item has beed removed from cart') } } return res.status(200).redirect('/login?message=Login to Purchase') } catch (err) { return res.status(200).redirect('/login?error=' + err.message) } }) module.exports = router