Currently, I am encountering issues with user authentication in my template. Oddly enough, everything works fine when testing with Postman. Firstly, I initiate an API call to /users/authenticate to retrieve a token. Next, I verify the token by making another API call to /users/me. Fortunately, this process is successful in allowing users to log in.
Here's a snippet from my controller:
.controller('homeCtrl', ['$rootScope', '$location', '$http', '$timeout', 'Auth', function ($rootScope, $location, $http, $timeout, Auth) {
$rootScope.loadme = false;
$rootScope.currentPa = $location.path();
$rootScope.$on('$routeChangeStart', function () {
if (Auth.isLoggedIn()) {
console.log('Success: User is logged in');
$rootScope.isLoggedIn = true;
Auth.getUser()
.then(function (data) {
$rootScope.username = data.data.username;
$rootScope.email = data.data.email;
$rootScope.loadme = true;
});
} else {
$rootScope.isLoggedIn = false;
$rootScope.username = '';
$rootScope.loadme = true;
}
});
$rootScope.doLogin = function (loginData) {
$rootScope.loading = true;
$rootScope.errorMsg = false;
console.log("TEST LOGIN");
Auth.doLogin($scope.loginData)
.then(function (data) {
console.log('logging in');
if (data.data.success) {
console.log('fgsgsg');
$rootScope.loading = false;
$rootScope.successMsg = data.data.message + 'Redirecting...';
$timeout(function () {
console.log('12345');
$location.path('/#!/');
$rootScope.loginData = '';
$rootScope.successMsg = false;
}, 2000);
} else {
console.log("no success");
$rootScope.loading = false;
$rootScope.errorMsg = data.data.message;
}
})
};
Below are the server routes and middleware:
//User Login Route
router.post('/authenticate', function (req, res) {
User.findOne({username: req.body.username})
.select('username email password')
.exec(function (err, user) {
if (err) {
throw err;
}
if (!user) {
res.json({success: false, message: 'Could not authenticate
user'});
} else if (user) {
if (req.body.password) {
var validPassword =
user.comparePassword(req.body.password);
}
else {
res.status(200).json({success: false, message: 'Please
provide password'})
}
if (!validPassword) {
res.status(200).json({success: false, message: 'Could
not authenticate password'});
} else {
var token = jwt.sign({username: user.username, email:
user.email}, secret, {expiresIn: '24h'});
res.status(200).json({success: true, message: 'User
authenticated', token: token});
}
}
console.log(user);
});
});
//Middleware for decoding tokens
router.use(function (req, res, next) {
var token = req.body.token || req.body.query || req.headers['x-access-
token'];
if (token) {
jwt.verify(token, secret, function (err, decoded) {
if (err) {
res.json({success: false, message: 'Token Invalid'})
} else {
req.decoded = decoded;
next();
}
});
} else {
res.json({success: false, message: 'No token provided'});
}
});
router.post('/me', function (req, res) {
res.send(req.decoded);
});
Lastly, presented below is the template structure:
<div ng-controller="homeCtrl"> <form ng-submit="doLogin(loginData)"> <label>Username:</label> <input class="form-control" type="text" name="username" placeholder="please enter username" ng-model="loginData.username"> <br> <label>Email:</label> <input class="form-control" type="text" name="email" placeholder="please enter email" ng-model="loginData.email"> <br> <label>Password:</label> <input class="form-control" type="password" name="password" placeholder="please enter password" ng-model="loginData.password"> <br> <button class="btn btn-primary" type="submit" formmethod="post">Login</button> </form>