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

Nuxt Static Site Auth #3023

Closed
MrRossBennett opened this issue Mar 14, 2018 · 6 comments
Closed

Nuxt Static Site Auth #3023

MrRossBennett opened this issue Mar 14, 2018 · 6 comments
Labels

Comments

@MrRossBennett
Copy link

MrRossBennett commented Mar 14, 2018

Hey all

We're deploying our Nuxt app via Netlify (so as a static site with the npm run generate command. There is no server).

We're using an auth service that stores a token in localstorage when a user is authenticated. We're then accessing that token using a Vuex store. Once the app is loaded we are then able to use Nuxt's middleware to protect certain routes from non-authenticated users etc.

But when the page is refreshed obviously the store is empty. So we're trying to re-verify the user on page load by dispatching an action. We've tried to do this via the fetch method in a page component and via a plugin, but both seem to fire too late and our isAuthenticated getter is returning false even for already-signed-in users.

Middleware doesn't seem an option as it's not fired on client refresh and obviously nuxtServerInit is out of the question as we, er, don't have a server.

What are our best options for page refresh on a static site?

Thanks

This question is available on Nuxt.js community (#c2618)
@husayt
Copy link
Contributor

husayt commented Mar 15, 2018

See nuxt auth module and also this example for best patterns

@MrRossBennett
Copy link
Author

Thanks, let me clarify our issue.

When you are running a Nuxt static site (so no server is present), middleware does not run on page load.

Is there anything we can do about this?

@davidroyer
Copy link

You need to use the plugins directory. Nuxt will run this code prior to rendering completely on the client.

Do what you need for auth and then commit to your store.

Here’s my file using firebase and a repo of doing this

https://github.com/davidroyer/nuxt-firebase-auth/blob/master/plugins/fireauth.js

@xie-zhichao
Copy link

@davidroyer I tried to use plugins to do authCheck, it caused other problem:

app.40ac7a2b1c36aa24d832.js:1 [nuxt] Error while initializing app DOMException: Failed to execute 'appendChild' on 'Node': This node type does not support this method.

@galvez
Copy link

galvez commented Aug 26, 2018

This seems like a specialized request for assistance, please check out the channels over at Discord for help. Closing as there's been no updates over 90 days.

@galvez galvez closed this as completed Aug 26, 2018
@lock
Copy link

lock bot commented Nov 1, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 1, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants