Skip to main content

Node.js (Express + TypeScript) Integration Guide

This guide demonstrates an enterprise-level setup for integrating Mubarokah ID OAuth 2.0 into a Node.js application using Express and TypeScript. It includes type definitions, a dedicated OAuth service, authentication middleware, and example routes.

Prerequisites

  • Node.js and npm/yarn installed.
  • TypeScript configured in your project.
  • Express framework.
  • An HTTP client like axios: npm install axios or yarn add axios
  • Session management (e.g., express-session): npm install express-session
  • Optional: A library for PKCE generation if not implementing manually (e.g., pkce-challenge).

Project Structure (Conceptual)

Type Definitions

Define interfaces for configuration, token responses, and user information. src/types/mubarokah.ts:

Mubarokah OAuth Service

This service encapsulates all interactions with Mubarokah ID’s OAuth endpoints. src/services/MubarokahOAuthService.ts:

Token Service (Conceptual)

This service would handle secure storage and retrieval of tokens. For brevity, a full implementation (e.g., using Redis or a database) is omitted. src/services/TokenService.ts (Interface and basic in-memory example):
The TokenService above uses an in-memory store, which is not suitable for production. Use a persistent store like Redis or a database, and encrypt sensitive tokens.

Authentication Middleware

This middleware protects routes that require authentication. src/middleware/auth.ts:
The requireAuth middleware is simplified. Production systems often use JWTs or session management. If using JWTs, validate the signature and claims. If session-based, ensure the session is valid. The provided example is more geared towards session-based authentication after the OAuth dance, with a placeholder for separate Bearer token validation for API clients.

Auth Routes

Define routes for login, callback, and logout. src/routes/auth.ts:

Application Setup

Integrate the Mubarokah OAuth service and routes into your Express application. src/app.ts (Simplified):
Security Note:
  • Use a strong, unique SESSION_SECRET stored as an environment variable in production.
  • Ensure secure: true for cookies in production (requires HTTPS).
  • The example TokenService is for demonstration. Use a robust, secure storage solution for tokens in production.
  • Storing raw access tokens in the session might have security implications depending on your session store’s security. Consider storing only a session ID and retrieving tokens from a secure backend store when needed.
This setup provides a solid foundation for integrating Mubarokah ID OAuth with Node.js, Express, and TypeScript, including session management and basic security considerations. Remember to adapt and enhance the token storage and session management for your production environment.