@drmhse/authos-react 0.1.1 → 0.1.2

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.d.mts CHANGED
@@ -19,6 +19,12 @@ interface AuthOSProviderProps {
19
19
  * Optional: Provide an existing SsoClient instance instead of creating a new one
20
20
  */
21
21
  client?: SsoClient;
22
+ /**
23
+ * Optional: Initial session token from server-side (for SSR hydration).
24
+ * When provided, skips the initial loading state on the client.
25
+ * Typically passed from cookies() in Next.js server components.
26
+ */
27
+ initialSessionToken?: string;
22
28
  }
23
29
  /**
24
30
  * Authentication context state
@@ -122,8 +128,28 @@ interface ProtectProps {
122
128
  * );
123
129
  * }
124
130
  * ```
131
+ *
132
+ * @example With SSR token (Next.js App Router)
133
+ * ```tsx
134
+ * import { cookies } from 'next/headers';
135
+ * import { AuthOSProvider } from '@drmhse/authos-react';
136
+ *
137
+ * export default async function RootLayout({ children }) {
138
+ * const cookieStore = cookies();
139
+ * const token = cookieStore.get('authos_token')?.value;
140
+ *
141
+ * return (
142
+ * <AuthOSProvider
143
+ * config={{ baseURL: 'https://auth.example.com' }}
144
+ * initialSessionToken={token}
145
+ * >
146
+ * {children}
147
+ * </AuthOSProvider>
148
+ * );
149
+ * }
150
+ * ```
125
151
  */
126
- declare function AuthOSProvider({ config, children, client: externalClient }: AuthOSProviderProps): react_jsx_runtime.JSX.Element;
152
+ declare function AuthOSProvider({ config, children, client: externalClient, initialSessionToken }: AuthOSProviderProps): react_jsx_runtime.JSX.Element;
127
153
  /**
128
154
  * Hook to access the AuthOS context.
129
155
  * Must be used within an AuthOSProvider.
package/dist/index.d.ts CHANGED
@@ -19,6 +19,12 @@ interface AuthOSProviderProps {
19
19
  * Optional: Provide an existing SsoClient instance instead of creating a new one
20
20
  */
21
21
  client?: SsoClient;
22
+ /**
23
+ * Optional: Initial session token from server-side (for SSR hydration).
24
+ * When provided, skips the initial loading state on the client.
25
+ * Typically passed from cookies() in Next.js server components.
26
+ */
27
+ initialSessionToken?: string;
22
28
  }
23
29
  /**
24
30
  * Authentication context state
@@ -122,8 +128,28 @@ interface ProtectProps {
122
128
  * );
123
129
  * }
124
130
  * ```
131
+ *
132
+ * @example With SSR token (Next.js App Router)
133
+ * ```tsx
134
+ * import { cookies } from 'next/headers';
135
+ * import { AuthOSProvider } from '@drmhse/authos-react';
136
+ *
137
+ * export default async function RootLayout({ children }) {
138
+ * const cookieStore = cookies();
139
+ * const token = cookieStore.get('authos_token')?.value;
140
+ *
141
+ * return (
142
+ * <AuthOSProvider
143
+ * config={{ baseURL: 'https://auth.example.com' }}
144
+ * initialSessionToken={token}
145
+ * >
146
+ * {children}
147
+ * </AuthOSProvider>
148
+ * );
149
+ * }
150
+ * ```
125
151
  */
126
- declare function AuthOSProvider({ config, children, client: externalClient }: AuthOSProviderProps): react_jsx_runtime.JSX.Element;
152
+ declare function AuthOSProvider({ config, children, client: externalClient, initialSessionToken }: AuthOSProviderProps): react_jsx_runtime.JSX.Element;
127
153
  /**
128
154
  * Hook to access the AuthOS context.
129
155
  * Must be used within an AuthOSProvider.
package/dist/index.js CHANGED
@@ -6,15 +6,22 @@ var jsxRuntime = require('react/jsx-runtime');
6
6
 
7
7
  // src/context.tsx
8
8
  var AuthOSContext = react.createContext(null);
9
- function AuthOSProvider({ config, children, client: externalClient }) {
9
+ function AuthOSProvider({ config, children, client: externalClient, initialSessionToken }) {
10
10
  const clientRef = react.useRef(null);
11
11
  if (!clientRef.current) {
12
12
  clientRef.current = externalClient ?? new ssoSdk.SsoClient(config);
13
13
  }
14
14
  const client = clientRef.current;
15
+ const hasInitialToken = react.useRef(!!initialSessionToken);
16
+ react.useEffect(() => {
17
+ if (initialSessionToken && hasInitialToken.current) {
18
+ client.setSession({ access_token: initialSessionToken });
19
+ hasInitialToken.current = false;
20
+ }
21
+ }, [client, initialSessionToken]);
15
22
  const [user, setUser] = react.useState(null);
16
23
  const [organization, setOrganization] = react.useState(null);
17
- const [isLoading, setIsLoading] = react.useState(true);
24
+ const [isLoading, setIsLoading] = react.useState(!initialSessionToken);
18
25
  const refreshUser = react.useCallback(async () => {
19
26
  try {
20
27
  const profile = await client.user.getProfile();
package/dist/index.mjs CHANGED
@@ -1,19 +1,26 @@
1
- import { createContext, useRef, useState, useCallback, useEffect, useMemo, useContext } from 'react';
1
+ import { createContext, useRef, useEffect, useState, useCallback, useMemo, useContext } from 'react';
2
2
  import { SsoClient, SsoApiError } from '@drmhse/sso-sdk';
3
3
  export { AuthErrorCodes, SsoApiError, SsoClient } from '@drmhse/sso-sdk';
4
4
  import { jsx, jsxs } from 'react/jsx-runtime';
5
5
 
6
6
  // src/context.tsx
7
7
  var AuthOSContext = createContext(null);
8
- function AuthOSProvider({ config, children, client: externalClient }) {
8
+ function AuthOSProvider({ config, children, client: externalClient, initialSessionToken }) {
9
9
  const clientRef = useRef(null);
10
10
  if (!clientRef.current) {
11
11
  clientRef.current = externalClient ?? new SsoClient(config);
12
12
  }
13
13
  const client = clientRef.current;
14
+ const hasInitialToken = useRef(!!initialSessionToken);
15
+ useEffect(() => {
16
+ if (initialSessionToken && hasInitialToken.current) {
17
+ client.setSession({ access_token: initialSessionToken });
18
+ hasInitialToken.current = false;
19
+ }
20
+ }, [client, initialSessionToken]);
14
21
  const [user, setUser] = useState(null);
15
22
  const [organization, setOrganization] = useState(null);
16
- const [isLoading, setIsLoading] = useState(true);
23
+ const [isLoading, setIsLoading] = useState(!initialSessionToken);
17
24
  const refreshUser = useCallback(async () => {
18
25
  try {
19
26
  const profile = await client.user.getProfile();
package/dist/nextjs.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
+ import { SsoClient } from '@drmhse/sso-sdk';
2
3
 
3
4
  interface AuthMiddlewareConfig {
4
5
  /**
@@ -143,4 +144,86 @@ declare function auth(): Promise<AuthState>;
143
144
  */
144
145
  declare function getToken(cookieName?: string): Promise<string | null>;
145
146
 
146
- export { type AuthMiddlewareConfig, type AuthState, type AuthUser, auth, authMiddleware, currentUser, getToken };
147
+ /**
148
+ * Configuration for creating an AuthOS client optimized for Next.js
149
+ */
150
+ interface CreateAuthOSClientOptions {
151
+ /**
152
+ * Base URL of the SSO API service
153
+ */
154
+ baseURL: string;
155
+ /**
156
+ * Cookie name for storing the access token
157
+ * @default 'authos_token'
158
+ */
159
+ tokenCookie?: string;
160
+ /**
161
+ * Cookie domain (optional)
162
+ * Use this for subdomain-wide auth
163
+ */
164
+ domain?: string;
165
+ /**
166
+ * Cookie path
167
+ * @default '/'
168
+ */
169
+ path?: string;
170
+ /**
171
+ * SameSite cookie attribute
172
+ * @default 'lax'
173
+ */
174
+ sameSite?: 'strict' | 'lax' | 'none';
175
+ }
176
+ /**
177
+ * Create an SSO client optimized for Next.js with cookie storage.
178
+ *
179
+ * This client uses cookies instead of localStorage, which enables:
180
+ * - Server-side middleware auth checks
181
+ * - SSR hydration without loading flash
182
+ * - Shared auth state across tabs
183
+ *
184
+ * @example
185
+ * ```tsx
186
+ * // lib/authos.ts
187
+ * import { createAuthOSClient } from '@drmhse/authos-react/nextjs';
188
+ *
189
+ * export const authos = createAuthOSClient({
190
+ * baseURL: process.env.NEXT_PUBLIC_AUTHOS_URL!,
191
+ * });
192
+ * ```
193
+ *
194
+ * @example With custom cookie settings
195
+ * ```tsx
196
+ * import { createAuthOSClient } from '@drmhse/authos-react/nextjs';
197
+ *
198
+ * export const authos = createAuthOSClient({
199
+ * baseURL: 'https://auth.example.com',
200
+ * tokenCookie: 'my_app_token',
201
+ * domain: '.example.com', // Share across subdomains
202
+ * });
203
+ * ```
204
+ */
205
+ declare function createAuthOSClient(options: CreateAuthOSClientOptions): SsoClient;
206
+ /**
207
+ * Create an SSO client for use with SSR token fetching.
208
+ * Use this in server components where you need to make API calls
209
+ * with a token from cookies.
210
+ *
211
+ * @example
212
+ * ```tsx
213
+ * // app/dashboard/page.tsx
214
+ * import { createServerClient } from '@drmhse/authos-react/nextjs';
215
+ * import { getToken } from '@drmhse/authos-react/nextjs';
216
+ *
217
+ * export default async function DashboardPage() {
218
+ * const token = await getToken();
219
+ * const client = createServerClient(token);
220
+ *
221
+ * const user = token ? await client.user.getProfile() : null;
222
+ *
223
+ * return <div>Welcome, {user?.email}!</div>;
224
+ * }
225
+ * ```
226
+ */
227
+ declare function createServerClient(token: string, baseURL: string): SsoClient;
228
+
229
+ export { type AuthMiddlewareConfig, type AuthState, type AuthUser, type CreateAuthOSClientOptions, auth, authMiddleware, createAuthOSClient, createServerClient, currentUser, getToken };
package/dist/nextjs.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
+ import { SsoClient } from '@drmhse/sso-sdk';
2
3
 
3
4
  interface AuthMiddlewareConfig {
4
5
  /**
@@ -143,4 +144,86 @@ declare function auth(): Promise<AuthState>;
143
144
  */
144
145
  declare function getToken(cookieName?: string): Promise<string | null>;
145
146
 
146
- export { type AuthMiddlewareConfig, type AuthState, type AuthUser, auth, authMiddleware, currentUser, getToken };
147
+ /**
148
+ * Configuration for creating an AuthOS client optimized for Next.js
149
+ */
150
+ interface CreateAuthOSClientOptions {
151
+ /**
152
+ * Base URL of the SSO API service
153
+ */
154
+ baseURL: string;
155
+ /**
156
+ * Cookie name for storing the access token
157
+ * @default 'authos_token'
158
+ */
159
+ tokenCookie?: string;
160
+ /**
161
+ * Cookie domain (optional)
162
+ * Use this for subdomain-wide auth
163
+ */
164
+ domain?: string;
165
+ /**
166
+ * Cookie path
167
+ * @default '/'
168
+ */
169
+ path?: string;
170
+ /**
171
+ * SameSite cookie attribute
172
+ * @default 'lax'
173
+ */
174
+ sameSite?: 'strict' | 'lax' | 'none';
175
+ }
176
+ /**
177
+ * Create an SSO client optimized for Next.js with cookie storage.
178
+ *
179
+ * This client uses cookies instead of localStorage, which enables:
180
+ * - Server-side middleware auth checks
181
+ * - SSR hydration without loading flash
182
+ * - Shared auth state across tabs
183
+ *
184
+ * @example
185
+ * ```tsx
186
+ * // lib/authos.ts
187
+ * import { createAuthOSClient } from '@drmhse/authos-react/nextjs';
188
+ *
189
+ * export const authos = createAuthOSClient({
190
+ * baseURL: process.env.NEXT_PUBLIC_AUTHOS_URL!,
191
+ * });
192
+ * ```
193
+ *
194
+ * @example With custom cookie settings
195
+ * ```tsx
196
+ * import { createAuthOSClient } from '@drmhse/authos-react/nextjs';
197
+ *
198
+ * export const authos = createAuthOSClient({
199
+ * baseURL: 'https://auth.example.com',
200
+ * tokenCookie: 'my_app_token',
201
+ * domain: '.example.com', // Share across subdomains
202
+ * });
203
+ * ```
204
+ */
205
+ declare function createAuthOSClient(options: CreateAuthOSClientOptions): SsoClient;
206
+ /**
207
+ * Create an SSO client for use with SSR token fetching.
208
+ * Use this in server components where you need to make API calls
209
+ * with a token from cookies.
210
+ *
211
+ * @example
212
+ * ```tsx
213
+ * // app/dashboard/page.tsx
214
+ * import { createServerClient } from '@drmhse/authos-react/nextjs';
215
+ * import { getToken } from '@drmhse/authos-react/nextjs';
216
+ *
217
+ * export default async function DashboardPage() {
218
+ * const token = await getToken();
219
+ * const client = createServerClient(token);
220
+ *
221
+ * const user = token ? await client.user.getProfile() : null;
222
+ *
223
+ * return <div>Welcome, {user?.email}!</div>;
224
+ * }
225
+ * ```
226
+ */
227
+ declare function createServerClient(token: string, baseURL: string): SsoClient;
228
+
229
+ export { type AuthMiddlewareConfig, type AuthState, type AuthUser, type CreateAuthOSClientOptions, auth, authMiddleware, createAuthOSClient, createServerClient, currentUser, getToken };
package/dist/nextjs.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var ssoSdk = require('@drmhse/sso-sdk');
4
+
3
5
  var __create = Object.create;
4
6
  var __defProp = Object.defineProperty;
5
7
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -5871,8 +5873,39 @@ async function getToken(cookieName = "authos_token") {
5871
5873
  const cookieStore = await (0, import_headers.cookies)();
5872
5874
  return cookieStore.get(cookieName)?.value ?? null;
5873
5875
  }
5876
+ function createAuthOSClient(options) {
5877
+ const {
5878
+ baseURL,
5879
+ tokenCookie = "authos_token",
5880
+ domain,
5881
+ path = "/",
5882
+ sameSite = "lax"
5883
+ } = options;
5884
+ return new ssoSdk.SsoClient({
5885
+ baseURL,
5886
+ storage: new ssoSdk.CookieStorage({
5887
+ domain,
5888
+ path,
5889
+ secure: true,
5890
+ // Always use secure cookies for auth
5891
+ sameSite,
5892
+ // 30 days default max age
5893
+ maxAge: 30 * 24 * 60 * 60
5894
+ }),
5895
+ storagePrefix: tokenCookie
5896
+ });
5897
+ }
5898
+ function createServerClient(token, baseURL) {
5899
+ return new ssoSdk.SsoClient({
5900
+ baseURL,
5901
+ token
5902
+ // Initial token for server-side use
5903
+ });
5904
+ }
5874
5905
 
5875
5906
  exports.auth = auth;
5876
5907
  exports.authMiddleware = authMiddleware;
5908
+ exports.createAuthOSClient = createAuthOSClient;
5909
+ exports.createServerClient = createServerClient;
5877
5910
  exports.currentUser = currentUser;
5878
5911
  exports.getToken = getToken;
package/dist/nextjs.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { SsoClient, CookieStorage } from '@drmhse/sso-sdk';
2
+
1
3
  var __create = Object.create;
2
4
  var __defProp = Object.defineProperty;
3
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -5869,5 +5871,34 @@ async function getToken(cookieName = "authos_token") {
5869
5871
  const cookieStore = await (0, import_headers.cookies)();
5870
5872
  return cookieStore.get(cookieName)?.value ?? null;
5871
5873
  }
5874
+ function createAuthOSClient(options) {
5875
+ const {
5876
+ baseURL,
5877
+ tokenCookie = "authos_token",
5878
+ domain,
5879
+ path = "/",
5880
+ sameSite = "lax"
5881
+ } = options;
5882
+ return new SsoClient({
5883
+ baseURL,
5884
+ storage: new CookieStorage({
5885
+ domain,
5886
+ path,
5887
+ secure: true,
5888
+ // Always use secure cookies for auth
5889
+ sameSite,
5890
+ // 30 days default max age
5891
+ maxAge: 30 * 24 * 60 * 60
5892
+ }),
5893
+ storagePrefix: tokenCookie
5894
+ });
5895
+ }
5896
+ function createServerClient(token, baseURL) {
5897
+ return new SsoClient({
5898
+ baseURL,
5899
+ token
5900
+ // Initial token for server-side use
5901
+ });
5902
+ }
5872
5903
 
5873
- export { auth, authMiddleware, currentUser, getToken };
5904
+ export { auth, authMiddleware, createAuthOSClient, createServerClient, currentUser, getToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drmhse/authos-react",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "React and Next.js adapter for AuthOS authentication",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",