Skip to content

Commit 7181c1c

Browse files
authored
fix(auth): correct email verification (NangoHQ#2537)
## Describe your changes ## Issue ticket number and link ## Checklist before requesting a review (skip if just adding/editing APIs & templates) - [ ] I added tests, otherwise the reason is: - [ ] I added observability, otherwise the reason is: - [ ] I added analytics, otherwise the reason is:
1 parent 1c106a2 commit 7181c1c

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

packages/server/lib/controllers/v1/account/validateEmailAndLogin.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const validation = z
1515

1616
export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (req, res) => {
1717
const emptyQuery = requireEmptyQuery(req);
18-
1918
if (emptyQuery) {
2019
res.status(400).send({ error: { code: 'invalid_query_params', errors: zodErrorToHTTP(emptyQuery.error) } });
2120
return;
@@ -32,7 +31,7 @@ export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (
3231

3332
const { token } = val.data;
3433

35-
const tokenResponse = await userService.getUserAndAccountByToken(token);
34+
const tokenResponse = await userService.getUserByToken(token);
3635

3736
if (tokenResponse.isErr()) {
3837
const error = tokenResponse.error;
@@ -54,11 +53,11 @@ export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (
5453
return;
5554
}
5655

57-
const userAndAccount = tokenResponse.value;
56+
const user = tokenResponse.value;
5857

59-
await userService.verifyUserEmail(userAndAccount.user_id);
58+
await userService.verifyUserEmail(user.id);
6059

61-
const { account_id, email } = userAndAccount;
60+
const { account_id, email } = user;
6261

6362
void analytics.track(AnalyticsTypes.ACCOUNT_CREATED, account_id, {}, { email });
6463

@@ -72,13 +71,13 @@ export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (
7271
}
7372
}
7473

75-
req.login(userAndAccount, function (err) {
74+
req.login(user, function (err) {
7675
if (err) {
7776
logger.error('Error logging in user');
7877
res.status(500).send({ error: { code: 'error_logging_in', message: 'There was a problem logging in the user. Please reach out to support.' } });
7978
return;
8079
}
8180

82-
res.status(200).send({ user: userToAPI(userAndAccount) });
81+
res.status(200).send({ user: userToAPI(user) });
8382
});
8483
});

packages/shared/lib/services/user.service.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as uuid from 'uuid';
33
import type { Result } from '@nangohq/utils';
44
import { Ok, Err } from '@nangohq/utils';
55
import type { User } from '../models/Admin.js';
6-
import type { DBTeam, DBUser } from '@nangohq/types';
6+
import type { DBUser } from '@nangohq/types';
77

88
const VERIFICATION_EMAIL_EXPIRATION = 3 * 24 * 60 * 60 * 1000;
99

@@ -20,13 +20,8 @@ class UserService {
2020
return result || null;
2121
}
2222

23-
async getUserAndAccountByToken(token: string): Promise<Result<User & DBTeam & { account_id: number; user_id: number }>> {
24-
const result = await db.knex
25-
.select('*', '_nango_accounts.id as account_id', '_nango_users.id as user_id')
26-
.from<User>(`_nango_users`)
27-
.join('_nango_accounts', '_nango_accounts.id', '_nango_users.account_id')
28-
.where({ email_verification_token: token })
29-
.first();
23+
async getUserByToken(token: string): Promise<Result<DBUser>> {
24+
const result = await db.knex.select('_nango_users.*').from<User>(`_nango_users`).where({ email_verification_token: token }).first();
3025

3126
if (result) {
3227
const expired = new Date(result.email_verification_token_expires_at).getTime() < new Date().getTime();

0 commit comments

Comments
 (0)