Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.4 kB
2
Indexable
Never
const express = require('express');

const bcrypt = require('bcrypt');

const User = require('../models/users');

const multer = require('multer');


const router = express.Router();



//Register Endpoint 
router.post('/register' , async(req ,res)=>{
    try {
        const data = req.body;

        data.name = data.firstName + " " + data.lastName;

        const salt = bcrypt.genSaltSync(10);

        const cryptedPass = await bcrypt.hashSync(data.password , salt);

        data.password = cryptedPass;

        const user = new User(data);

        const savedData = await user.save();

        
        res.status(200).send({
            "status": "success",
            "message": "User registered successfully",
            "data": {
                "name": (savedData.name)[1],
                "email": savedData.email,
                "phone": savedData.phone
              }
            
        });
    } catch (error) {
        res.status(400).send({
            "status": "error",
            "message": error

        })
    }

})

// Login Endpoint
router.post('/login' ,async(req , res)=>{
    try {
        const data = req.body;
        
        const user =await User.findOne({ email : data.email});
        
        if(user) {
            const valiPassword = bcrypt.compareSync(data.password , user.password);

            if (valiPassword) {
                res.send({
                    "status": "success",
                    "message": "User logged in successfully",
                    "data": {
                        "userId": user._id,
                        "email": user.email

                    }

                })
            } else {
                res.send({
                    "status" : "invalide",
                    "message" : "Invalide email or password"
                })
            }
        } else {
            res.send({
                "status" : "not found",
                "message" : "user not found"
            })
        }

    } catch (error) {
        res.send({
            "status" : "error",
            "message" : error
        })
    }
})

//User Profile Update Endpoint

var filename = '';

const mystorage = multer.diskStorage({
    destination : './public/images/users',
    filename : (req , file , redirect)=>{
        let date = Date.now();

        let fl = date + '.' + file.mimetype.split('/')[1];

        redirect(null , fl)

        filename = fl;
    }
})

const updload = multer({storage : mystorage});


router.patch('/updateProfile/:id' , updload.any('image') , async(req , res)=>{
    try {
        const myid = req.params.id;

        const newdata = req.body;

        if((newdata.firstName != undefined) & (newdata.lastName != undefined)){
            newdata.name = newdata.firstName + ' ' + newdata.lastName;
        }

        newdata.profileImage = filename;

        await User.findByIdAndUpdate({_id : myid} , newdata);

        

        res.send({
            "status": "success",
            "message": "Profile updated successfully"
        })
    } catch (error) {
        res.send({
            "status": "error",
            "message": error
        })
    }
})

// Password Change Endpoint

router.post('/changePassword/:id' , async(req , res)=>{
    try {
        const myid = req.params.id;

        const user = await User.findById({_id : myid});

        const newdatapass = req.body;

        const checkPass = bcrypt.compareSync(newdatapass.currentPassword , user.password);
        if (checkPass) {
            const salt = bcrypt.genSaltSync(10);

            const genNewPass = await bcrypt.hashSync(newdatapass.newPassword , salt);

            await User.findByIdAndUpdate({_id : myid} , {password : genNewPass});

            res.send({
                "status": "success",
                "message": "Password changed successfully"
            })
        } else {
            res.send({
                'status' : 'Invalide',
                'message' : 'Your password are not nvalide try again'
            });
        }
    } catch (error) {
        res.send({
            "status": "error",
            "message": error

        })
    }
})


module.exports = router;
Leave a Comment