Search code examples
meteorreact-routermeteor-accounts

METEOR: how to redirect after logout


I am quite new to Meteor & React. Here I would like to redirect my currect user to home page whenever the logout button is pressed. Attached you can see the protected page template with the logout button.

Please note that I am working with the latest versions (Meteor 1.6.1 and React V4).

import React from 'react';
import { Accounts } from 'meteor/accounts-base';

export default class Link extends React.Component{
  onLogout(){
    Accounts.logout()
  };
  render(){
    return(
      <div>
        <p>Private Content goes here</p>
        <button onClick={this.onLogout.bind(this)}>Logout</button>
      </div>
    );
  }
};

any kind of support will be appreciated.


Solution

  • There are two main options to use here:

    1. Pass a callback to Accounts.logout(func)

    This is the simplest but mixes the return behavior into your component, which is not ideal.

    2. Use Accounts.onLogout(func)

    You could put this with your accounts initialization or with your router code, whichever keeps the logic grouped together best for your app.


    In that callback, you'll want to use your router to redirect. The exact syntax will depend on your router, but will generally look like:

    Router.go('/')