I have a authentication API that response a token when i make a login request(http://localhost:3001/api/users/login).
//Login
router.post('/login', async (req, res) => {
//Validate data before we use
const { error } = loginValidation(req.body);
if (error) return res.status(400).send(error.details[0].message)
//Checking if the email exists
const user = await User.findOne({
email: req.body.email
})
if (!user) return res.status(400).send('Email or password dont match')
//Password is correct
const validPass = await bcrypt.compare(req.body.password, user.password)
if (!validPass) return res.status(400).send('Invalid password')
//Create and assign a token
const token = jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET);
res.header('auth-token', token).send(token)
})
I want to take that data from const token in my front end in local storage using Jquery. So i register my user.
$('#button-click').click(function () {
$.ajax(
{
type: "POST",
url: 'http://localhost:3001/api/user/register',
data: JSON.stringify({
"name": $("#name").val(),
"email": $("#email").val(),
"password": $("#password").val()
}),
error: function (e) {
console.log(e)
},
contentType: "application/json; charset=utf-8",
dataType: "json",
}
)
})
but i dont know how to take that data, can anyone help me ?