Skip to content

Commit 0369bed

Browse files
committed
Complete user sign in
1 parent ded5fce commit 0369bed

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/services/queries/users.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,25 @@ import { client } from "$services/redis";
33
import type { CreateUserAttrs } from "$services/types";
44
import { genId } from "$services/utils";
55

6-
export const getUserByUsername = async (username: string) => {};
6+
export const getUserByUsername = async (username: string) => {
7+
// use username variable
8+
// look up if we can find the decimal id of the user
9+
const decimalId = await client.zScore(usernamesKey(), username);
10+
11+
// check if there is an id
12+
if (!decimalId) {
13+
throw new Error("User does not exist!");
14+
}
15+
16+
// convert the decimal to base16
17+
const id = decimalId.toString(16);
18+
19+
// find the user
20+
const user = await client.hGetAll(usersKey(id));
21+
22+
// deserialize the user and return
23+
return deserialize(id, user);
24+
};
725

826
export const getUserById = async (id: string) => {
927
const user = await client.hGetAll(usersKey(id));
@@ -19,7 +37,7 @@ export const createUser = async (attrs: CreateUserAttrs) => {
1937

2038
await client.hSet(usersKey(id), serialize(attrs));
2139
await client.sAdd(usernamesUniqueKey(), attrs.username);
22-
await client.zAdd(usernamesUniqueKey(), {
40+
await client.zAdd(usernamesKey(), {
2341
value: attrs.username,
2442
score: parseInt(id, 16), // we converted a base 16 to a base 10 by implementing the parInt function
2543
});

0 commit comments

Comments
 (0)