0

I apologize in advance for the wall of text.

I'm working on a project for school. We are trying to create a messenger application with a separate front/back-end. For the back-end we decided to use Hapi (NodeJS), which is now a working API that authenticates based on JWT. Let's assume the back-end is running on http://localhost:3000.

To interact with the API and create an actual user-friendly messenger, we decided to go with VueJS, using Vuetify as a design framework. We created a webpack template with vue create (Vue CLI3) and started designing a register/login page, which is served on http://localhost:8080.

I have now arrived at the point where I have to think about a way to show different content based on authentication. I can request a JWT from the API by logging in, but I don't want to store it in local storage, as that is insecure. The same goes for cookies, if they are not HttpOnly.

I feel like a messenger app wouldn't require a user to login every time they restart their browser, so I need some kind of persistence, which leaves only one option (please correct me if I'm wrong): HttpOnly cookies containing our session data, a JWT in this case. Seeing as I can't set HttpOnly cookies with Vue (as it's JS), how would I go about creating persistent authentication?

We want a user to be redirected to a login page if they aren't logged in. If they are, they can access and see extra functions (Vue components). If they refresh, or restart their browser, they should still be logged in and able to access the functions the API offers (like sending messages to others, reading their own messages). This, while not being vulnerable to XSS.

I have unleashed my Google-fu for 2 days straight now and can't find a good answer. People use VueX which isn't actually persistent and only works persistently with local storage, which again, is insecure for storing a JWT.

Is this at all possible with a separate front/back-end? If I send back a cookie with Hapi, that might work and get stored in the browser, but that'll be HttpOnly so how do I read it with the front-end to determine if I am authenticated?

Please be as verbose as possible, as I seem to miss a lot of design principles and context that are apparently very obvious to everyone else on the internet. I honestly believe that if there is a very good answer to this, that it'll help other people starting out tremendously.

TL;DR: How to create persistent authentication with a Vue front-end and Hapi back-end, without sacrificing security?

b0nes
  • 103
  • 1
  • 7

1 Answers1

0

as I know, HTTPOnly cookies can be set only on https protocol

source : https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookies

and I think it is safe to store jwt token in localstorage as long as you use https

another related thread about jwt storing in reactjs : https://stackoverflow.com/a/44209185/10661914

Jemika Negara
  • 54
  • 2
  • 5