chan.dev / posts

AuthKit in Any JS Framework

🌱 This post is in the growth phase. It may still be useful as it grows up.

Create a new app

Add WorkOS environment variables

Install @workos-inc/node sdk

Ensure that rendering strategy is server or hybrid

Create /sign-in endpoint/route

Redirect to AuthKit authorization url

Create /auth/callback endpoint/route

Parse code from /auth/callback request

Exchange authorization code for user session

Take sealedSession from authentication response

Catch authentication errors

Redirect to protected route

Redirect when authentication fails with invalid_jwt

Attempt token refresh when authorization is expired. Respond with error if refresh fails.

Set ‘wos-session’ with refreshed session

(Consolidate conditional view logic to user assignment)

Add /sign-out endpoint

getLogoutURLFromSessionCookie

Redirect to authkit logout url

Add middleware boilerplate

Restrict middleware from running, save for protected routes

Extract withAuth middleware function

Extend global context with WorkOS User type