@reauth-dev/sdk 0.1.0 → 0.3.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/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  createReauthClient
3
- } from "./chunk-JX2J36FS.mjs";
3
+ } from "./chunk-5LFJ5PXQ.mjs";
4
+ import "./chunk-EY5LQCDG.mjs";
4
5
 
5
6
  // src/errors.ts
6
7
  var ReauthErrorCode = /* @__PURE__ */ ((ReauthErrorCode2) => {
@@ -1,14 +1,24 @@
1
- import { R as ReauthConfig, c as User } from '../types-D8oOYbeC.mjs';
1
+ import { R as ReauthConfig, o as User, D as DomainConfig, M as MagicLinkVerifyResult, G as GoogleOAuthStartResult, c as TwitterOAuthStartResult } from '../types-DKUKhCNE.mjs';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
  import { ReactNode } from 'react';
4
4
 
5
+ type UseAuthOptions = ReauthConfig & {
6
+ /**
7
+ * Interval in milliseconds to refresh the session. Set to 0 to disable.
8
+ * Default: 5 minutes (300000ms)
9
+ */
10
+ refreshInterval?: number;
11
+ };
5
12
  /**
6
13
  * React hook for authentication state management.
7
14
  *
8
15
  * @example
9
16
  * ```typescript
10
17
  * function MyComponent() {
11
- * const { user, loading, login, logout } = useAuth({ domain: 'yourdomain.com' });
18
+ * const { user, loading, login, logout } = useAuth({
19
+ * domain: 'yourdomain.com',
20
+ * refreshInterval: 60000, // Refresh every minute (optional, default: 5 min)
21
+ * });
12
22
  *
13
23
  * if (loading) return <div>Loading...</div>;
14
24
  * if (!user) return <button onClick={login}>Sign in</button>;
@@ -22,7 +32,7 @@ import { ReactNode } from 'react';
22
32
  * }
23
33
  * ```
24
34
  */
25
- declare function useAuth(config: ReauthConfig): {
35
+ declare function useAuth(config: UseAuthOptions): {
26
36
  login: () => void;
27
37
  logout: () => Promise<void>;
28
38
  refetch: () => Promise<void>;
@@ -33,6 +43,66 @@ declare function useAuth(config: ReauthConfig): {
33
43
  waitlistPosition: number | null;
34
44
  };
35
45
 
46
+ type HeadlessStep = "idle" | "magic_link_sent" | "google_started" | "twitter_started" | "completed";
47
+ type UseHeadlessAuthOptions = {
48
+ domain: string;
49
+ callbackUrl?: string;
50
+ timeout?: number;
51
+ };
52
+ /**
53
+ * React hook for headless authentication (custom UI login).
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * function LoginPage() {
58
+ * const {
59
+ * requestMagicLink,
60
+ * verifyMagicLink,
61
+ * startGoogleOAuth,
62
+ * loading,
63
+ * error,
64
+ * step,
65
+ * } = useHeadlessAuth({
66
+ * domain: 'yourdomain.com',
67
+ * callbackUrl: 'https://app.yourdomain.com/auth/verify',
68
+ * });
69
+ *
70
+ * if (step === 'magic_link_sent') {
71
+ * return <p>Check your email for a magic link!</p>;
72
+ * }
73
+ *
74
+ * if (step === 'completed') {
75
+ * return <p>Authenticated!</p>;
76
+ * }
77
+ *
78
+ * return (
79
+ * <div>
80
+ * <button onClick={() => requestMagicLink('user@example.com')}>
81
+ * Send Magic Link
82
+ * </button>
83
+ * <button onClick={startGoogleOAuth}>
84
+ * Sign in with Google
85
+ * </button>
86
+ * {error && <p>{error}</p>}
87
+ * </div>
88
+ * );
89
+ * }
90
+ * ```
91
+ */
92
+ declare function useHeadlessAuth(options: UseHeadlessAuthOptions): {
93
+ getConfig: () => Promise<DomainConfig>;
94
+ requestMagicLink: (email: string) => Promise<void>;
95
+ verifyMagicLink: (token: string) => Promise<MagicLinkVerifyResult>;
96
+ startGoogleOAuth: () => Promise<GoogleOAuthStartResult>;
97
+ startTwitterOAuth: () => Promise<TwitterOAuthStartResult>;
98
+ reset: () => void;
99
+ loading: boolean;
100
+ error: string | null;
101
+ step: HeadlessStep;
102
+ config: DomainConfig | null;
103
+ verifyResult: MagicLinkVerifyResult | null;
104
+ };
105
+
36
106
  type AuthContextType = {
37
107
  user: User | null;
38
108
  loading: boolean;
@@ -44,7 +114,7 @@ type AuthContextType = {
44
114
  refetch: () => Promise<void>;
45
115
  };
46
116
  type AuthProviderProps = {
47
- config: ReauthConfig;
117
+ config: UseAuthOptions;
48
118
  children: ReactNode;
49
119
  };
50
120
  /**
@@ -120,4 +190,4 @@ type ProtectedRouteProps = {
120
190
  */
121
191
  declare function ProtectedRoute({ children, fallback, onUnauthenticated, onWaitlist, }: ProtectedRouteProps): react_jsx_runtime.JSX.Element | null;
122
192
 
123
- export { AuthProvider, ProtectedRoute, useAuth, useAuthContext };
193
+ export { AuthProvider, ProtectedRoute, type UseAuthOptions, type UseHeadlessAuthOptions, useAuth, useAuthContext, useHeadlessAuth };
@@ -1,14 +1,24 @@
1
- import { R as ReauthConfig, c as User } from '../types-D8oOYbeC.js';
1
+ import { R as ReauthConfig, o as User, D as DomainConfig, M as MagicLinkVerifyResult, G as GoogleOAuthStartResult, c as TwitterOAuthStartResult } from '../types-DKUKhCNE.js';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
  import { ReactNode } from 'react';
4
4
 
5
+ type UseAuthOptions = ReauthConfig & {
6
+ /**
7
+ * Interval in milliseconds to refresh the session. Set to 0 to disable.
8
+ * Default: 5 minutes (300000ms)
9
+ */
10
+ refreshInterval?: number;
11
+ };
5
12
  /**
6
13
  * React hook for authentication state management.
7
14
  *
8
15
  * @example
9
16
  * ```typescript
10
17
  * function MyComponent() {
11
- * const { user, loading, login, logout } = useAuth({ domain: 'yourdomain.com' });
18
+ * const { user, loading, login, logout } = useAuth({
19
+ * domain: 'yourdomain.com',
20
+ * refreshInterval: 60000, // Refresh every minute (optional, default: 5 min)
21
+ * });
12
22
  *
13
23
  * if (loading) return <div>Loading...</div>;
14
24
  * if (!user) return <button onClick={login}>Sign in</button>;
@@ -22,7 +32,7 @@ import { ReactNode } from 'react';
22
32
  * }
23
33
  * ```
24
34
  */
25
- declare function useAuth(config: ReauthConfig): {
35
+ declare function useAuth(config: UseAuthOptions): {
26
36
  login: () => void;
27
37
  logout: () => Promise<void>;
28
38
  refetch: () => Promise<void>;
@@ -33,6 +43,66 @@ declare function useAuth(config: ReauthConfig): {
33
43
  waitlistPosition: number | null;
34
44
  };
35
45
 
46
+ type HeadlessStep = "idle" | "magic_link_sent" | "google_started" | "twitter_started" | "completed";
47
+ type UseHeadlessAuthOptions = {
48
+ domain: string;
49
+ callbackUrl?: string;
50
+ timeout?: number;
51
+ };
52
+ /**
53
+ * React hook for headless authentication (custom UI login).
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * function LoginPage() {
58
+ * const {
59
+ * requestMagicLink,
60
+ * verifyMagicLink,
61
+ * startGoogleOAuth,
62
+ * loading,
63
+ * error,
64
+ * step,
65
+ * } = useHeadlessAuth({
66
+ * domain: 'yourdomain.com',
67
+ * callbackUrl: 'https://app.yourdomain.com/auth/verify',
68
+ * });
69
+ *
70
+ * if (step === 'magic_link_sent') {
71
+ * return <p>Check your email for a magic link!</p>;
72
+ * }
73
+ *
74
+ * if (step === 'completed') {
75
+ * return <p>Authenticated!</p>;
76
+ * }
77
+ *
78
+ * return (
79
+ * <div>
80
+ * <button onClick={() => requestMagicLink('user@example.com')}>
81
+ * Send Magic Link
82
+ * </button>
83
+ * <button onClick={startGoogleOAuth}>
84
+ * Sign in with Google
85
+ * </button>
86
+ * {error && <p>{error}</p>}
87
+ * </div>
88
+ * );
89
+ * }
90
+ * ```
91
+ */
92
+ declare function useHeadlessAuth(options: UseHeadlessAuthOptions): {
93
+ getConfig: () => Promise<DomainConfig>;
94
+ requestMagicLink: (email: string) => Promise<void>;
95
+ verifyMagicLink: (token: string) => Promise<MagicLinkVerifyResult>;
96
+ startGoogleOAuth: () => Promise<GoogleOAuthStartResult>;
97
+ startTwitterOAuth: () => Promise<TwitterOAuthStartResult>;
98
+ reset: () => void;
99
+ loading: boolean;
100
+ error: string | null;
101
+ step: HeadlessStep;
102
+ config: DomainConfig | null;
103
+ verifyResult: MagicLinkVerifyResult | null;
104
+ };
105
+
36
106
  type AuthContextType = {
37
107
  user: User | null;
38
108
  loading: boolean;
@@ -44,7 +114,7 @@ type AuthContextType = {
44
114
  refetch: () => Promise<void>;
45
115
  };
46
116
  type AuthProviderProps = {
47
- config: ReauthConfig;
117
+ config: UseAuthOptions;
48
118
  children: ReactNode;
49
119
  };
50
120
  /**
@@ -120,4 +190,4 @@ type ProtectedRouteProps = {
120
190
  */
121
191
  declare function ProtectedRoute({ children, fallback, onUnauthenticated, onWaitlist, }: ProtectedRouteProps): react_jsx_runtime.JSX.Element | null;
122
192
 
123
- export { AuthProvider, ProtectedRoute, useAuth, useAuthContext };
193
+ export { AuthProvider, ProtectedRoute, type UseAuthOptions, type UseHeadlessAuthOptions, useAuth, useAuthContext, useHeadlessAuth };