Uname: 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
Software: LiteSpeed
PHP version: 8.1.32 [ PHP INFO ] PHP os: Linux
Server Ip: 162.213.251.212
Your Ip: 18.119.112.17
User: allssztx (535) | Group: allssztx (533)
Safe Mode: OFF
Disable Function:
NONE

name : email.js
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const JWT_SECRET = "E4d4U$er";
const User = require('../../schema/User')

router.post('/', async (req, res) => {
    try {
        const {
            fullName,
            email,
            password,
            confirmPassword,
            phone
        } = req.body
        let errors = []
        if (!fullName) {
            errors.push(`Full Name is Invalid or not Filled`)
        }
        if (!email) {
            errors.push(`Email is Invalid or not Filled`)
        }
        if (!password) {
            errors.push(`Password is Invalid or not Filled`)
        }
        if (errors.length == 0) {
            if (password !== confirmPassword) {
                return res.status(409).json({
                    error: "Password & Confirm Should be same"
                })
            }
            const checkUser = await User.findOne({ 'primaryEmail.email': email })
            const salt = await bcrypt.genSalt(10)
            const hashPassword = await bcrypt.hash(password, salt)
            if (checkUser) {
                if (checkUser.primaryEmail.provider.some(provider => provider.type === 'google') || checkUser.primaryEmail.provider.some(provider => provider.type === 'facebook')) {
                    checkUser.fullName = fullName
                    checkUser.password = hashPassword
                    checkUser.phone = phone
                    await checkUser.save()
                    const user = {
                        id: checkUser._id, primaryEmail: {
                            email: email,
                            verified: false
                        }
                    }
                    const authtoken = jwt.sign(user, JWT_SECRET);
                    return res.status(200).json({
                        success: true,
                        message: "Registeration Completed",
                        authtoken
                    })
                }
                return res.status(409).json({
                    success: false,
                    error: "Email Already Registered"
                })
            }
            newUser = await User.create({
                fullName,
                password: hashPassword,
                primaryEmail: {
                    email: email,
                    verified: false
                },
                phone
            })
            const fetchUser = await User.findOne({ 'primaryEmail.email': email })
            const { _id, primaryEmail } = fetchUser
            const user = {
                id: _id, primaryEmail
            }
            const authtoken = jwt.sign(user, JWT_SECRET);
            return res.status(200).cookie('authtoken', authtoken).cookie('e4d4user', true).json({
                success: true,
                message: "Registeration Completed",
                authtoken
            })
        }
        else {
            console.log(req.body)
            return res.status(422).json({
                success: false,
                error: "Fill all Required Fields",
                errors
            })
        }
    }
    catch (err) {
        console.log(err.message);
        return res.status(500).json({
            message: err.message
        })
    }
})

module.exports = router
© 2025 GrazzMean-Shell