Skip to content

Commit

Permalink
fix(auth): correct email verification (NangoHQ#2537)
Browse files Browse the repository at this point in the history
## 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:
  • Loading branch information
bodinsamuel authored Jul 23, 2024
1 parent 1c106a2 commit 7181c1c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const validation = z

export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (req, res) => {
const emptyQuery = requireEmptyQuery(req);

if (emptyQuery) {
res.status(400).send({ error: { code: 'invalid_query_params', errors: zodErrorToHTTP(emptyQuery.error) } });
return;
Expand All @@ -32,7 +31,7 @@ export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (

const { token } = val.data;

const tokenResponse = await userService.getUserAndAccountByToken(token);
const tokenResponse = await userService.getUserByToken(token);

if (tokenResponse.isErr()) {
const error = tokenResponse.error;
Expand All @@ -54,11 +53,11 @@ export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (
return;
}

const userAndAccount = tokenResponse.value;
const user = tokenResponse.value;

await userService.verifyUserEmail(userAndAccount.user_id);
await userService.verifyUserEmail(user.id);

const { account_id, email } = userAndAccount;
const { account_id, email } = user;

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

Expand All @@ -72,13 +71,13 @@ export const validateEmailAndLogin = asyncWrapper<ValidateEmailAndLogin>(async (
}
}

req.login(userAndAccount, function (err) {
req.login(user, function (err) {
if (err) {
logger.error('Error logging in user');
res.status(500).send({ error: { code: 'error_logging_in', message: 'There was a problem logging in the user. Please reach out to support.' } });
return;
}

res.status(200).send({ user: userToAPI(userAndAccount) });
res.status(200).send({ user: userToAPI(user) });
});
});
11 changes: 3 additions & 8 deletions packages/shared/lib/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as uuid from 'uuid';
import type { Result } from '@nangohq/utils';
import { Ok, Err } from '@nangohq/utils';
import type { User } from '../models/Admin.js';
import type { DBTeam, DBUser } from '@nangohq/types';
import type { DBUser } from '@nangohq/types';

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

Expand All @@ -20,13 +20,8 @@ class UserService {
return result || null;
}

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

if (result) {
const expired = new Date(result.email_verification_token_expires_at).getTime() < new Date().getTime();
Expand Down

0 comments on commit 7181c1c

Please sign in to comment.