This is a proof of concept, please consider it as alpha version.
Use at your own risk!
Feedback, suggestions and contributions are most welcome!
- No reliance on external oauth services like Auth0, Firebase Auth, Okta,... Note that this might be an advantage or a disadvantage depending on your requirements. Such services typically come with some extras like analytics, user management or anomaly detection, it's a question of tradeoffs.
- Minimal boilerplate by leveraging high-quality libraries like grant.js, Nextjs and cookie-session.
- serverless: the backend-side relies only on (vercel) functions
- uses grant.js under the hood, which is customizable to the extreme, and supports a very large number of OAuth providers out of the box
- supports workspaces (subdomains)
- shares the user's authentication across the top domain and all subdomains
- if the user triggers authentication from any page under any domain/subdomain/path, the user is sent back to the same exact page after the authentication
- stateless session (does not require a db), stores the session in a secure, encrypted cookie
- uses a short cookie/session maxAge of 15 minutes, but sliding: session life is auto-extended as long as the user remains active
- to minimize cookie rewrites, the session auto-extention mechanism happens max. once per minute
- cookie sharing with subdomains under
localhost
(for example withsub.localhost
) is not supported by browsers. It is thus necessary to add entries manually inside/etc/hosts
In order for subdomains to work locally, please make sure to add these lines to your /etc/hosts
file
127.0.0.1 localhost.com
127.0.0.1 sub1.localhost.com
127.0.0.1 sub2.localhost.com
In a terminal:
cp .env.example.js .env.js
In your newly created .env.js
, make sure to update all values marked with CHANGEME
by your own.
COOKIE_SESSION_SECRET: "CHANGEME",
OAUTH_GOOGLE_KEY: "CHANGEME.apps.googleusercontent.com",
OAUTH_GOOGLE_SECRET: "CHANGEME",
...
In a terminal:
yarn dev
Then:
- Navigate to
http://localhost.com:3000
orhttp://sub1.localhost.com:3000
- Click on "Continue with Google" to authenticate
- You should now be authenticated and see your profile data
- Navigate to either
http://localhost.com:3000
,http://sub1.localhost.com:3000
orhttp://sub2.localhost.com:3000
. You should stay authenticated across [sub]domains and still be able to see your profile data.
This demo/proof of concept is intentionally kept extremely bare-bones and minimal so it remains easy to read, understand and reproduce.
The first thing you might want to do is to persist your authenticated users in a database. The place to do so would be in the pages/api/oauth/callback.js
, where you can process the OAuth response as you wish.
[TODO]