In my app, I have the loginButtons
on the landing page.
I would like users to be automatically redirected to the /home
route after a successful login.
This code seems to accomplish that:
// Redirect to /home after logging in
Accounts.onLogin(function() {
Router.go("/home");
});
// Make sure the user is logged in when accessing other routes
Router.onBeforeAction((function() {
if (!Meteor.userId() && !Meteor.loggingIn()) {
Router.go("/");
}
this.next();
}), {
except: ["/"]
});
However, when a logged-in user then tries to access the landing page again, they get redirected to the /home
route. This effectively blocks their access to the landing page.
How can I redirect users to /home
after logging in without preventing them from accessing the landing page afterwards?
I managed to fix it using the u2622:persistent-session
package:
// Redirect to /home after logging in
Accounts.onLogin(function() {
if (!Session.get("loginRedirected")){
Router.go("/home");
Session.setAuth("loginRedirected", true);
}
});
The Session.setAuth
method it adds creates session variables in localstorage
so that they persist across page refreshes. It even automatically clears them when the user logs out.