- Engineering
- Computer Science
- so far i have sweet alerts producing errors when a...
Question: so far i have sweet alerts producing errors when a...
Question details
So far I have sweet alerts producing errors when a user has an error with their username/ password.
Now I want to send a sweet alert message saying 'Registration Successful' after a correct registration before it directs to home page.
here is my code: what do I need to add to alert this message?
authorisation.js:
/*API to register user*/
router.post('/register', function(req, res, next){
var username = req.body.user_name;
var password = req.body.password;
//Check if account already exists
User.findOne({ 'user_name' : username }, function(err, user)
{
if (err)
res.send(err);
//Check to see if theres already a user with that username
if (user) {
res.status(401).json({
"status": "error",
"body": "Username already taken"
});
} else {
//If there is no user with that username create the user
var newUser = new User();
//Set the user's local credentials
newUser.user_name = username;
newUser.password = newUser.generateHash(password);
newUser.access_token = createJwt({user_name:username});
newUser.save(function(err, user) {
if (err)
throw err;
res.cookie('Authorization', 'Bearer ' +
user.access_token);
res.json({'Success' : 'Account succesfully registered'});
});
}
});
});
users.js
$(document).ready(
function() {
/*Event handler for when the user attempts to register */
$("#reg-form").submit(function (event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: '/users/register',
dataType: 'json',
data: {
'user_name': event.target.inputUsername.value,
'password': event.target.inputPassword.value
},
success: function(token){
$(location).attr('href', '/homepage' );
},
error: function(errMsg) {
swal(
'Error...',
errMsg.responseJSON.body,
'error'
)
}
});
}); });
Solution by an expert tutor
