@authgate/browser 0.4.0 → 0.5.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/dist/index.d.ts +29 -0
- package/dist/index.js +38 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -80,4 +80,33 @@ export declare function authFetch(input: RequestInfo | URL, init?: RequestInit,
|
|
|
80
80
|
* any reason (unauthorized, expired session, or error).
|
|
81
81
|
*/
|
|
82
82
|
export declare function refreshSession(audience?: string): Promise<boolean>;
|
|
83
|
+
export type CurrentUser = {
|
|
84
|
+
id: string;
|
|
85
|
+
email: string;
|
|
86
|
+
username: string;
|
|
87
|
+
/** Whether the user account is disabled */
|
|
88
|
+
disabled: boolean;
|
|
89
|
+
/** Account creation time (ISO 8601) */
|
|
90
|
+
created_at: string;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Fetches the currently authenticated user's identity.
|
|
94
|
+
*
|
|
95
|
+
* Behavior:
|
|
96
|
+
* - Calls GET /auth/user using credentials (cookies)
|
|
97
|
+
* - If the access token is expired, authFetch attempts a single refresh
|
|
98
|
+
* - If refresh succeeds, the request is retried once
|
|
99
|
+
* - If the user is not authenticated, returns null
|
|
100
|
+
*
|
|
101
|
+
* This function never throws for authentication failures.
|
|
102
|
+
*
|
|
103
|
+
* @param opts Optional options.
|
|
104
|
+
* @param opts.audience The audience for which authentication should be ensured
|
|
105
|
+
* (e.g. "app", "admin"). Defaults to "app".
|
|
106
|
+
*
|
|
107
|
+
* @returns The current user object if authenticated, or `null` otherwise.
|
|
108
|
+
*/
|
|
109
|
+
export declare function getCurrentUser(opts?: {
|
|
110
|
+
audience?: string;
|
|
111
|
+
}): Promise<CurrentUser | null>;
|
|
83
112
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -144,3 +144,41 @@ export async function refreshSession(audience = "app") {
|
|
|
144
144
|
}
|
|
145
145
|
return res.ok;
|
|
146
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Fetches the currently authenticated user's identity.
|
|
149
|
+
*
|
|
150
|
+
* Behavior:
|
|
151
|
+
* - Calls GET /auth/user using credentials (cookies)
|
|
152
|
+
* - If the access token is expired, authFetch attempts a single refresh
|
|
153
|
+
* - If refresh succeeds, the request is retried once
|
|
154
|
+
* - If the user is not authenticated, returns null
|
|
155
|
+
*
|
|
156
|
+
* This function never throws for authentication failures.
|
|
157
|
+
*
|
|
158
|
+
* @param opts Optional options.
|
|
159
|
+
* @param opts.audience The audience for which authentication should be ensured
|
|
160
|
+
* (e.g. "app", "admin"). Defaults to "app".
|
|
161
|
+
*
|
|
162
|
+
* @returns The current user object if authenticated, or `null` otherwise.
|
|
163
|
+
*/
|
|
164
|
+
export async function getCurrentUser(opts) {
|
|
165
|
+
let res;
|
|
166
|
+
try {
|
|
167
|
+
res = await authFetch("/auth/user", { method: "GET" }, { audience: opts?.audience ?? "app" });
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
if (res.status === 401) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
if (!res.ok) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
return (await res.json());
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
package/package.json
CHANGED