question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to restrict pages to logged in users only

See original GitHub issue

Any pointers on redirecting users who try to access a route that is for logged in users only? Here’s what I have so far in routes.js:

// const user is either true or false, and passes that in as a prop to the app
on('*', async (state, next) => {
    const component = await next();
    const user = await http.get(`/api2/user`);
    return component && <App context={state.context} user={user}>{component}</App>;
});

I feel like the redirection should happen somewhere in this file, rather than inside <App /> somewhere, is it something to do with the on('error'... function? Though I’d rather redirect users to a login page and not a 404.

<bountysource-plugin>

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
ucaycommented, Feb 3, 2017

In my case, I use redux. You can add this to src/routes/index.js or src/routes/yourroute/index.js

async action({ store }) {
    const { user } = store.getState();
    if(null === user) return { redirect: '/login' };

    (...)
}
0reactions
jnsandrewcommented, Feb 3, 2017

Oh I forgot all about this. In the end I just validated with a token, and redirected users if the response from the API was a 403.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Restrict WordPress Pages by User Role - WPBeginner
On the edit screen, scroll down to 'Restrict this content' meta box and select 'Members with certain role' option. Restrict content by user...
Read more >
Tutorial: WordPress Restrict Page Access to Logged In Users
Profile Builder: The Easiest Way to Restrict Page Access to Logged-in Users in WordPress. Profile Builder gives you a simple way to customize ......
Read more >
Content Control – User Access Restriction Plugin
Content Control is a lightweight and powerful plugin that allows you to take complete control of your website's content by restricting access to...
Read more >
5 WordPress Plugins that Restrict User Access to Content
5 plugins to restrict user access in WordPress · 1. Restrict User Access · 2. Restrict Content · 3. User Access Manager ·...
Read more >
How to Restrict WordPress Content to Specific Users
Restrict individual form fields based on visibility settings. · Use hidden fields and conditional logic. · Create a View that restricts access to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found