Accounts-ui loginButtons: configuring a redirect target after successful sign-in
0 reputation · 16 Mar 2020, 12:56 UTC
0 reputation · 16 Mar 2020, 12:56 UTC
I am using Meteor's accounts-ui package with the built-in {{> loginButtons}} template in an application that also uses accounts-password. After a user signs in, I want them sent to a specific route (for example a dashboard) rather than staying on the page where the login dropdown was rendered.
From the documented API, Accounts.ui.config() exposes options like passwordSignupFields and requestPermissions, but I cannot find a documented option that controls post-login navigation. The template appears to manage only its own dropdown state, and routing is left entirely to the application. I would prefer to avoid replacing the whole template with custom Blaze markup just to get a redirect.
Assume Meteor 2.x with Blaze and a client-side router such as ostrio:flow-router-extra or kadira:flow-router. I am unsure whether the intended integration point is a login callback, a router-level trigger, or template override, and whether behavior differs between accounts-ui and the lower-level accounts-base API.
accounts-ui for a post-login redirect, or is Accounts.onLogin on the client the documented integration point?Accounts.onLogin is used, does it fire reliably for password, resume-token, and OAuth logins alike?There is no built-in accounts-ui option to set a redirect after sign-in. The documented integration point is the client-side Accounts.onLogin callback, combined with your router's programmatic navigation.
Accounts.ui.config() only controls UI-specific fields such as passwordSignupFields and OAuth request permissions; it does not expose a post-login navigation setting.Accounts.onLogin is invoked for every successful login flow: password (Meteor.loginWithPassword), resume-token (Meteor.loginWithToken), and OAuth services.client/main.js):Accounts.onLogin(() => {
// Defer navigation until the current flush and any
// login method callbacks have finished
Meteor.setTimeout(() => {
FlowRouter.go('/dashboard');
}, 0);
});kadira:flow-router, ostrio:flow-router-extra, or another), replace FlowRouter.go with that router's programmatic navigation method.Tracker.afterFlush instead of Meteor.setTimeout to guarantee Blaze has finished rendering before navigating:Accounts.onLogin(() => {
Tracker.afterFlush(() => {
FlowRouter.go('/dashboard');
});
});accounts-password; confirm the URL changes to /dashboard and the login dropdown closes cleanly.Meteor.loginWithToken(token)) and verify the same redirect occurs.accounts-ui dropdown is still closing, causing a visible flash; the deferred call avoids this.window.location.href bypasses Blaze reactivity and the router's data dependencies; always prefer the router's programmatic navigation.Accounts.onLogin does not run on failure, handle errors via the login method's own callback or Accounts.onLoginFailure if you need to surface a message.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.