I have a question, i want to do an app web where all pages require a login authentification (like facebook), but i don't find how to redirect my user to the home page when he is logging in. And when i want to force him to go to the login page. I'm using iron:router, account:login and coffeescript
Thank you in advance
OnBeforeActions = loginRequired: () ->
if !Meteor.user()
if Meteor.loggingIn()
else
Router.go 'login'
else
Router.go 'home'
Router.onBeforeAction OnBeforeActions.loginRequired, only: [
'home'
'models'
'photographers'
'createProfil'
'createProject'
]
Router.route '/login', (->
@render 'login'),
name: 'login',
layoutTemplate: 'loginLayout'
In your hook you need to check if the user is logged in, if they aren't then you redirect the router to the login page and stop the current route from loading. If they are then you continue loading the current route. I would also suggest using except:
instead of only:
if you want this hook to run on every page except login
.
Here's a javascript equivalent to what you need to do (which I know works):
Router.onBeforeAction(function () {
if (!Meteor.userId() && !Meteor.loggingIn()) {
this.redirect('login');
this.stop();
} else {
this.next();
}
},{except: ['login'] });
and in coffeescript (which I have not tested since I don't use coffeescript myself):
Router.onBeforeAction (->
if !Meteor.userId() and !Meteor.loggingIn()
@redirect 'login'
@stop()
else
@next()
return
), except: [ 'login' ]
To redirect a user after login you add an onLogin
hook.
Accounts.onLogin(function () {
Router.go('home');
})
Again, in coffee (untested):
Accounts.onLogin ->
Router.go 'home'
return