Skip to content

Commit c76829a

Browse files
committed
fix: handle cases where userid might be undefined (unverified user)
1 parent 98e7d5d commit c76829a

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/routes/onboardingRoutes.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,39 @@ router.post('/otp/verify', async (req: Request, res: Response): Promise<void> =>
4343
return;
4444
}
4545

46-
const user = await getUserByPhoneNumber(phone);
46+
let user = await getUserByPhoneNumber(phone);
47+
48+
if (user) {
49+
// User exists - mark them as verified if they weren't already
50+
if (user.verification_status === 'unverified') {
51+
const verifyResult = await verifyUser(user.id);
52+
if (verifyResult.success && verifyResult.user) {
53+
user = verifyResult.user;
54+
} else {
55+
res.status(500).json({ error: 'Failed to verify user' });
56+
return;
57+
}
58+
}
4759

48-
if (!user) {
49-
// User doesn't exist, store phone and prompt for onboarding
50-
await prisma.verifiedPhone.upsert({
51-
where: { phone },
52-
update: {},
53-
create: { phone },
60+
const token = jwt.sign({ userId: user.id }, JWT_SECRET, { expiresIn: '7d' });
61+
res.json({
62+
token,
63+
user,
64+
isNewUser: false,
65+
message: 'User verified successfully'
5466
});
67+
} else {
68+
await prisma.verifiedPhone.create({
69+
data: { phone },
70+
});
71+
5572
res.json({
5673
success: true,
5774
isNewUser: true,
58-
message: 'OTP verified. User does not exist, please complete onboarding.',
75+
verifiedPhone: phone,
76+
message: 'OTP verified. Please complete onboarding.',
5977
});
60-
return;
6178
}
62-
63-
// User exists, update verification status
64-
if (user.verification_status !== 'verified') {
65-
await verifyUser(user.id);
66-
}
67-
68-
const updatedUser = await getUserByPhoneNumber(phone);
69-
const token = jwt.sign({ userId: user.id }, JWT_SECRET, { expiresIn: '7d' });
70-
71-
res.json({
72-
token,
73-
user: updatedUser,
74-
isNewUser: false,
75-
message: 'User verified successfully',
76-
});
7779
} catch (error) {
7880
console.error(error);
7981
res.status(500).json({ error: 'Verification failed' });
@@ -111,6 +113,7 @@ router.post('/onboard', async (req: Request, res: Response) => {
111113
}
112114
}
113115

116+
// Create user as verified since they completed OTP verification
114117
const user = await createUser({
115118
name,
116119
dob,
@@ -119,7 +122,7 @@ router.post('/onboard', async (req: Request, res: Response) => {
119122
gender: gender as Gender,
120123
profile_pic: profilePicUrl,
121124
preferred_language: preferred_language as Language,
122-
verification_status: 'verified'
125+
verification_status: 'verified' // Always verified if they reach this point
123126
});
124127

125128
const token = jwt.sign({ userId: user.id }, JWT_SECRET, { expiresIn: '7d' });
@@ -135,6 +138,7 @@ router.post('/onboard', async (req: Request, res: Response) => {
135138
}
136139
});
137140

141+
// New route to upgrade unverified user to verified (when they complete OTP later)
138142
router.post('/upgrade-to-verified', async (req: Request, res: Response) => {
139143
const { phone, code } = req.body;
140144

@@ -165,13 +169,13 @@ router.post('/upgrade-to-verified', async (req: Request, res: Response) => {
165169

166170
// Upgrade user to verified
167171
const verifyResult = await verifyUser(user.id);
168-
if (!verifyResult.success) {
172+
if (!verifyResult.success || !verifyResult.user) {
169173
res.status(500).json({ error: 'Failed to verify user' });
170174
return;
171175
}
172176

173177
// Generate new token
174-
const token = jwt.sign({ userId: user.id }, JWT_SECRET, { expiresIn: '7d' });
178+
const token = jwt.sign({ userId: verifyResult.user.id }, JWT_SECRET, { expiresIn: '7d' });
175179

176180
res.json({
177181
token,

0 commit comments

Comments
 (0)