Skip to content

Commit c9cc790

Browse files
Copilothotlong
andcommitted
Improve type safety and ID generation robustness
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 81c7b25 commit c9cc790

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/adapter/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ export interface ObjectQLAdapterConfig {
1515
ql: ObjectQLClient;
1616
}
1717

18+
/**
19+
* Generate a unique ID for entities
20+
* Prefers crypto.randomUUID, falls back to timestamp-based ID
21+
*/
22+
function generateId(): string {
23+
// Try crypto.randomUUID first (available in modern runtimes)
24+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
25+
return crypto.randomUUID();
26+
}
27+
28+
// Fallback: timestamp + random number + counter for better uniqueness
29+
const timestamp = Date.now().toString(36);
30+
const random = Math.random().toString(36).substring(2, 15);
31+
const counter = (Math.random() * 1000000).toString(36);
32+
return `${timestamp}-${random}-${counter}`;
33+
}
34+
1835
export function createObjectQLAdapter(config: ObjectQLAdapterConfig): Adapter {
1936
const { ql } = config;
2037

@@ -180,7 +197,7 @@ export function createObjectQLAdapter(config: ObjectQLAdapterConfig): Adapter {
180197
async createVerificationToken(data: AdapterVerificationToken): Promise<AdapterVerificationToken> {
181198
const token = await ql.entity('VerificationToken').create({
182199
data: {
183-
id: data.id || crypto.randomUUID?.() || `${Date.now()}-${Math.random()}`,
200+
id: data.id || generateId(),
184201
identifier: data.identifier,
185202
token: data.token,
186203
expiresAt: data.expiresAt,

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function createAuthPlugin(config: AuthPluginConfig = {}): ObjectStackPlug
122122
}
123123

124124
// Re-export key types and utilities
125-
export type { ObjectStackAuthServerConfig } from './server/index.js';
125+
export type { ObjectStackAuthServerConfig, ObjectOSPermissions } from './server/index.js';
126126
export type { ObjectStackSession } from './server/index.js';
127127
export { createAuthServer, getSession } from './server/index.js';
128128

@@ -159,6 +159,7 @@ export type {
159159
PasswordResetConfirm,
160160
EmailVerification,
161161
AuthErrorCode,
162+
ObjectOSPermissions,
162163
} from './types.js';
163164
export { AuthError } from './types.js';
164165

src/server/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import type { BetterAuthOptions } from 'better-auth';
33
import type { ObjectQLClient } from '@objectstack/ql';
44
import { createObjectQLAdapter } from '../adapter/index.js';
55

6+
/**
7+
* ObjectOS Permissions type
8+
* Can be customized based on your RBAC implementation
9+
*/
10+
export type ObjectOSPermissions = string[] | Record<string, boolean> | null;
11+
612
/**
713
* Server-side configuration for ObjectStack Auth Plugin
814
*/
@@ -11,9 +17,9 @@ export interface ObjectStackAuthServerConfig {
1117
secret?: string;
1218
baseURL?: string;
1319
trustedOrigins?: string[];
14-
emailProvider?: any; // Better-Auth email provider
15-
socialProviders?: any[]; // Better-Auth social providers
16-
onGetPermissions?: (userId: string) => Promise<any>;
20+
emailProvider?: BetterAuthOptions['emailAndPassword'];
21+
socialProviders?: BetterAuthOptions['socialProviders'];
22+
onGetPermissions?: (userId: string) => Promise<ObjectOSPermissions>;
1723
}
1824

1925
/**
@@ -137,7 +143,7 @@ export interface ObjectStackSession {
137143
name?: string;
138144
image?: string;
139145
emailVerified: boolean;
140-
permissions?: any; // ObjectOS permissions
146+
permissions?: ObjectOSPermissions;
141147
};
142148
session: {
143149
id: string;

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ export interface VerificationToken {
6161
updatedAt: Date;
6262
}
6363

64+
/**
65+
* ObjectOS Permissions
66+
* Can be an array of permission strings or a map of permissions to boolean values
67+
*/
68+
export type ObjectOSPermissions = string[] | Record<string, boolean> | null;
69+
6470
/**
6571
* Extended user with ObjectOS permissions
6672
*/
6773
export interface UserWithPermissions extends User {
68-
permissions?: string[] | null;
74+
permissions?: ObjectOSPermissions;
6975
}
7076

7177
/**

0 commit comments

Comments
 (0)