Skip to content

Express Adapter API

High-level Express.js integration.

createAuthRouter

Creates an Express router with all authentication routes.

typescript
import { createAuthRouter } from "auth-kit-js/express";

const router = createAuthRouter(config);

Config

typescript
interface AuthRouterConfig {
  google?: GoogleOAuthConfig;
  facebook?: FacebookOAuthConfig;
  telegram?: TelegramConfig;

  onLogin: (
    profile: NormalizedProfile,
    req: Request,
  ) => Promise<{ token: string }>;
  onError?: (error: Error, req: Request, res: Response) => void;

  successRedirect?: string;
  errorRedirect?: string;
  usePKCE?: boolean; // default: true

  cookieOptions?: {
    httpOnly?: boolean;
    secure?: boolean;
    sameSite?: "strict" | "lax" | "none";
    maxAge?: number;
  };
}

Routes Created

MethodRouteDescription
GET/googleStart Google OAuth
GET/google/callbackHandle Google callback
GET/facebookStart Facebook OAuth
GET/facebook/callbackHandle Facebook callback
POST/telegram/webappVerify Telegram WebApp
POST/telegram/widgetVerify Login Widget

Response Format

Success:

json
{
  "success": true,
  "token": "your-token",
  "profile": {
    "provider": "google",
    "providerUserId": "123",
    "email": "user@example.com",
    "name": "John Doe",
    "avatarUrl": "https://..."
  }
}

Error:

json
{
  "success": false,
  "error": "Error message",
  "code": "ERROR_CODE"
}

Example

typescript
import express from "express";
import session from "express-session";
import { createAuthRouter } from "auth-kit-js/express";

const app = express();

app.use(express.json());
app.use(
  session({
    secret: "secret",
    resave: false,
    saveUninitialized: false,
  }),
);

const auth = createAuthRouter({
  google: {
    clientId: process.env.GOOGLE_CLIENT_ID!,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    redirectUri: "http://localhost:3000/auth/google/callback",
  },
  async onLogin(profile) {
    // Save user to database
    const user = await db.users.upsert(profile);
    return { token: jwt.sign({ userId: user.id }, "secret") };
  },
  successRedirect: "http://localhost:5173/dashboard",
});

app.use("/auth", auth);

Released under the MIT License.