@uln/auth 0.1.0 → 0.3.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.
package/src/users.ts CHANGED
@@ -2,45 +2,45 @@ import { hashPassword, verifyPassword } from "./password.ts";
2
2
  import type { User, UserStore } from "./types.ts";
3
3
 
4
4
  export async function createUser(
5
- store: UserStore,
6
- input: { email: string; name: string; password?: string; isAdmin?: boolean },
5
+ store: UserStore,
6
+ input: { email: string; name: string; password?: string; isAdmin?: boolean },
7
7
  ): Promise<User> {
8
- const existing = await store.getUserByEmail(input.email);
9
- if (existing) throw new Error(`User with email ${input.email} already exists`);
8
+ const existing = await store.getUserByEmail(input.email);
9
+ if (existing) throw new Error(`User with email ${input.email} already exists`);
10
10
 
11
- const now = new Date().toISOString();
12
- const passwordHash = input.password ? await hashPassword(input.password) : await hashPassword(crypto.randomUUID());
13
- const user: User = {
14
- id: crypto.randomUUID(),
15
- email: input.email.toLowerCase(),
16
- name: input.name,
17
- passwordHash,
18
- isAdmin: input.isAdmin ?? false,
19
- emailVerified: input.isAdmin ?? false,
20
- createdAt: now,
21
- updatedAt: now,
22
- };
11
+ const now = new Date().toISOString();
12
+ const passwordHash = input.password ? await hashPassword(input.password) : await hashPassword(crypto.randomUUID());
13
+ const user: User = {
14
+ id: crypto.randomUUID(),
15
+ email: input.email.toLowerCase(),
16
+ name: input.name,
17
+ passwordHash,
18
+ isAdmin: input.isAdmin ?? false,
19
+ emailVerified: input.isAdmin ?? false,
20
+ createdAt: now,
21
+ updatedAt: now,
22
+ };
23
23
 
24
- await store.saveUser(user);
25
- return user;
24
+ await store.saveUser(user);
25
+ return user;
26
26
  }
27
27
 
28
28
  export async function verifyUserCredentials(store: UserStore, email: string, password: string): Promise<User | null> {
29
- const user = await store.getUserByEmail(email.toLowerCase());
30
- if (!user) return null;
29
+ const user = await store.getUserByEmail(email.toLowerCase());
30
+ if (!user) return null;
31
31
 
32
- const valid = await verifyPassword(password, user.passwordHash);
33
- return valid ? user : null;
32
+ const valid = await verifyPassword(password, user.passwordHash);
33
+ return valid ? user : null;
34
34
  }
35
35
 
36
36
  export async function changePassword(store: UserStore, userId: string, newPassword: string): Promise<void> {
37
- const user = await store.getUser(userId);
38
- if (!user) throw new Error(`User ${userId} not found`);
37
+ const user = await store.getUser(userId);
38
+ if (!user) throw new Error(`User ${userId} not found`);
39
39
 
40
- const updated: User = {
41
- ...user,
42
- passwordHash: await hashPassword(newPassword),
43
- updatedAt: new Date().toISOString(),
44
- };
45
- await store.saveUser(updated);
40
+ const updated: User = {
41
+ ...user,
42
+ passwordHash: await hashPassword(newPassword),
43
+ updatedAt: new Date().toISOString(),
44
+ };
45
+ await store.saveUser(updated);
46
46
  }