'Server can not perform JWT Authentication Request on MERN
I was working on a MERN app and was trying to use jason web-token (JWT) and bcrypt for authentication. It uses two routes for signup and authentication. However, I'm getting an internal server error coming from admin route not sure though. I have not been able to connect to the frontend correctly because of that.
//admin model
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const Joi = require('joi');
const passwordComplexity = require('joi-password-complexity');
const AdminSchema = new mongoose.Schema({
name: { type: String, required:true },
email: { type: String, required:true, unique:true },
password: { type: String, required:true, },
});
AdminSchema.methods.generateAuthToken = function(){
const token=jwt.sign({_id:this._id}, process.env.JWTPRIVATEKEY,{expiresIn: "7d",});
return token;
}
const AdminModel = mongoose.model("admin", AdminSchema)
const validate=(data) =>{
const schema= Joi.object({
name:Joi.string().required().label("Name"),
email:Joi.string().email().required.label("Email"),
password:passwordComplexity().required().label("Password")
});
return schema.validate(data)
};
module.exports = {AdminModel, validate};
//module.exports = AdminModel;
//admin route
const router= require("express").Router();
const {AdminModel, validate} = require("../models/admin");
const bcrypt = require("bcrypt");
router.post("/", async(req,res) =>{
try {
const {error} = validate(req.body);
if(error)
return res.status(400).send({message: error.details[0].message});
const user = await AdminModel.findOne({email: req.body.email});
if(user)
return res.status(409).send({message:"Admin with given email exists"});
const salt = await bcrypt.genSalt(Number(process.env.SALT));
const hashPassword = await bcrypt.hash(req.body.password, salt);
await new AdminModel({ ...req.body, password:hashPassword}).save();
res.status(201).send({message:"Admin created successfully"})
} catch (error) {
res.status(500).send({message:"Internal Server Error YOu see"})
}
})
module.exports = router;
//adminAuthentication route
const router = require("express").Router();
const {AdminModel} = require("../models/admin");
const bcrypt = require("bcrypt");
const Joi = require("joi");
router.post("/", async (req, res) => {
try {
const { error } = validate(req.body);
if (error)
return res.status(400).send({ message: error.details[0].message });
const Admin = await AdminModel.findOne({ email: req.body.email });
if (!Admin)
return res.status(401).send({ message: "Invalid Email or Password" });
const validPassword = await bcrypt.compare(
req.body.password,
Admin.password
);
if (!validPassword)
return res.status(401).send({ message: "Invalid Email or Password" });
const token = Admin.generateAuthToken();
res.status(200).send({ data: token, message: "logged in successfully" });
} catch (error) {
res.status(500).send({ message: "Internal Server Error" });
}
});
const validate = (data) => {
const schema = Joi.object({
email: Joi.string().email().required().label("Email"),
password: Joi.string().required().label("Password"),
});
return schema.validate(data);
};
module.exports = router;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
