Here is my loginController:
var User = require('./../models/User.js');
var loginController = {};
loginController.match = function(req, res){
var username = req.body.username;
var password = req.body.password;
User.findOne({username : username, password : password})
.then(function(user){
if(user){
res.render('profile',{
title : 'Profile',
user : user
});
/*return res.status(200).json({
success : true,
data : user
});*/
}
else{
return res.status(200).json({
message : "Either username or password is wrong"
});
}
})
.catch(function(err){
return res.status(500).json({
message : err
})
});
}
module.exports = loginController;
After submitting the login credentials, the loginController runs the validation. If the validation is successful, it renders the profile.handlebars file successfully. However, there's an issue with consuming CSS that is served using express.static from a separate file (app.js).
Contents of app.js:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var hbs = require('express-handlebars');
var Path = require('path');
var routes = require('./routes.js');
var app = express();
/*View Engine*/
app.engine('handlebars',hbs({defaultLayout : 'layouts.handlebars', layoutsDir : __dirname + './../views/layouts'}));
app.set('views', Path.join(__dirname, './../views'));
app.set('view engine', 'handlebars');
/*Serve Static files*/
app.use(express.static(__dirname+'./../public'));
mongoose.connect('mongodb://localhost/tournament', function(){
console.log('connected to tournament..');
});
/*Template Routes*/
/*Login*/
app.get('/', function(req,res,next){
res.render('login',{
title : 'Login'
});
})
/*Profile
app.get('/profile',function(req,res,next){
res.render('profile',options)
});
*/
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({secret : "asdasdvbvc234234sdfsdf23213123", resave : false, saveUninitialized : true}));
app.use('/api', routes);
module.exports = app;
Contents of profile.handlebars:
<div class="row">
<div class="col-md-12">
<div class="float-right">
Hello, {{user.username}}
<button class="btn btn-md btn-danger">Logout</button>
</div>
</div>
</div>