@docyrus/signin 0.3.1 → 0.4.1
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 +61 -2
- package/dist/constants-CKGcqYM4.d.ts +105 -0
- package/dist/core-index.d.ts +107 -3
- package/dist/core-index.js +125 -4
- package/dist/core-index.js.map +1 -1
- package/dist/index.d.ts +8 -29
- package/dist/index.js +135 -11
- package/dist/index.js.map +1 -1
- package/dist/react-native/index.d.ts +43 -0
- package/dist/react-native/index.js +732 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/{types-C4-_L2Z-.d.ts → types-CmNZOfNS.d.ts} +16 -3
- package/dist/use-docyrus-client--oJPgA_i.d.ts +30 -0
- package/dist/vue/index.d.ts +9 -5
- package/dist/vue/index.js +139 -12
- package/dist/vue/index.js.map +1 -1
- package/package.json +11 -7
- package/dist/core-index-xNry_naL.d.ts +0 -199
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Authentication provider for Docyrus apps. Provides "Sign in with Docyrus" experience with automatic environment detection — standalone apps use OAuth2 Authorization Code + PKCE via page redirect, while iframe-embedded apps receive tokens via `window.postMessage` from the host.
|
|
4
4
|
|
|
5
|
-
Supports **React**, **Vue**, **Electron**, and **Next.js SSR** via separate entrypoints.
|
|
5
|
+
Supports **React**, **Vue**, **React Native**, **Electron**, and **Next.js SSR** via separate entrypoints.
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
@@ -43,6 +43,7 @@ pnpm add @docyrus/signin
|
|
|
43
43
|
| `@docyrus/signin/nextjs` | Server-side helpers (middleware, server client, session) | Next.js |
|
|
44
44
|
| `@docyrus/signin/vue` | Vue provider, composables, components | Vue 3 |
|
|
45
45
|
| `@docyrus/signin/electron` | Electron token storage | Electron |
|
|
46
|
+
| `@docyrus/signin/react-native` | React Native OAuth2 auth (in-app browser) | React Native / Expo |
|
|
46
47
|
| `@docyrus/signin/core` | Pure TypeScript core (no framework dependency) | Any |
|
|
47
48
|
|
|
48
49
|
## Quick Start (React)
|
|
@@ -158,6 +159,61 @@ function LoginPage() {
|
|
|
158
159
|
}
|
|
159
160
|
```
|
|
160
161
|
|
|
162
|
+
## Quick Start (React Native / Expo)
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import { DocyrusAuthProvider, useDocyrusAuth } from '@docyrus/signin/react-native';
|
|
166
|
+
import { createTokenStorage, ReactNativeCryptoProvider } from '@docyrus/api-client/react-native';
|
|
167
|
+
import * as WebBrowser from 'expo-web-browser';
|
|
168
|
+
import * as ExpoCrypto from 'expo-crypto';
|
|
169
|
+
import * as SecureStore from 'expo-secure-store';
|
|
170
|
+
|
|
171
|
+
const tokenStorage = createTokenStorage({
|
|
172
|
+
getItem: (key) => SecureStore.getItemAsync(key),
|
|
173
|
+
setItem: (key, value) => SecureStore.setItemAsync(key, value),
|
|
174
|
+
removeItem: (key) => SecureStore.deleteItemAsync(key),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
function App() {
|
|
178
|
+
return (
|
|
179
|
+
<DocyrusAuthProvider
|
|
180
|
+
clientId="your-oauth2-client-id"
|
|
181
|
+
forceMode="react-native"
|
|
182
|
+
nativeRedirectUri="myapp://auth/callback"
|
|
183
|
+
openAuthSession={WebBrowser.openAuthSessionAsync}
|
|
184
|
+
tokenStorage={tokenStorage}
|
|
185
|
+
cryptoProvider={new ReactNativeCryptoProvider(ExpoCrypto)}>
|
|
186
|
+
<HomeScreen />
|
|
187
|
+
</DocyrusAuthProvider>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function HomeScreen() {
|
|
192
|
+
const { status, signIn, signOut } = useDocyrusAuth();
|
|
193
|
+
const client = useDocyrusClient();
|
|
194
|
+
|
|
195
|
+
if (status === 'loading') return <Text>Loading...</Text>;
|
|
196
|
+
if (status === 'unauthenticated') return <Button title="Sign In" onPress={signIn} />;
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<View>
|
|
200
|
+
<Text>Authenticated!</Text>
|
|
201
|
+
<Button title="Sign Out" onPress={signOut} />
|
|
202
|
+
</View>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### React Native Props
|
|
208
|
+
|
|
209
|
+
| Prop | Type | Description |
|
|
210
|
+
|------|------|-------------|
|
|
211
|
+
| `forceMode` | `'react-native'` | **Required** — enables mobile OAuth2 flow |
|
|
212
|
+
| `nativeRedirectUri` | `string` | **Required** — deep link URI scheme (e.g., `myapp://auth/callback`) |
|
|
213
|
+
| `openAuthSession` | `(url, redirectUri) => Promise<{type, url?}>` | **Required** — `WebBrowser.openAuthSessionAsync` from expo-web-browser |
|
|
214
|
+
| `tokenStorage` | `OAuth2TokenStorage` | Token storage (use `createTokenStorage()` from api-client) |
|
|
215
|
+
| `cryptoProvider` | `CryptoProvider` | PKCE crypto (use `ReactNativeCryptoProvider` from api-client) |
|
|
216
|
+
|
|
161
217
|
## Configuration
|
|
162
218
|
|
|
163
219
|
Pass props to `DocyrusAuthProvider` to override defaults:
|
|
@@ -182,7 +238,10 @@ Pass props to `DocyrusAuthProvider` to override defaults:
|
|
|
182
238
|
| `redirectUri` | `string` | `window.location.origin + callbackPath` | OAuth2 redirect URI |
|
|
183
239
|
| `scopes` | `string[]` | `['offline_access', 'Read.All', ...]` | OAuth2 scopes |
|
|
184
240
|
| `callbackPath` | `string` | `/auth/callback` | Path to detect OAuth callback |
|
|
185
|
-
| `forceMode` | `'standalone' \| 'iframe'` | Auto-detected | Force a specific auth mode |
|
|
241
|
+
| `forceMode` | `'standalone' \| 'iframe' \| 'react-native'` | Auto-detected | Force a specific auth mode |
|
|
242
|
+
| `nativeRedirectUri` | `string` | — | Deep link URI for React Native OAuth2 callback |
|
|
243
|
+
| `openAuthSession` | `Function` | — | `WebBrowser.openAuthSessionAsync` for React Native |
|
|
244
|
+
| `cryptoProvider` | `CryptoProvider` | Browser crypto | Custom crypto for PKCE (React Native) |
|
|
186
245
|
| `storageKeyPrefix` | `string` | `docyrus_oauth2_` | localStorage key prefix |
|
|
187
246
|
| `tokenStorage` | `OAuth2TokenStorage` | Browser localStorage | Custom token storage (e.g., `ElectronTokenStorage`) |
|
|
188
247
|
| `allowedHostOrigins` | `string[]` | `undefined` | Extra trusted iframe origins |
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { OAuth2Tokens, RestApiClient } from '@docyrus/api-client';
|
|
2
|
+
import { D as DocyrusAuthConfig, A as AuthMode, b as AuthStatus } from './types-CmNZOfNS.js';
|
|
3
|
+
|
|
4
|
+
type AuthStateListener = (state: {
|
|
5
|
+
status: AuthStatus;
|
|
6
|
+
tokens: OAuth2Tokens | null;
|
|
7
|
+
error: Error | null;
|
|
8
|
+
}) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Unified authentication manager.
|
|
11
|
+
* Orchestrates standalone OAuth2 and iframe postMessage modes,
|
|
12
|
+
* exposes a pre-configured RestApiClient, and manages token lifecycle.
|
|
13
|
+
*/
|
|
14
|
+
declare class AuthManager {
|
|
15
|
+
private mode;
|
|
16
|
+
private status;
|
|
17
|
+
private tokens;
|
|
18
|
+
private error;
|
|
19
|
+
private client;
|
|
20
|
+
private standaloneAuth;
|
|
21
|
+
private iframeAuth;
|
|
22
|
+
private reactNativeAuth;
|
|
23
|
+
private tokenRefreshTimer;
|
|
24
|
+
private listeners;
|
|
25
|
+
private config;
|
|
26
|
+
constructor(config?: DocyrusAuthConfig);
|
|
27
|
+
getMode(): AuthMode;
|
|
28
|
+
getStatus(): AuthStatus;
|
|
29
|
+
getTokens(): OAuth2Tokens | null;
|
|
30
|
+
getClient(): RestApiClient | null;
|
|
31
|
+
getError(): Error | null;
|
|
32
|
+
subscribe(listener: AuthStateListener): () => void;
|
|
33
|
+
private notify;
|
|
34
|
+
/** Initialize the auth manager. Must be called once on mount. */
|
|
35
|
+
initialize(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Standalone mode initialization.
|
|
38
|
+
* 1. Check if we're returning from an OAuth callback.
|
|
39
|
+
* 2. If not, check for existing tokens in localStorage.
|
|
40
|
+
* 3. If tokens are expired, try to refresh.
|
|
41
|
+
*/
|
|
42
|
+
private initializeStandaloneMode;
|
|
43
|
+
/**
|
|
44
|
+
* Iframe mode initialization.
|
|
45
|
+
* Listen for postMessage tokens from the host.
|
|
46
|
+
* Status remains 'loading' until the host sends the first signin message.
|
|
47
|
+
*/
|
|
48
|
+
private initializeIframeMode;
|
|
49
|
+
/**
|
|
50
|
+
* React Native mode initialization.
|
|
51
|
+
* No callback URL check needed — the in-app browser returns the URL directly.
|
|
52
|
+
* Just check for existing tokens and refresh if expired.
|
|
53
|
+
*/
|
|
54
|
+
private initializeReactNativeMode;
|
|
55
|
+
/** Initiate sign-in. Works in standalone and react-native modes. */
|
|
56
|
+
signIn(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Get the OAuth2 authorization URL without navigating.
|
|
59
|
+
* Use this in Electron apps to open the URL in an external browser.
|
|
60
|
+
* Returns null if not in standalone mode.
|
|
61
|
+
*/
|
|
62
|
+
getAuthorizationUrl(): Promise<string | null>;
|
|
63
|
+
/**
|
|
64
|
+
* Sign out.
|
|
65
|
+
* Standalone: revoke token, clear localStorage, reset state.
|
|
66
|
+
* Iframe: clear local state (host manages the actual session).
|
|
67
|
+
*/
|
|
68
|
+
signOut(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Called when valid tokens are received (either mode).
|
|
71
|
+
* Creates/updates the RestApiClient and schedules token refresh.
|
|
72
|
+
*/
|
|
73
|
+
private setAuthenticated;
|
|
74
|
+
/**
|
|
75
|
+
* Create a RestApiClient with a custom TokenManager that proactively
|
|
76
|
+
* refreshes the token in getToken(). This is necessary because
|
|
77
|
+
* BaseApiClient.getAccessToken() only calls tokenManager.getToken()
|
|
78
|
+
* and does NOT auto-call refreshToken() when the token is expired.
|
|
79
|
+
*/
|
|
80
|
+
private createClient;
|
|
81
|
+
/**
|
|
82
|
+
* Get a valid access token, proactively refreshing if expired.
|
|
83
|
+
* Called by the RestApiClient on every request via tokenManager.getToken().
|
|
84
|
+
*/
|
|
85
|
+
private getValidToken;
|
|
86
|
+
/**
|
|
87
|
+
* Handle token refresh depending on mode.
|
|
88
|
+
* Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
|
|
89
|
+
* Iframe: send postMessage to host requesting new tokens.
|
|
90
|
+
*/
|
|
91
|
+
private handleTokenRefresh;
|
|
92
|
+
/** Schedule proactive token refresh before expiry. */
|
|
93
|
+
private scheduleTokenRefresh;
|
|
94
|
+
private clearTokenRefreshTimer;
|
|
95
|
+
/** Cleanup: remove listeners, timers, stop iframe auth. */
|
|
96
|
+
destroy(): void;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
declare const DEFAULT_API_URL = "https://alpha-api.docyrus.com";
|
|
100
|
+
declare const DEFAULT_OAUTH_SCOPES: string[];
|
|
101
|
+
declare const DEFAULT_CALLBACK_PATH = "/auth/callback";
|
|
102
|
+
/** Default cookie name for SSR token sync */
|
|
103
|
+
declare const DEFAULT_SSR_COOKIE_KEY = "docyrus-token";
|
|
104
|
+
|
|
105
|
+
export { AuthManager as A, DEFAULT_API_URL as D, DEFAULT_OAUTH_SCOPES as a, DEFAULT_CALLBACK_PATH as b, DEFAULT_SSR_COOKIE_KEY as c, type AuthStateListener as d };
|
package/dist/core-index.d.ts
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
export { A as AuthManager,
|
|
2
|
-
|
|
3
|
-
import '
|
|
1
|
+
export { A as AuthManager, d as AuthStateListener, D as DEFAULT_API_URL, b as DEFAULT_CALLBACK_PATH, a as DEFAULT_OAUTH_SCOPES } from './constants-CKGcqYM4.js';
|
|
2
|
+
import { OAuth2Tokens } from '@docyrus/api-client';
|
|
3
|
+
import { D as DocyrusAuthConfig, A as AuthMode } from './types-CmNZOfNS.js';
|
|
4
|
+
export { b as AuthStatus, c as DocyrusAuthContextValue, H as HostSignInMessage, T as TokenRefreshRequestMessage } from './types-CmNZOfNS.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Standalone OAuth2 authorization code flow with PKCE via page redirect.
|
|
8
|
+
* Wraps api-client's OAuth2Client and BrowserOAuth2TokenStorage.
|
|
9
|
+
*/
|
|
10
|
+
declare class StandaloneOAuth2Auth {
|
|
11
|
+
private oauth2Client;
|
|
12
|
+
private storage;
|
|
13
|
+
private callbackPath;
|
|
14
|
+
constructor(config?: DocyrusAuthConfig);
|
|
15
|
+
/**
|
|
16
|
+
* Check if the current page URL is the OAuth2 callback.
|
|
17
|
+
* Called on app initialization to detect returning from the auth server.
|
|
18
|
+
*/
|
|
19
|
+
isCallbackUrl(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Initiate the OAuth2 authorization code flow.
|
|
22
|
+
* Stores the current URL for post-auth redirect, then navigates
|
|
23
|
+
* the page to the authorization endpoint.
|
|
24
|
+
*
|
|
25
|
+
* PKCE state (codeVerifier, state) is stored in localStorage
|
|
26
|
+
* by the OAuth2Client and survives the page redirect.
|
|
27
|
+
*/
|
|
28
|
+
initiateLogin(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Get the authorization URL without navigating.
|
|
31
|
+
* Useful for Electron apps that open the URL in an external browser.
|
|
32
|
+
* PKCE state is stored in storage just like initiateLogin().
|
|
33
|
+
*/
|
|
34
|
+
getAuthorizationUrl(): Promise<string>;
|
|
35
|
+
/**
|
|
36
|
+
* Handle the OAuth2 callback URL.
|
|
37
|
+
* Reads PKCE state from localStorage, validates the state param,
|
|
38
|
+
* exchanges the authorization code for tokens, and stores them.
|
|
39
|
+
*/
|
|
40
|
+
handleCallback(): Promise<OAuth2Tokens>;
|
|
41
|
+
/**
|
|
42
|
+
* Get the URL the user was on before the OAuth redirect.
|
|
43
|
+
* Clears the stored value after retrieval.
|
|
44
|
+
*/
|
|
45
|
+
getReturnUrl(): string | null;
|
|
46
|
+
/** Get currently stored tokens (e.g., on page reload). */
|
|
47
|
+
getStoredTokens(): Promise<OAuth2Tokens | null>;
|
|
48
|
+
/** Check if the stored token is expired (with 60s buffer). */
|
|
49
|
+
isTokenExpired(): Promise<boolean>;
|
|
50
|
+
/** Refresh the access token using the stored refresh token. */
|
|
51
|
+
refreshToken(): Promise<OAuth2Tokens>;
|
|
52
|
+
/** Get a valid access token, refreshing if needed. */
|
|
53
|
+
getValidAccessToken(): Promise<string>;
|
|
54
|
+
/** Logout: revoke token and clear storage. */
|
|
55
|
+
logout(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type IframeAuthCallback = (tokens: OAuth2Tokens) => void;
|
|
59
|
+
/**
|
|
60
|
+
* Iframe authentication via postMessage.
|
|
61
|
+
*
|
|
62
|
+
* Protocol:
|
|
63
|
+
* - App → Host: { type: "signin-ready" } (on start)
|
|
64
|
+
* - Host → App: { type: "signin", accessToken, refreshToken }
|
|
65
|
+
* - App → Host: { type: "token-refresh-request" }
|
|
66
|
+
*
|
|
67
|
+
* Every incoming message's event.origin is validated against
|
|
68
|
+
* the allowed host pattern before processing.
|
|
69
|
+
*/
|
|
70
|
+
declare class IframeAuth {
|
|
71
|
+
private allowedOrigins;
|
|
72
|
+
private allowedPattern;
|
|
73
|
+
private onTokensReceived;
|
|
74
|
+
private refreshPromise;
|
|
75
|
+
private messageHandler;
|
|
76
|
+
constructor(allowedOrigins?: string[], allowedPattern?: RegExp);
|
|
77
|
+
/**
|
|
78
|
+
* Start listening for postMessage events from the host.
|
|
79
|
+
* The callback is invoked every time valid tokens are received.
|
|
80
|
+
*/
|
|
81
|
+
start(onTokensReceived: IframeAuthCallback): void;
|
|
82
|
+
/** Stop listening for postMessage events. */
|
|
83
|
+
stop(): void;
|
|
84
|
+
/**
|
|
85
|
+
* Request fresh tokens from the host.
|
|
86
|
+
* Posts a "token-refresh-request" to the host, then waits for
|
|
87
|
+
* a "signin" response.
|
|
88
|
+
*
|
|
89
|
+
* Only one refresh request can be in-flight at a time.
|
|
90
|
+
* Additional callers share the same promise.
|
|
91
|
+
*/
|
|
92
|
+
requestTokenRefresh(): Promise<OAuth2Tokens>;
|
|
93
|
+
private isOriginAllowed;
|
|
94
|
+
private handleMessage;
|
|
95
|
+
private rejectPendingRefresh;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Detect whether the app is inside an iframe from a trusted *.docyrus.app host
|
|
100
|
+
* or running as a standalone page.
|
|
101
|
+
*
|
|
102
|
+
* If we are in an iframe but NOT from a trusted origin, we fall back
|
|
103
|
+
* to standalone mode (graceful degradation).
|
|
104
|
+
*/
|
|
105
|
+
declare function detectAuthMode(allowedOrigins?: string[], allowedPattern?: RegExp): AuthMode;
|
|
106
|
+
|
|
107
|
+
export { AuthMode, DocyrusAuthConfig, IframeAuth, StandaloneOAuth2Auth, detectAuthMode };
|
package/dist/core-index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BrowserOAuth2TokenStorage, OAuth2Client, RestApiClient } from '@docyrus/api-client';
|
|
1
|
+
import { BrowserOAuth2TokenStorage, OAuth2Client, RestApiClient, MemoryOAuth2TokenStorage } from '@docyrus/api-client';
|
|
2
2
|
|
|
3
3
|
// src/core/auth-manager.ts
|
|
4
4
|
|
|
@@ -181,8 +181,8 @@ var IframeAuth = class {
|
|
|
181
181
|
*/
|
|
182
182
|
requestTokenRefresh() {
|
|
183
183
|
if (this.refreshPromise) {
|
|
184
|
+
const existing = this.refreshPromise;
|
|
184
185
|
return new Promise((resolve, reject) => {
|
|
185
|
-
const existing = this.refreshPromise;
|
|
186
186
|
const originalResolve = existing.resolve;
|
|
187
187
|
const originalReject = existing.reject;
|
|
188
188
|
existing.resolve = (tokens) => {
|
|
@@ -239,6 +239,78 @@ var IframeAuth = class {
|
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
241
|
};
|
|
242
|
+
var ReactNativeOAuth2Auth = class {
|
|
243
|
+
oauth2Client;
|
|
244
|
+
storage;
|
|
245
|
+
openAuthSession;
|
|
246
|
+
redirectUri;
|
|
247
|
+
constructor(config) {
|
|
248
|
+
const apiUrl = config.apiUrl ?? DEFAULT_API_URL;
|
|
249
|
+
if (!config.clientId) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
'DocyrusAuthProvider: "clientId" is required for React Native mode.'
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
if (!config.nativeRedirectUri) {
|
|
255
|
+
throw new Error(
|
|
256
|
+
`DocyrusAuthProvider: "nativeRedirectUri" is required for React Native mode. Set it to your app's deep link URI (e.g., "myapp://auth/callback").`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (!config.openAuthSession) {
|
|
260
|
+
throw new Error(
|
|
261
|
+
'DocyrusAuthProvider: "openAuthSession" is required for React Native mode. Pass WebBrowser.openAuthSessionAsync from expo-web-browser.'
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
const scopes = config.scopes ?? DEFAULT_OAUTH_SCOPES;
|
|
265
|
+
this.redirectUri = config.nativeRedirectUri;
|
|
266
|
+
this.openAuthSession = config.openAuthSession;
|
|
267
|
+
this.storage = config.tokenStorage ?? new MemoryOAuth2TokenStorage();
|
|
268
|
+
this.oauth2Client = new OAuth2Client({
|
|
269
|
+
baseURL: apiUrl,
|
|
270
|
+
clientId: config.clientId,
|
|
271
|
+
redirectUri: this.redirectUri,
|
|
272
|
+
defaultScopes: scopes,
|
|
273
|
+
usePKCE: true,
|
|
274
|
+
tokenStorage: this.storage,
|
|
275
|
+
cryptoProvider: config.cryptoProvider
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Initiate login by opening an in-app browser for OAuth2 authorization.
|
|
280
|
+
* Unlike the browser version, this does NOT navigate the app — it opens
|
|
281
|
+
* a modal browser and waits for the callback URL.
|
|
282
|
+
*/
|
|
283
|
+
async initiateLogin() {
|
|
284
|
+
const { url } = await this.oauth2Client.getAuthorizationUrl();
|
|
285
|
+
const result = await this.openAuthSession(url, this.redirectUri);
|
|
286
|
+
if (result.type !== "success" || !result.url) {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`Authentication was ${result.type === "cancel" ? "cancelled by the user" : "dismissed"}`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
return this.oauth2Client.handleCallback(result.url);
|
|
292
|
+
}
|
|
293
|
+
/** Get currently stored tokens. */
|
|
294
|
+
async getStoredTokens() {
|
|
295
|
+
return this.oauth2Client.getTokens();
|
|
296
|
+
}
|
|
297
|
+
/** Check if the stored token is expired (with 60s buffer). */
|
|
298
|
+
async isTokenExpired() {
|
|
299
|
+
return this.oauth2Client.isTokenExpired();
|
|
300
|
+
}
|
|
301
|
+
/** Refresh the access token using the stored refresh token. */
|
|
302
|
+
async refreshToken() {
|
|
303
|
+
return this.oauth2Client.refreshAccessToken();
|
|
304
|
+
}
|
|
305
|
+
/** Get a valid access token, refreshing if needed. */
|
|
306
|
+
async getValidAccessToken() {
|
|
307
|
+
return this.oauth2Client.getValidAccessToken();
|
|
308
|
+
}
|
|
309
|
+
/** Logout: revoke token and clear storage. */
|
|
310
|
+
async logout() {
|
|
311
|
+
return this.oauth2Client.logout();
|
|
312
|
+
}
|
|
313
|
+
};
|
|
242
314
|
|
|
243
315
|
// src/core/cookie-sync.ts
|
|
244
316
|
var DEFAULT_SSR_COOKIE_KEY = "docyrus-token";
|
|
@@ -269,6 +341,7 @@ var AuthManager = class {
|
|
|
269
341
|
client = null;
|
|
270
342
|
standaloneAuth = null;
|
|
271
343
|
iframeAuth = null;
|
|
344
|
+
reactNativeAuth = null;
|
|
272
345
|
tokenRefreshTimer = null;
|
|
273
346
|
listeners = /* @__PURE__ */ new Set();
|
|
274
347
|
config;
|
|
@@ -307,7 +380,9 @@ var AuthManager = class {
|
|
|
307
380
|
/** Initialize the auth manager. Must be called once on mount. */
|
|
308
381
|
async initialize() {
|
|
309
382
|
try {
|
|
310
|
-
if (this.mode === "
|
|
383
|
+
if (this.mode === "react-native") {
|
|
384
|
+
await this.initializeReactNativeMode();
|
|
385
|
+
} else if (this.mode === "iframe") {
|
|
311
386
|
this.initializeIframeMode();
|
|
312
387
|
} else {
|
|
313
388
|
await this.initializeStandaloneMode();
|
|
@@ -367,8 +442,44 @@ var AuthManager = class {
|
|
|
367
442
|
this.setAuthenticated(tokens);
|
|
368
443
|
});
|
|
369
444
|
}
|
|
370
|
-
/**
|
|
445
|
+
/**
|
|
446
|
+
* React Native mode initialization.
|
|
447
|
+
* No callback URL check needed — the in-app browser returns the URL directly.
|
|
448
|
+
* Just check for existing tokens and refresh if expired.
|
|
449
|
+
*/
|
|
450
|
+
async initializeReactNativeMode() {
|
|
451
|
+
this.reactNativeAuth = new ReactNativeOAuth2Auth(this.config);
|
|
452
|
+
const stored = await this.reactNativeAuth.getStoredTokens();
|
|
453
|
+
if (stored) {
|
|
454
|
+
const isExpired = await this.reactNativeAuth.isTokenExpired();
|
|
455
|
+
if (!isExpired) {
|
|
456
|
+
this.setAuthenticated(stored);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (stored.refreshToken) {
|
|
460
|
+
try {
|
|
461
|
+
const newTokens = await this.reactNativeAuth.refreshToken();
|
|
462
|
+
this.setAuthenticated(newTokens);
|
|
463
|
+
return;
|
|
464
|
+
} catch {
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
this.status = "unauthenticated";
|
|
469
|
+
this.notify();
|
|
470
|
+
}
|
|
471
|
+
/** Initiate sign-in. Works in standalone and react-native modes. */
|
|
371
472
|
async signIn() {
|
|
473
|
+
if (this.mode === "react-native" && this.reactNativeAuth) {
|
|
474
|
+
try {
|
|
475
|
+
const tokens = await this.reactNativeAuth.initiateLogin();
|
|
476
|
+
this.setAuthenticated(tokens);
|
|
477
|
+
} catch (err) {
|
|
478
|
+
this.error = err instanceof Error ? err : new Error(String(err));
|
|
479
|
+
this.notify();
|
|
480
|
+
}
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
372
483
|
if (this.mode !== "standalone" || !this.standaloneAuth) return;
|
|
373
484
|
await this.standaloneAuth.initiateLogin();
|
|
374
485
|
}
|
|
@@ -391,6 +502,9 @@ var AuthManager = class {
|
|
|
391
502
|
if (this.mode === "standalone" && this.standaloneAuth) {
|
|
392
503
|
await this.standaloneAuth.logout();
|
|
393
504
|
}
|
|
505
|
+
if (this.mode === "react-native" && this.reactNativeAuth) {
|
|
506
|
+
await this.reactNativeAuth.logout();
|
|
507
|
+
}
|
|
394
508
|
if (this.mode === "iframe" && this.iframeAuth) {
|
|
395
509
|
this.iframeAuth.stop();
|
|
396
510
|
}
|
|
@@ -478,6 +592,13 @@ var AuthManager = class {
|
|
|
478
592
|
this.notify();
|
|
479
593
|
return newTokens.accessToken;
|
|
480
594
|
}
|
|
595
|
+
if (this.mode === "react-native" && this.reactNativeAuth) {
|
|
596
|
+
const newTokens = await this.reactNativeAuth.refreshToken();
|
|
597
|
+
this.tokens = newTokens;
|
|
598
|
+
this.scheduleTokenRefresh(newTokens);
|
|
599
|
+
this.notify();
|
|
600
|
+
return newTokens.accessToken;
|
|
601
|
+
}
|
|
481
602
|
if (this.mode === "iframe" && this.iframeAuth) {
|
|
482
603
|
const newTokens = await this.iframeAuth.requestTokenRefresh();
|
|
483
604
|
return newTokens.accessToken;
|