I recently read Cookies vs Tokens for Angularjs and implemented a login and authentication element that allows users to log in from the login page. The application is configured to create an accounting module (responsible for entering the system, account, profile, etc.) as a separate page that will be redirected to the SPA for the main application.
After a successful subscription, the token is sent back to the client on the login page as a JWT, and sessionStorage / localStorage values ββare set via js. Finally, the user is redirected (also via js) to the main application. The problem is that I redirect via js, the header cannot be set, which obviously does not allow authorization in the main application when the page loads (since my middleware is higher than static and auth api requests). If I try to redirect from the server after the form message, and not return the token via JSON upon successful completion, sessionStorage will not be installed via js, as described in the blog post.
I came up with two ideas and wanted to know which one is best practice.
On the server, set the cookie response authentication file "Only http" (our browser requirements allow it) cookie, which is read on the next request of the main application. Then the cookie will be read by the server and allowed to serve secure static assets. My initial thought was that cookie aims to use the authorization header for each request, as time cookie can be read, even if it was removed in the first request api.
Allow loading of the previously mentioned static assets without authentication (html, css, application js) and the first internal API request (which is required almost immediately during loading in the application), which will then be accessed via Angular $ http interceptors to set the request authorization header. The same interceptor may be redirected to the page with the caption, if 401 is sent back.
I thought that the second option is more simple, because it will only need to move the middleware auth for static intermediate software, and then upgrade http-interceptor in Angular, but thought it might be a bad practice to have static files to download, and then redirects after the fact. Any input is appreciated.
source share