I am trying to use iron router to route the html pages on click events.But as I run the application, all pages appear in the main page itself, whereas the click event doesn't route the page needed.The JS code as below
Template.Login.events({
'submit form': function(){
event.preventDefault();
var username = $('[name=userId]').val();
var password = $('[name=password]').val();
if(username == '' && password ==''){
alert("Please fill all fields...!!!!!!");
}
else if( username =='deal' && password ==''){
alert("You are logged in as a Deal manager");
Meteor.Router.to("/dashboard");
}
else if( username =='practice' && password ==''){
alert("You are logged in as a Practice manager");
Meteor.Router.to("/pracDashboard");
}
}
});
As in the above is a login page with conditions as 'deal' or 'practice' to route to different pages.The moment I place the dashboard.html in the folder, both the login and dashboard page appear in the same window.
For displaying a page we should convert the html page in template form. Let say login.html is as follows
<html>
<title>Sample title</title>
<body>Sample Text</body>
</html>
for meteor the html page should be like this
<template name="login">
Sample Text
</template>
Then for displaying login page we need to implement Route like this
Router.route('/login', function (){
this.render('login.html');
});
So set Router first then in events during submit redirect.
Router.route('/dashboard', function (){
this.render('dashboard.html');
});
Router.route('/pracDashboard', function (){
this.render('pracDashboard.html');
});
Router.route('/login', function (){
this.render('login.html');
});
Template.login.events({
'submit form': function(){
event.preventDefault();
var username = $('[name=userId]').val();
var password = $('[name=password]').val();
if(username == '' && password =='') {
alert("Please fill all fields...!!!!!!");
} else if( username =='deal' && password =='') {
alert("You are logged in as a Deal manager");
Router.go("/dashboard");
} else if( username =='practice' && password ==''){
alert("You are logged in as a Practice manager");
Router.go("/pracDashboard");
}
}
});
Related
I'm using Meteor useraccounts:bootstrap.
On my navbar i have btnLogin and btnLogOut. I want only my login button to show when im logged out, and only the logout button when im logged in.
My logic:
if (!error) {
if (state === "signIn") {
document.getElementById('btnLogin').style.display = 'none';
}
else if (state != 'signIn') {
document.getElementById('btnLogOut').style.display = 'visible';
}
How do i get my app to look for state without triggering an event/click?
Thank you!
If you're logged in, Meteor.userId() will return the _id of the current user.
So you can have something like this:
page.html:
{{#if isUserLoggedIn}}
<<<<<show logout button >>>>>>
{{else}}
<<<<<show login button >>>>>>
{{/if}}
page.js
Template.page.helper({
isUserLoggedIn: function(){
if(Meteor.userId()){
return true;
}
return false;
}
});
i'm trying to display Logged in User name .
After login i can get user name but once i refresh page it is undefined .
My code :
<p>{{currentUser.profile.full_name}}</p>
i try other way too
blaze Side
{{currentUser}}
js
Template.main.helpers({
"currentUser": function() {
if (Meteor.user())
return Meteor.user().profile.full_name;
}
});
after login i can get name but i can not get name after refresh page.
So, what is solution for get name if i refresh page in Meteor blaze ?
Try this:
Template.Default.onCreated(function() {
this.user = Meteor.user();
});
Template.main.helpers({
currentUser: function() {
var user = Template.instance().user;
if (user) {
return user.profile.full_name;
}
});
Also make sure that profile.full_name exists;
I created a collection for adminuser's enter the system.I mean I dont want to use account packet for admin side but I dont know How to make Route setting after admin to be login.I made something but it doesnt work correct,
login.html
Template.login.events({
'click #entre': function (e, template) {
var Username = template.$('#username').val();
var Password = template.$('#password').val();
var getinfo= admin.findOne({});
if (Username == " " || Password == "") {
swal("Error", "All fields must be Completed.", "error");
} else if (getinfo.username== Username && getinfo.password== Password) {
swal("Success", "Welcome admin.", "success");
Session.set("hi", true);
} else {
swal("error", "Login Informations wrong.", "error");
}
}
});
router.js
Router.onBeforeAction(function () {
if (!Session.get("hi")) {
this.render('login');
} else {
this.render('dashboard');
}
});
Router.route('userList', function () {
this.render('userList');
});
Router.route('addnewuser', function () {
this.render('addnewuser');
});
Note:I want to make that when admin to be login,it can reach to all pages userlist,addnewuser etc.
If I understood it properly you want to give admin rights to certain routes of yours. There are several ways to achieve this using iron:router, either by building a Controller or using Filters. I would create an Iron Router Controller to tackle that and attach it to any route that needs that kind of checks. Both Controllers and Filters are actually reusable bits of code which is what we are looking for.
Make sure you add alanning:roles to your packages list (if you haven't already) and add some admin roles to at least one of your Meteor.users() like it's shown here
Building the actual Controllers is easy.
lib/router.js
AdminController = RouteController.extend({
onBeforeAction: function () {
var loggedInUser = Meteor.userId();
if (!!loggedInUser) {
if (!Roles.userIsInRole(loggedInUser, 'admin')) {
// Basic redirect to the homepage
Router.go('homepage');
this.stop();
}
} else {
// Log them in when they are not
Router.go('login');
this.stop();
}
this.next();
}
});
Router.route('/admin', {
name: 'admin',
controller: AdminController,
waitOn: function () {
// return subscriptions here
}
});
Continue to add that to any other routes you like.
In my application I want to seed the database with users and send them an enrollment link to activate their account (and choose a password). I also want them to verify/change some profile data.
On the server I seed the database like this:
Meteor.startup(function () {
if(Meteor.users.find().count() === 0) {
var user_id = Accounts.createUser({ email: 'some#email.com', profile: { some: 'profile' } });
Accounts.sendEnrollmentEmail(user_id);
}
})
The enrollment link is sent as expected, but I want to create a custom template for when the url in the email is clicked. Preferably handled by iron-router. (Not using the accounts-ui package).
I tried things like redirecting the user to a custom route like this:
var doneCallback, token;
Accounts.onEnrollmentLink(function (token, done) {
doneCallback = done;
token = token;
Router.go('MemberEnroll')
});
which is not working (it changes the url but not rendering my template)
I also tried to change the enroll URL on the server like this:
Accounts.urls.enrollAccount = function (token) {
return Meteor.absoluteUrl('members/enroll/' + token);
};
But when I do this, the Accounts.onEnrollmentLink callback does not fire.
Also, changing the URL is not documented so I'm not sure its a good practice at all.
Any help is appreciated.
In my application I'm doing like this
this.route('enroll', {
path: '/enroll-account/:token',
template: 'enroll_page',
onBeforeAction: function() {
Meteor.logout();
Session.set('_resetPasswordToken', this.params.token);
this.subscribe('enrolledUser', this.params.token).wait();
},
data: function() {
if(this.ready()){
return {
enrolledUser: Meteor.users.findOne()
}
}
}
})
As enrollment url is like this
http://www.yoursite.com/enroll-account/hkhk32434kh42hjkhk43
when users click on the link they will redirect to this template and you can render your template
In my publication
Meteor.publish('enrolledUser', function(token) {
return Meteor.users.find({"services.password.reset.token": token});
});
After taking the password from the user
Accounts.resetPassword(token, creds.password,function(e,r){
if(e){
alert("Sorry we could not reset your password. Please try again.");
}else{
alert("Logged In");
Router.go('/');
}
})
enroll link
Accounts.urls.enrollAccount = function (token) {
return Meteor.absoluteUrl('enroll-account/' + token);
};
Im afraid now isnt possible, what i did is changing the html and css using "rendered" function but it has some probs with delay
Meteor.startup(function(){
Template["_enrollAccountDialog"].rendered = function(){
document.getElementById('enroll-account-password-label').innerHTML = 'Escolha sua senha';
$('.accounts-dialog').css('background-color','#f4f5f5');
$('.accounts-dialog').css('text-align','center');
$('.accounts-dialog').removeAttr('width');
document.getElementById('login-buttons-enroll-account-button').className = ' create-account-button';
document.getElementById('login-buttons-enroll-account-button').innerHTML = 'Criar conta';
}
});
I am testing a restrict login function with router code below
var requireLogin = function() {
if (! Meteor.user()) {
console.log("user not logged");
this.next()
} else {
console.log("user logged");
this.next()
}
}
Router.onBeforeAction(requireLogin, {except: ['home','login','about']});
when I try to enter restricted area like userprofile it ask me to log in and print "user not logged"
and after I successfully log in and try to access that area again. it printing both code starts with "user not logged" and then "user logged"
I want to know how to avoid this to happen? since some page become glitched when this happened.
I want it to only print "user logged" if I enter a restricted area page.
Any help would be appreciated.
You need to integrate Meteor.loggingIn() somewhere in your requireLogin function. Because, what's happening is that Meteor is still loading the user system and for every route change, it re-authenticates the user based on current session, if it exists.
var requireLogin = function() {
if(!Meteor.user()){
if(Meteor.loggingIn()){
this.render(this.loadingTemplate);
}else{
this.render('accessDenied');
}
}else {
this.next();
}
}
You will notice that it uses this.loadingTemplate. To keep this, you must also configure your routes to have a loading template. e.g.:
Router.configure({
loadingTemplate: 'loading'
});
or you could just simply swap that out with this.render('loading'); where 'loading' is the template name of your 'Now loading' yield/page.