JWT authorization in nuxt.js

The name of the picture


JWT authorization in nuxt.js



I have a problem with authorization via JWT using nuxt. I want to restrict access to specific routes for users that don't have JWT token. The most proper place to check authorization in nuxt is middleware, but I can not access localStorage inside of it. I tried to set mode: "spa" in nuxt.config.js and to check if (process.browser) condition in middleware to get access to localStorage in middleware, but this condition is false for every request. So, are there any options to check JWT token existance in middleware? Should I store JWT token in localStorage? If not, where should I store it?


mode: "spa"


if (process.browser)




1 Answer
1



First you need to use process.client instead of process.browser to check if its a client or not. See docs here


process.client


process.browser



isClient Boolean Client & Server Boolean to let you know if you're
actually renderer from the client-side (deprecated. use
process.client).



Second if u set mode: spa it always will be true, so this check only makes sense in universal mode.


mode: spa



Third you can store token both in localstorage and cookie, thats how nuxt-auth module is done. Take a loot at it, probably u will be able to use it instead of writing your own implementation, or at least you will have some ideas how its implemented






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS