I'm developing a web service that have following structure:
- Web Server: this is implemented with NextJS which do Server-Side Rendering and serve server-side rendered webpage data to Client.
- API Server: and this one is implemented with NestJS. Clients will send graphql queries/mutations to this server.
- Client: any clients can visit my web service and sign in (or sign up)
and I want to implement authentication feature for my web server, but problem is came up here. how can I share the auth data between
Web Server, API Server, Client? if you signed in on your own browser, eventually a signing request will be sent to the API server.
but there's literally no way to know whether the client is signed in or not from
Web Server. I mean, auth data (
whether if user has signed in or not) will be stored only at
I had searched and spent a lot of time about this and I've got an answer that I have to use JWT Token but there were several ways to store it:
Storing it on clients' web storage: I don't think this can be an answer since the
Web Servershouldn't able to get clients' web storage data. this means SSR (server-side rendering) wouldn't work well.
Storing it on
Cookie: this is bad too. because we send a signing in request to the
Cookiebut set from other subdomains with specifying
Domain: we can specify domain for setting cookie. as far as I know, a response from
api.example.comcan set cookies for
Set-Cookiesvalue. read this
yeah, method 3 seems pretty neat to have but I absolutely don't know that setting cookies from another sub-domains will cause big security hole. my web service will process about users' money so security issues will come very critical.
someone I know advised me that I can use reverse proxy with paths not domain. I mean, if
API Server was serving on
api.example.com, we can serve it on
example.com/api/* so now we can share the cookies between all the server and client. but we should store cookies on web storage too since cookies will be flagged as
HttpOnly. this will cause increasing complexity of development.
In this case, which method will be the best answer for my case? are these methods I've mentioned above are really safe to do?