[Token doesn’t work and the profile component doesn’t run. showing token not authenticated
router.post('/login', (req, res, next) => {
User.findOne({ email: req.body.email }, (err, user) => {
if (err) throw err;
if (!user) {
res.json({
success: false,
message: 'Authenticated failed, User not found'
});
} else if (user) {
var validPassword = user.comparePassword(req.body.password);
if (!validPassword) {
res.json({
success: false,
message: 'Authentication failed. Wrong password'
});
} else {
var token = jwt.sign({
user: user
},'123456', process.env.JWT_SECRET_KEY,{
expiresIn: '7d'
});
res.json({
success: true,
message: "Enjoy your token",
token: token
});
}
}
});
});
router.route('/profile')
.get(checkJWT, (req, res, next) => {
User.findOne({ _id: req.decoded.user._id }, (err, user) => {
res.json({
success: true,
user: user,
message: "Successful"
});
});
})
.post(checkJWT, (req, res, next) => {
User.findOne({ _id: req.decoded.user._id }, (err, user) => {
if (err) return next(err);
if (req.body.name) user.name = req.body.name;
if (req.body.email) user.email = req.body.email;
if (req.body.password) user.password = req.body.password;
user.isSeller = req.body.isSeller;
user.save();
res.json({
success: true,
message: 'Successfully edited your profile'
});
});
});
..................................................................................................................................................................................................................
<a *ngIf="token" (click)="collapse();" routerLink="/profile" class="dropdown-item">
<i class="fa fa-user-circle" aria-hidden="true"></i> Profile
</a>
<a *ngIf="!token" (click)="collapse();" routerLink="/login" class="dropdown-item">
<i class="fa fa-key" aria-hidden="true"></i> Login
</a>
Source: Angular Questions