@molecule/app-auth 1.8.2 → 2.0.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.
@@ -3,9 +3,16 @@
3
3
  *
4
4
  * These delegate to the bonded auth client retrieved via `getClient()`.
5
5
  *
6
+ * Every wrapper that hands back a profile takes the profile type as a type
7
+ * argument — `login<SessionUser>(…)`, `register<TExtra, SessionUser>(…)` — so an
8
+ * app that bonded a client with its own profile reads that profile from the
9
+ * result. Without it the default, `UserProfile`, applies: its `id: string` is
10
+ * the contract of the default configuration, not a value the client invents for
11
+ * a custom one.
12
+ *
6
13
  * @module
7
14
  */
8
- import type { AuthResult, LoginCredentials, RegisterData, RegisterExtraShape, UserProfile } from './types.js';
15
+ import type { AuthProfileConstraint, AuthResult, LoginCredentials, RegisterData, RegisterExtraShape, UserProfile } from './types.js';
9
16
  /**
10
17
  * Checks if the current user is authenticated.
11
18
  *
@@ -14,16 +21,24 @@ import type { AuthResult, LoginCredentials, RegisterData, RegisterExtraShape, Us
14
21
  export declare const isAuthenticated: () => boolean;
15
22
  /**
16
23
  * Returns the current user profile, or `null` if not authenticated.
24
+ *
25
+ * @template T - The user profile the bonded client returns: a JSON object type
26
+ * of named fields (see {@link AuthProfileConstraint}), including an app
27
+ * profile with no `id`. Defaults to {@link UserProfile}; name it at the call.
17
28
  * @returns The user profile, or `null` if not authenticated.
18
29
  */
19
- export declare const getUser: <T extends UserProfile = UserProfile>() => T | null;
30
+ export declare const getUser: <T extends AuthProfileConstraint<T> = UserProfile>() => T | null;
20
31
  /**
21
32
  * Logs in with the given credentials (email/username + password).
22
33
  *
34
+ * @template T - The user profile the bonded client returns. Pass the app's own
35
+ * profile when the client was created with one
36
+ * (`createJWTAuthClient<SessionUser>`), so `result.user` is typed for the
37
+ * pages; defaults to {@link UserProfile}.
23
38
  * @param credentials - Email/username and password.
24
39
  * @returns The auth result containing tokens and user profile.
25
40
  */
26
- export declare const login: (credentials: LoginCredentials) => Promise<AuthResult>;
41
+ export declare const login: <T extends AuthProfileConstraint<T> = UserProfile>(credentials: LoginCredentials) => Promise<AuthResult<T>>;
27
42
  /**
28
43
  * Logs out the current user, clearing tokens and session state.
29
44
  * @returns A promise that resolves when logout completes.
@@ -32,10 +47,17 @@ export declare const logout: () => Promise<void>;
32
47
  /**
33
48
  * Registers a new user account.
34
49
  *
50
+ * The registration extra fields stay the FIRST type argument, exactly as before
51
+ * (`register<{ username: string }>(data)` keeps compiling); the profile is the
52
+ * second one (`register<{ username: string }, SessionUser>(data)`).
53
+ *
35
54
  * @template TExtra - The app's own registration fields, at the top level
36
55
  * (`{ username: string }` for a RealWorld-shaped API); defaults to none.
56
+ * @template T - The user profile the bonded client returns. Pass the app's own
57
+ * profile when the client was created with one; defaults to
58
+ * {@link UserProfile}.
37
59
  * @param data - Registration data (email, password, and any `TExtra` fields).
38
60
  * @returns The auth result containing tokens and user profile.
39
61
  */
40
- export declare const register: <TExtra extends RegisterExtraShape<TExtra> = Record<never, never>>(data: RegisterData<TExtra>) => Promise<AuthResult>;
62
+ export declare const register: <TExtra extends RegisterExtraShape<TExtra> = Record<never, never>, T extends AuthProfileConstraint<T> = UserProfile>(data: RegisterData<TExtra>) => Promise<AuthResult<T>>;
41
63
  //# sourceMappingURL=utilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACV,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,WAAW,EACZ,MAAM,YAAY,CAAA;AAEnB;;;;GAIG;AACH,eAAO,MAAM,eAAe,QAAO,OAAwC,CAAA;AAE3E;;;GAGG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,WAAW,GAAG,WAAW,OAAK,CAAC,GAAG,IAAgC,CAAA;AAEpG;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAAI,aAAa,gBA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C,UAAU,CAAC,CAAC,CAAC,CAA0C,CAAA"}
package/dist/utilities.js CHANGED
@@ -3,6 +3,13 @@
3
3
  *
4
4
  * These delegate to the bonded auth client retrieved via `getClient()`.
5
5
  *
6
+ * Every wrapper that hands back a profile takes the profile type as a type
7
+ * argument — `login<SessionUser>(…)`, `register<TExtra, SessionUser>(…)` — so an
8
+ * app that bonded a client with its own profile reads that profile from the
9
+ * result. Without it the default, `UserProfile`, applies: its `id: string` is
10
+ * the contract of the default configuration, not a value the client invents for
11
+ * a custom one.
12
+ *
6
13
  * @module
7
14
  */
8
15
  import { getClient } from './provider.js';
@@ -14,16 +21,24 @@ import { getClient } from './provider.js';
14
21
  export const isAuthenticated = () => getClient().isAuthenticated();
15
22
  /**
16
23
  * Returns the current user profile, or `null` if not authenticated.
24
+ *
25
+ * @template T - The user profile the bonded client returns: a JSON object type
26
+ * of named fields (see {@link AuthProfileConstraint}), including an app
27
+ * profile with no `id`. Defaults to {@link UserProfile}; name it at the call.
17
28
  * @returns The user profile, or `null` if not authenticated.
18
29
  */
19
30
  export const getUser = () => getClient().getUser();
20
31
  /**
21
32
  * Logs in with the given credentials (email/username + password).
22
33
  *
34
+ * @template T - The user profile the bonded client returns. Pass the app's own
35
+ * profile when the client was created with one
36
+ * (`createJWTAuthClient<SessionUser>`), so `result.user` is typed for the
37
+ * pages; defaults to {@link UserProfile}.
23
38
  * @param credentials - Email/username and password.
24
39
  * @returns The auth result containing tokens and user profile.
25
40
  */
26
- export const login = (credentials) => getClient().login(credentials);
41
+ export const login = async (credentials) => getClient().login(credentials);
27
42
  /**
28
43
  * Logs out the current user, clearing tokens and session state.
29
44
  * @returns A promise that resolves when logout completes.
@@ -32,10 +47,17 @@ export const logout = () => getClient().logout();
32
47
  /**
33
48
  * Registers a new user account.
34
49
  *
50
+ * The registration extra fields stay the FIRST type argument, exactly as before
51
+ * (`register<{ username: string }>(data)` keeps compiling); the profile is the
52
+ * second one (`register<{ username: string }, SessionUser>(data)`).
53
+ *
35
54
  * @template TExtra - The app's own registration fields, at the top level
36
55
  * (`{ username: string }` for a RealWorld-shaped API); defaults to none.
56
+ * @template T - The user profile the bonded client returns. Pass the app's own
57
+ * profile when the client was created with one; defaults to
58
+ * {@link UserProfile}.
37
59
  * @param data - Registration data (email, password, and any `TExtra` fields).
38
60
  * @returns The auth result containing tokens and user profile.
39
61
  */
40
- export const register = (data) => getClient().register(data);
62
+ export const register = async (data) => getClient().register(data);
41
63
  //# sourceMappingURL=utilities.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AASzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAY,EAAE,CAAC,SAAS,EAAE,CAAC,eAAe,EAAE,CAAA;AAE3E;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAAkD,EAAE,CAAC,SAAS,EAAK,CAAC,OAAO,EAAE,CAAA;AAEpG;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,WAA6B,EAAuB,EAAE,CAC1E,SAAS,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;AAEhC;;;GAGG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,GAAkB,EAAE,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,CAAA;AAE/D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,IAA0B,EACL,EAAE,CAAC,SAAS,EAAuB,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA"}
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAUzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAY,EAAE,CAAC,SAAS,EAAE,CAAC,eAAe,EAAE,CAAA;AAE3E;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAA+D,EAAE,CACtF,SAAS,EAAK,CAAC,OAAO,EAAE,CAAA;AAE1B;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,KAAK,EACxB,WAA6B,EACL,EAAE,CAAC,SAAS,EAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;AAE9D;;;GAGG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,GAAkB,EAAE,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,CAAA;AAE/D;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAI3B,IAA0B,EACF,EAAE,CAAC,SAAS,EAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-auth",
3
- "version": "1.8.2",
3
+ "version": "2.0.0",
4
4
  "description": "Client-side authentication interface for molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",