Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit 9cd3f7e

Browse files
Updated users model
1 parent bf95701 commit 9cd3f7e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

models/users.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function userToJSON(user: User): UserJSON {
1919
export async function getUsersCountWithEmail(email: string) {
2020
const total = (await global.db<User>("users")
2121
.where("email", email)
22-
.count("*", {as: "total"})
22+
.count("*", { as: "total" })
2323
)[0].total;
2424
return Number(total);
2525
}
@@ -43,4 +43,25 @@ export async function saveUser(user: User) {
4343
return getUserByID(insertId[0]) as User;
4444
}
4545

46+
export async function checkUserForAuthToken(userId: number, token: string) {
47+
const total = (await global.db("user_auth_tokens")
48+
.where("user_id", userId)
49+
.where("token", token)
50+
.count("*", { as: "total" })
51+
)[0].total;
52+
return Number(total) != 0;
53+
}
54+
55+
export async function addUserAuthToken(userId: number, token: string) {
56+
if (await checkUserForAuthToken(userId, token)) {
57+
throw new Error("User already has this auth token.");
58+
}
59+
60+
const insertId = await global.db("user_auth_tokens").insert({
61+
user_id: userId,
62+
token
63+
});
64+
return insertId.length == 1;
65+
}
66+
4667
export default () => global.db<User>("users");

0 commit comments

Comments
 (0)