Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ yalc.lock
package-lock.json

docs/
.cursor-docs/



Expand Down
9 changes: 9 additions & 0 deletions pkg/auth/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
extends: ['../../.eslintrc'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
};

162 changes: 162 additions & 0 deletions pkg/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# @imtbl/auth

Minimal OAuth-based authentication package for Immutable Passport. Provides a thin wrapper around `oidc-client-ts` for OAuth/OIDC authentication flows.

## Installation

```bash
npm install @imtbl/auth
# or
pnpm add @imtbl/auth
# or
yarn add @imtbl/auth
```

## Quick Start

```typescript
import { Auth } from '@imtbl/auth';

const auth = new Auth({
clientId: 'your-client-id',
redirectUri: 'https://your-app.com/callback',
});

// Login with popup
const user = await auth.loginPopup();
console.log(user?.profile.email);
console.log(user?.access_token);

// Get current user
const currentUser = await auth.getUser();

// Logout
await auth.logout();
```

## Usage

### Popup Flow

```typescript
const auth = new Auth({
clientId: 'your-client-id',
redirectUri: 'https://your-app.com/callback',
});

const user = await auth.loginPopup();
if (user) {
console.log(user.access_token);
console.log(user.id_token);
console.log(user.profile.email);
}
```

### Redirect Flow

```typescript
// On your login page
await auth.loginRedirect();

// On your callback page (e.g., /callback)
const user = await auth.handleRedirect();
if (user) {
console.log(user.access_token);
}
```

### Direct Login Methods

```typescript
// Google login
await auth.loginPopup({ directLoginMethod: 'google' });

// Apple login
await auth.loginPopup({ directLoginMethod: 'apple' });

// Email login
await auth.loginPopup({
directLoginMethod: 'email',
email: 'user@example.com',
});
```

### Token Management

```typescript
const user = await auth.getUser();

if (user) {
// Check if token is expired
if (user.expired) {
await auth.refreshToken();
}

// Access tokens directly
const accessToken = user.access_token;
const idToken = user.id_token;
const refreshToken = user.refresh_token;
}
```

## API Reference

### `Auth`

#### Constructor

```typescript
new Auth(config: AuthConfig)
```

**Config Options:**
- `clientId` (required): OAuth client ID
- `redirectUri` (required): OAuth redirect URI
- `popupRedirectUri` (optional): Custom popup redirect URI (defaults to `redirectUri`)
- `logoutRedirectUri` (optional): Custom logout redirect URI
- `scope` (optional): OAuth scope (defaults to `'openid profile email'`)

#### Methods

- `loginPopup(options?: LoginOptions): Promise<User | null>` - Login with popup window
- `loginRedirect(options?: LoginOptions): Promise<void>` - Login with redirect flow
- `handleRedirect(): Promise<User | null>` - Handle OAuth callback after redirect
- `getUser(): Promise<User | null>` - Get current authenticated user
- `logout(): Promise<void>` - Logout with redirect
- `logoutSilent(): Promise<void>` - Logout silently (without redirect)
- `refreshToken(): Promise<void>` - Refresh access token if expired

### Types

- `User` - OIDC user object from `oidc-client-ts` (includes `id_token`, `access_token`, `refresh_token`, `profile`, `expired`, `expires_at`, `scope`, etc.)
- `AuthConfig` - Configuration options
- `LoginOptions` - Login options:
- `directLoginMethod?: string` - Direct login method (`'google'`, `'apple'`, `'email'`)
- `email?: string` - Email address (required when `directLoginMethod` is `'email'`)
- `marketingConsent?: 'opted_in' | 'unsubscribed'` - Marketing consent status

## Integration with Wallet Package

The auth package can be used standalone or passed to the wallet package for automatic authentication:

```typescript
import { Auth } from '@imtbl/auth';
import { connectWallet } from '@imtbl/wallet';

const auth = new Auth({ clientId: '...', redirectUri: '...' });

// Pass auth client - login handled automatically when needed
const provider = await connectWallet({ auth });

// User will be prompted to login automatically when required
const accounts = await provider.request({ method: 'eth_requestAccounts' });
```

## Storage

- **Browser**: Uses `localStorage` for token storage
- **SSR**: Uses `InMemoryWebStorage` (tokens not persisted)

## License

Apache-2.0
67 changes: 67 additions & 0 deletions pkg/auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "@imtbl/auth",
"description": "Minimal authentication package for Immutable Passport",
"version": "0.0.0",
"author": "Immutable",
"bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
"dependencies": {
"oidc-client-ts": "3.3.0"
},
"devDependencies": {
"@swc/core": "^1.3.36",
"@swc/jest": "^0.2.37",
"@types/jest": "^29.4.3",
"@types/node": "^18.14.2",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"eslint": "^8.40.0",
"jest": "^29.4.3",
"jest-environment-jsdom": "^29.4.3",
"prettier": "^2.8.7",
"ts-node": "^10.9.1",
"tsup": "8.3.0",
"typescript": "^5.6.2"
},
"engines": {
"node": ">=20.11.0"
},
"exports": {
"development": {
"types": "./src/index.ts",
"browser": "./dist/browser/index.js",
"require": "./dist/node/index.cjs",
"default": "./dist/node/index.js"
},
"default": {
"types": "./dist/types/index.d.ts",
"browser": "./dist/browser/index.js",
"require": "./dist/node/index.cjs",
"default": "./dist/node/index.js"
}
},
"files": [
"dist"
],
"homepage": "https://github.com/immutable/ts-immutable-sdk#readme",
"license": "Apache-2.0",
"main": "dist/node/index.cjs",
"module": "dist/node/index.js",
"browser": "dist/browser/index.js",
"publishConfig": {
"access": "public"
},
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "pnpm transpile && pnpm typegen",
"transpile": "tsup src/index.ts --config ../../tsup.config.js",
"typegen": "tsc --customConditions default --emitDeclarationOnly --outDir dist/types",
"pack:root": "pnpm pack --pack-destination $(dirname $(pnpm root -w))",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"test": "jest",
"test:watch": "jest --watch",
"typecheck": "tsc --customConditions default --noEmit --jsx preserve"
},
"type": "module",
"types": "./dist/types/index.d.ts"
}

Loading
Loading