0

I have a Nuxt app with this middleware

middleware(context) {
    const token = context.route.query.token;

    if (!token) {
        const result = await context.$api.campaignNewShare.createNewShare();
        context.redirect({'name': 'campaigns-new', 'query': {token: result.data.token}});
        local.storage.setKey(token, new DateTime().getTime());
    }
},

I need to store the token to the local storage but I am not sure if it is possible or safe.

2
  • 1
    You should probably use only this plugin and never think about this question again. Commented Oct 21, 2021 at 14:10
  • 1
    Or even this one. Commented Oct 21, 2021 at 14:11

1 Answer 1

1

No, that is impossible. localStorage is a browser part, but on SSR the code evaluates on the server-side. Using cookies is a good solution.

I recommend to use this module: https://github.com/nuxt-community/universal-storage-module

Sign up to request clarification or add additional context in comments.

3 Comments

Cookies are not good solution. LoacalStorage has much more space to store data. Am I missing something?
I have a question. If I call $storage.setLocalStorage(key, value) in the middleware and then redirects to another location. Can I call localStorage.getItem('key') on this new location? I dont understand how this package handles the storages if they are not actually available like localStorage in middleware.
@Čamo I agree cookies are not the best solution. I believe there should be a SSR solution for authorisation based on auth tokens. Have you read this post auth.nuxtjs.org/api/options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.