1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
import argon2 from "argon2";
import express from "express";
import { users, linkedDevices } from "../db/schema";
import { db } from "../db/db";
import { Infer, z } from "zod";
import { signJwt } from "../account/jwt";
import { eq } from "drizzle-orm";
import { logger } from "../lib/pino";
/** Validates signin request body with email and password */
const SigninBodySchema = z.object({
email: z
.string()
.transform((val) => val.trim())
.pipe(
z
.email({ error: "Invalid email" })
.nonempty({ error: "Email can't be empty" }),
),
password: z.string(),
});
const router: express.Router = express.Router();
router.post("/signin", async (req, res) => {
const body: Infer<typeof SigninBodySchema> = req.body;
logger.info({ email: body.email }, "Signin attempt initiated");
const parsed = SigninBodySchema.safeParse(body);
if (!parsed.success) {
logger.warn(
{ email: body.email, error: parsed.error },
"Signin validation failed",
);
res.send({
success: false,
reason: parsed.error,
token: "",
});
return;
}
const existingUser = (
await db.select().from(users).where(eq(users.email, body.email)).limit(1)
)[0];
if (!existingUser) {
logger.warn({ email: body.email }, "Signin failed: user not found");
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
const validPassword = await argon2.verify(
existingUser.password,
body.password,
);
if (!validPassword) {
logger.warn(
{ email: body.email, userId: existingUser.id },
"Signin failed: invalid password",
);
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
const jwt = await signJwt(
{ id: existingUser.id, type: "parent" },
"urn:buddy:users",
);
logger.info(
{ userId: existingUser.id, email: body.email },
"User signin successful",
);
res.send({
success: true,
token: jwt,
reason: "",
});
});
router.post("/kid/link", async (req, res) => {
const body: Infer<typeof SigninBodySchema> = req.body;
const parsed = SigninBodySchema.safeParse(body);
if (!parsed.success) {
logger.warn({ error: parsed.error }, "Kid link validation failed");
res.send({
success: false,
reason: parsed.error,
token: "",
});
return;
}
logger.info({ email: parsed.data.email }, "Kid link request initiated");
const existingUser = (
await db
.select()
.from(users)
.where(eq(users.email, parsed.data.email))
.limit(1)
)[0];
if (!existingUser) {
logger.warn(
{ email: parsed.data.email },
"Kid link failed: user not found",
);
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
logger.debug({ email: parsed.data.email }, "User found for kid link");
const validPassword = await argon2.verify(
existingUser.password,
parsed.data.password,
);
if (!validPassword) {
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
if (!existingUser.emailVerified) {
res.send({
success: false,
reason: "You must verify your email in the parent app before using Buddy",
});
return;
}
const newDevice = (
await db
.insert(linkedDevices)
.values({ parentId: existingUser.id })
.returning({ id: linkedDevices.id })
)[0];
const jwt = await signJwt(
{ id: newDevice!.id, type: "child" },
"urn:buddy:devices",
);
logger.info(
{ deviceId: newDevice!.id, parentId: existingUser.id },
"New child device linked successfully",
);
res.send({
success: true,
token: jwt,
reason: "",
});
});
export default router;
|