ideal-auth 0.6.0 → 0.6.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/auth-instance.d.ts +1 -1
- package/dist/auth.d.ts +13 -5
- package/dist/auth.js +11 -0
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
- package/skills/ideal-auth/SKILL.md +6 -2
package/dist/auth-instance.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ interface AuthInstanceDeps<TUser extends AnyUser> {
|
|
|
7
7
|
rememberMaxAge: number;
|
|
8
8
|
cookieOptions: ConfigurableCookieOptions;
|
|
9
9
|
resolveUser?: (id: string) => Promise<TUser | null | undefined>;
|
|
10
|
-
sessionFields?: (keyof TUser & string)[];
|
|
10
|
+
sessionFields?: readonly (keyof TUser & string)[];
|
|
11
11
|
hash?: HashInstance;
|
|
12
12
|
resolveUserByCredentials?: (credentials: Record<string, any>) => Promise<AnyUser | null | undefined>;
|
|
13
13
|
credentialKey: string;
|
package/dist/auth.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import type { AnyUser, AuthInstance,
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { AnyUser, AuthInstance, AuthConfig } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Create an auth factory.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* // resolveUser — user() returns SafeUser
|
|
7
|
+
* createAuth<SafeUser>({ resolveUser: ... })
|
|
8
|
+
*
|
|
9
|
+
* // sessionFields — user() returns Pick<DbUser, 'id' | 'email' | 'name'>
|
|
10
|
+
* const sessionFields = ['email', 'name'] as const;
|
|
11
|
+
* createAuth<DbUser, (typeof sessionFields)[number]>({ sessionFields, ... })
|
|
12
|
+
*/
|
|
13
|
+
export declare function createAuth<TUser extends AnyUser, K extends keyof TUser & string = keyof TUser & string>(config: AuthConfig<TUser>): () => AuthInstance<TUser, Pick<TUser, 'id' | K>>;
|
package/dist/auth.js
CHANGED
|
@@ -4,6 +4,17 @@ const SESSION_DEFAULTS = {
|
|
|
4
4
|
maxAge: 60 * 60 * 24 * 7, // 7 days
|
|
5
5
|
rememberMaxAge: 60 * 60 * 24 * 30, // 30 days
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Create an auth factory.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // resolveUser — user() returns SafeUser
|
|
12
|
+
* createAuth<SafeUser>({ resolveUser: ... })
|
|
13
|
+
*
|
|
14
|
+
* // sessionFields — user() returns Pick<DbUser, 'id' | 'email' | 'name'>
|
|
15
|
+
* const sessionFields = ['email', 'name'] as const;
|
|
16
|
+
* createAuth<DbUser, (typeof sessionFields)[number]>({ sessionFields, ... })
|
|
17
|
+
*/
|
|
7
18
|
export function createAuth(config) {
|
|
8
19
|
if (!config.secret || config.secret.length < 32) {
|
|
9
20
|
throw new Error('secret must be at least 32 characters');
|
package/dist/types.d.ts
CHANGED
|
@@ -72,7 +72,7 @@ export interface AuthConfigWithResolveUser<TUser extends AnyUser> extends AuthCo
|
|
|
72
72
|
export interface AuthConfigWithSessionFields<TUser extends AnyUser, K extends keyof TUser & string = keyof TUser & string> extends AuthConfigBase<TUser> {
|
|
73
73
|
/** Cannot use `resolveUser` together with `sessionFields`. */
|
|
74
74
|
resolveUser?: never;
|
|
75
|
-
sessionFields: K[];
|
|
75
|
+
sessionFields: readonly K[];
|
|
76
76
|
}
|
|
77
77
|
export type AuthConfig<TUser extends AnyUser = AnyUser> = AuthConfigWithResolveUser<TUser> | AuthConfigWithSessionFields<TUser>;
|
|
78
78
|
export interface HashConfig {
|
package/package.json
CHANGED
|
@@ -126,13 +126,17 @@ const user = await auth().user(); // Type: SafeUser | null
|
|
|
126
126
|
|
|
127
127
|
**Cookie-backed (`sessionFields`):** Cookie stores user ID + declared fields. `user()` returns `Pick<TUser, 'id' | K>` — password excluded from the type.
|
|
128
128
|
|
|
129
|
+
Define fields once as `const` and derive the type with `(typeof sessionFields)[number]`:
|
|
130
|
+
|
|
129
131
|
```typescript
|
|
130
132
|
type DbUser = { id: string; email: string; name: string; role: string; password: string };
|
|
131
133
|
|
|
132
|
-
const
|
|
134
|
+
const sessionFields = ['email', 'name', 'role'] as const;
|
|
135
|
+
|
|
136
|
+
const auth = createAuth<DbUser, (typeof sessionFields)[number]>({
|
|
133
137
|
secret: process.env.IDEAL_AUTH_SECRET!,
|
|
134
138
|
cookie: createCookieBridge(),
|
|
135
|
-
sessionFields
|
|
139
|
+
sessionFields,
|
|
136
140
|
resolveUserByCredentials: async (creds) => db.user.findFirst({ where: { email: creds.email } }),
|
|
137
141
|
hash,
|
|
138
142
|
});
|