@imtbl/auth 2.12.5 → 2.12.6-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -0
- package/dist/browser/index.js +79 -27
- package/dist/node/index.cjs +96 -40
- package/dist/node/index.js +79 -27
- package/dist/types/index.d.ts +2 -1
- package/dist/types/login/standalone.d.ts +141 -0
- package/dist/types/types.d.ts +32 -3
- package/package.json +6 -6
- package/src/Auth.test.ts +225 -0
- package/src/Auth.ts +23 -10
- package/src/index.ts +16 -0
- package/src/login/standalone.ts +745 -0
- package/src/types.ts +36 -2
package/README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# @imtbl/auth
|
|
2
|
+
|
|
3
|
+
Authentication utilities for the Immutable SDK.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @imtbl/auth
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
This package provides two ways to handle Immutable authentication:
|
|
14
|
+
|
|
15
|
+
1. **Auth Class** - Full-featured authentication with session managed on client side.
|
|
16
|
+
2. **Standalone Login Functions** - Stateless login functions for use with external session managers (e.g., NextAuth)
|
|
17
|
+
|
|
18
|
+
## Standalone Login Functions
|
|
19
|
+
|
|
20
|
+
For Next.js applications using NextAuth, use the standalone login functions. These handle OAuth flows and return tokens without managing session state.
|
|
21
|
+
|
|
22
|
+
### loginWithPopup
|
|
23
|
+
|
|
24
|
+
Opens a popup window for authentication and returns tokens when complete.
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { loginWithPopup } from '@imtbl/auth';
|
|
28
|
+
import { signIn } from 'next-auth/react';
|
|
29
|
+
|
|
30
|
+
async function handleLogin() {
|
|
31
|
+
const tokens = await loginWithPopup({
|
|
32
|
+
clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
33
|
+
redirectUri: `${window.location.origin}/callback`,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Sign in to NextAuth with the tokens
|
|
37
|
+
await signIn('immutable', {
|
|
38
|
+
tokens: JSON.stringify(tokens),
|
|
39
|
+
redirect: false,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### loginWithRedirect
|
|
45
|
+
|
|
46
|
+
Redirects the page to the authentication provider. Use `handleLoginCallback` on the callback page.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { loginWithRedirect } from '@imtbl/auth';
|
|
50
|
+
|
|
51
|
+
function handleLogin() {
|
|
52
|
+
loginWithRedirect({
|
|
53
|
+
clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
54
|
+
redirectUri: `${window.location.origin}/callback`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### handleLoginCallback
|
|
60
|
+
|
|
61
|
+
Handles the OAuth callback and exchanges the authorization code for tokens.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { handleLoginCallback } from '@imtbl/auth';
|
|
65
|
+
import { signIn } from 'next-auth/react';
|
|
66
|
+
|
|
67
|
+
// In your callback page
|
|
68
|
+
async function processCallback() {
|
|
69
|
+
const tokens = await handleLoginCallback({
|
|
70
|
+
clientId: process.env.NEXT_PUBLIC_IMMUTABLE_CLIENT_ID!,
|
|
71
|
+
redirectUri: `${window.location.origin}/callback`,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (tokens) {
|
|
75
|
+
await signIn('immutable', {
|
|
76
|
+
tokens: JSON.stringify(tokens),
|
|
77
|
+
redirect: false,
|
|
78
|
+
});
|
|
79
|
+
// Redirect to home or dashboard
|
|
80
|
+
window.location.href = '/';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### LoginConfig
|
|
86
|
+
|
|
87
|
+
Configuration options for standalone login functions:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
interface LoginConfig {
|
|
91
|
+
/** Your Immutable application client ID */
|
|
92
|
+
clientId: string;
|
|
93
|
+
/** The OAuth redirect URI for your application */
|
|
94
|
+
redirectUri: string;
|
|
95
|
+
/** Optional separate redirect URI for popup flows */
|
|
96
|
+
popupRedirectUri?: string;
|
|
97
|
+
/** OAuth audience (default: "platform_api") */
|
|
98
|
+
audience?: string;
|
|
99
|
+
/** OAuth scopes (default: "openid profile email offline_access transact") */
|
|
100
|
+
scope?: string;
|
|
101
|
+
/** Authentication domain (default: "https://auth.immutable.com") */
|
|
102
|
+
authenticationDomain?: string;
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### TokenResponse
|
|
107
|
+
|
|
108
|
+
The token data returned from successful authentication:
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
interface TokenResponse {
|
|
112
|
+
/** OAuth access token for API calls */
|
|
113
|
+
accessToken: string;
|
|
114
|
+
/** OAuth refresh token for token renewal */
|
|
115
|
+
refreshToken?: string;
|
|
116
|
+
/** OpenID Connect ID token */
|
|
117
|
+
idToken?: string;
|
|
118
|
+
/** Unix timestamp (ms) when the access token expires */
|
|
119
|
+
accessTokenExpires: number;
|
|
120
|
+
/** User profile information */
|
|
121
|
+
profile: {
|
|
122
|
+
sub: string;
|
|
123
|
+
email?: string;
|
|
124
|
+
nickname?: string;
|
|
125
|
+
};
|
|
126
|
+
/** zkEVM wallet information if available */
|
|
127
|
+
zkEvm?: {
|
|
128
|
+
ethAddress: string;
|
|
129
|
+
userAdminAddress: string;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Auth Class
|
|
135
|
+
|
|
136
|
+
For applications that need full authentication management (like the Passport SDK), use the `Auth` class:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { Auth } from '@imtbl/auth';
|
|
140
|
+
|
|
141
|
+
const auth = new Auth({
|
|
142
|
+
clientId: 'your-client-id',
|
|
143
|
+
redirectUri: 'https://your-app.com/callback',
|
|
144
|
+
scope: 'openid profile email offline_access transact',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Login with popup
|
|
148
|
+
const user = await auth.login();
|
|
149
|
+
|
|
150
|
+
// Get current user
|
|
151
|
+
const user = await auth.getUser();
|
|
152
|
+
|
|
153
|
+
// Logout
|
|
154
|
+
await auth.logout();
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Integration with NextAuth
|
|
158
|
+
|
|
159
|
+
For a complete Next.js authentication setup, use this package with:
|
|
160
|
+
- `@imtbl/auth-next-server` - Server-side NextAuth configuration
|
|
161
|
+
- `@imtbl/auth-next-client` - Client-side components and hooks
|
|
162
|
+
|
|
163
|
+
See those packages for full integration documentation.
|