OCS Auth Service
Production-ready OAuth2-like authentication service built with Fastify, Prisma, and PostgreSQL.
Requirements
- Node.js 20+
- PostgreSQL
- Redis
Setup
- Create a database and update
.envfrom.env.example.- Add
REDIS_URL(example:redis://localhost:6379). - Add OAuth provider settings:
BASE_URLGOOGLE_CLIENT_ID,GOOGLE_CLIENT_SECRETGITHUB_CLIENT_ID,GITHUB_CLIENT_SECRETMICROSOFT_CLIENT_ID,MICROSOFT_CLIENT_SECRET
- Add
- Install dependencies:
npm install - Generate Prisma client:
npm run prisma:generate - Run migrations:
npx prisma migrate dev - Start dev server:
npm run dev
Production
- Build:
npm run build - Start:
npm run start
Notes
- Create OAuth clients in the database before use.
- Migration provisions
social_oauthclient used by social login JWT issuance. - JWT keys must be valid RSA PEM values.
- Social OAuth2 endpoints:
GET /auth/googleGET /auth/google/callbackGET /auth/githubGET /auth/github/callbackGET /auth/microsoftGET /auth/microsoft/callback
- To return user to front-end after social login, call start endpoints with PKCE context:
- Example:
GET /auth/google?redirect_uri=https://financial.pscodium.dev/callback&client_id=electron-app&code_challenge=<PKCE_CHALLENGE>&code_challenge_method=S256 - Callback redirects to your app with
?code=<INTERNAL_AUTH_CODE> - Exchange
codeatPOST /auth/tokenwithgrant_type=authorization_code,client_id,redirect_uri, andcode_verifier.
- Example:
POST /auth/tokensetsrefresh_token(path=/auth) andaccess_token(path=/) inHttpOnlycookies.- For auth/api split across subdomains, set
COOKIE_DOMAIN(example:.pscodium.dev) so cookies can be sent to sibling subdomains. - Cookie policy is configurable via
COOKIE_SAME_SITE(strict|lax|none). - For
grant_type=refresh_token, you can omitrefresh_tokenin body and use the cookie automatically.
Project Structure
src/modules/*/presentation: HTTP layer (routes, controllers, request schemas).src/modules/*/application: application services/use-case orchestration.src/modules/*/infrastructure: data access implementations (repositories).src/modules/*/domain: module-specific types/contracts.src/shared: cross-cutting concerns grouped by concern:security(crypto, jwt, validators)persistence(prisma)cache(redis)http(auth context)observability(logger)errors(app error base class)