Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to restrict pages to logged in users only #276

Closed
jnsandrew opened this issue Oct 9, 2015 · 5 comments
Closed

Best way to restrict pages to logged in users only #276

jnsandrew opened this issue Oct 9, 2015 · 5 comments

Comments

@jnsandrew
Copy link

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 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.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@simtechmedia
Copy link

I followed this tutorial, https://auth0.com/blog/2015/04/09/adding-authentication-to-your-react-flux-app/ worked good :)

@mychaelgo
Copy link

@simtechmedia can you give full example on what you implement ?

@gmlnchv
Copy link

gmlnchv commented Nov 13, 2015

@simtechmedia +1

@ucay
Copy link

ucay commented 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' };

    (...)
}

@jnsandrew
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants