The situation
- I am writing a Single-Page-Web App (using Angular). Lets call it SPA
- Another team-mate is writing some APIs (using Node.js). Lets call is Server
- My SPA is to Login to the Server using login/passwd, and do some stuff
My team-mate has decided to use cookies to track the session. Hence, upon a successful login, a http-only cookie is to be set in the web-browser the SPA is loaded in.
The problem
If we put the SPA in the Server's public_html dir, all works well. This, however, makes the SPA as a part of the API code. This breaks our build process, since every version upgrade to the SPA now requires upgrading the API too.
If we host the SPA in a seperate webserver that only serves the static SPA files, I run into CORS issues. Since the SPA comes from a different origin than the APIs it is trying to access, the browser blocks the ajax calls. To overcome this, we will have to set Access-Control-Allow-Origin
on the server side appropriately. I also understand that Access-Control-Allow-Credentials:true
needs to be set, to instruct the browser to set/send the cookies.
Possible solutions
We create a build process which does a git-pull to the Server's public_html dir every time the SPA gets upgraded. I am trying to avoid this to keep the client and server upgrades separate.
We create a proxy kind of situation, where the Server doesnt store the SPA files, but collects them on-demand from another server that hosts the SPA files. In this case, the web-browser will see the SPA files and subsequent ajax calls from the same origin.
We code the server to set
Access-Control-Allow-Origin:*
in its responses. Firstly, this is too open and looks insecure. Is it really insecure, or is it just my perception? Also, since we are settingAccess-Control-Allow-Credentials:true
, Chrome complainsCannot use wildcard in Access-Control-Allow-Origin when credentials flag is true.
. To overcome this, we will have to put exact origins (perhaps using a regex) in theAccess-Control-Allow-Origin
. This may seriously restrict us from distributing our SPA to users in unknown domains.For a Server API designer, is Cookie based authentication the recommended way to handle Authentication for SPAs? OAuth2.0 and JWT based Authentication seems to suggest that Cookies based Authentication is not right for SPAs. Any pros/cons?
Kindly comment on the above options, or suggest any others that you may have used. Thanks in advance.