Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decide best practice for user ID in DB schema #163

Open
benmccann opened this issue Oct 16, 2024 · 1 comment
Open

Decide best practice for user ID in DB schema #163

benmccann opened this issue Oct 16, 2024 · 1 comment

Comments

@benmccann
Copy link
Member

Drizzle adder uses an integer. Lucia uses a string

It would be nice to be consistent both to promote best practices and to avoid users having to migrate their DB if they setup drizzle first and then come back later and setup lucia

Some people don't like ints because it reveals how many users they have

The Lucia integration is done app side. I'm not sure if that has downsides and would be better to be done in the database? It does use an extra dependency currently, but maybe there's a Node built-in we could leverage if we do want to do it app side?

@MilosNikolic
Copy link

Lucia v3 used generateIdFromEntropySize() function to generate user IDs.

Looking at Lucia codebase, this is what the function looked like:

import { encodeBase32LowerCaseNoPadding } from '@oslojs/encoding';

export function generateIdFromEntropySize(size: number): string {
	const buffer = crypto.getRandomValues(new Uint8Array(size));
	return encodeBase32LowerCaseNoPadding(buffer);
}

https://github.com/lucia-auth/lucia/blob/v3/packages/lucia/src/crypto.ts

Perhaps do the following:

import { encodeBase32LowerCaseNoPadding } from '@oslojs/encoding';

function generateIdFromEntropySize(size: number): string {
	const buffer = crypto.getRandomValues(new Uint8Array(size));
	return encodeBase32LowerCaseNoPadding(buffer);
}
export const user = sqliteTable('user', {
	id: text('id')
		.primaryKey()
		.$defaultFn(() => generateIdFromEntropySize(10)),
	age: integer('age'),
	username: text('username').notNull().unique(),
	passwordHash: text('password_hash').notNull()
});

This approach also doesn't require new dependencies since Lucia V4 approach already depends on Oslo.
Ideally, generateIdFromEntropySize() would be a helper function outside of schema file.

Oslo docs: https://oslo.js.org/reference/encoding/Base32Encoding/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants