Detect User Login & Redirect

Technical Support
  • Hi everyone, thanks for any input on this.

    Is there a way to detect if a user is logged in to NodeBB and have them redirected depending on that status?

    Specifically,
    User visits www.example.com, a basic html home page.

    • If user has logged into NodeBB previously and is still logged in, redirect them to the Nodebb forum (www.example.com/forum).
    • If the user is not logged in, do nothing.

    I would appreciate any guidance on this,
    Thanks for your time.

  • You can get loggedIn status from
    https://www.example.com/forum/api/user/${username}

    It returns a json object with a loggedIn boolean.

    Example (using jquery to fetch)

    $.getJSON('https://community.nodebb.org/api/user/yariplus', (data) => {
      if (data.loggedIn) window.location.href = 'https://www.example.com/forum/'
    })
    
  • Thank you for your quick reply.

    Is there a way to check logged-in status without specifying a user?

    All I want to be able to do is to have a custom homepage for my NodeBB website that only displays to guests.

    If one of my registered (& logged in) users visits, have it send them to the forum rather than to the custom homepage.

    Thanks again for your help, appreciated.

  • Yup, you can fetch https://www.example.com/forum/api/me, it will return an error if not logged in, and an object if you are.

  • @yariplus to clarify, it's /api/me/, with the trailing slash. It is significant 🙂


Suggested Topics