Greetings everyone! I am currently developing a web application using meteorjs. Thanks to backbone, I have successfully implemented a multipage feature in my web app. Take a look at my router.js code below:
Router = {
uri: _.compact(window.location.pathname.split("/")),
routes: [],
addRoute: function(route, template, session, currTemplateType) {
var segments = _.compact(route.split("/"));
var placeholders = _.reduce(segments, function(currentArr, piece, index) {
if (piece.substr(0, 1) === ":") {
currentArr.push(index);
segments[index] = piece.substr(1);
}
return currentArr;
}, []);
this.routes.push({
segments: segments,
template: template,
placeholderIndexes: placeholders,
session : session,
currTemplateType : currTemplateType
});
},
getMatchingRoute: function(){
for (var i in this.routes) {
var route = this.routes[i];
var data = {};
if (route.segments.length === this.uri.length) {
var match = _.every(route.segments, function(seg, i){
if (_.contains(route.placeholderIndexes, i)) {
data[seg] = this.uri[i];
return true;
} else {
return seg === this.uri[i];
}
}, this);
if (match) {
return {
data: data,
template: route.template,
session: route.session,
currTemplateType: route.currTemplateType
}
}
}
}
//no matches (add 404 or default template maybe?)
return false;
},
run: function(){
var route = this.getMatchingRoute();
if (route) {
var fragment = Meteor.render(function() {
if (Template[route.template] !== undefined) {
return Template[route.template](route.data);
}
});
Session.set(SessionLookUp.pageByURL, route.session);
Session.set(SessionLookUp.currentTemplateType, route.currTemplateType);
if(route.currTemplateType !== TemplateType.login){
var isLog = "true";
if(isLog === undefined || isLog === "false")
window.location.href = "/cust/login";
else{
document.body.appendChild(fragment);
}
}
else{
document.body.appendChild(fragment);
}
} else {
var fragment = Meteor.render(function() {
return Template["404_page"](route.data);
});
document.body.appendChild(fragment);
}
}
};
And here are some snippets from my pager.js:
Router.addRoute('/cust/login', 'login', UserType.customer, TemplateType.login);
Router.addRoute('/cust/register','cust_reg', UserType.customer, TemplateType.register);
Router.addRoute('/cust/profile', 'cust_profile', UserType.customer,"");
In my scenario, when a user logs in at localhost:3000/cust/login and the username and password are verified, the system will redirect them to localhost:3000/cust/profile. You can see how I navigate the page in the code snippet below:
Session.set(SessionLookUp.isLoggedIn, "true");
window.location.href = "/cust/profile";
However, there seems to be an issue where the session becomes null or undefined after the above code executes. Can anyone shed some light on why this is happening and provide a solution? I suspect the problem may lie in how I handle the page redirection. By the way, I've created a custom login form.