Skip to content

fix(core): return one-time token status as expired when expiration time is passed #7257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

charIeszhao
Copy link
Member

@charIeszhao charIeszhao commented Apr 9, 2025

Summary

The GET one-time token APIs should automatically calculate the expiration time of the tokens, and modify the status in return data to expired when necessary.

Previously, the GET APIs return the DB entries directly. However, since we only update the token status on verifying tokens, those unverified ones don't have the opportunity to have their status updated even if the expiration time has passed. Therefore, the token status might be out-of-date in such situations.

Testing

Integration tests added

Checklist

  • .changeset
  • unit tests
  • integration tests
  • necessary TSDoc comments

@charIeszhao charIeszhao requested a review from a team April 9, 2025 06:04
@charIeszhao charIeszhao self-assigned this Apr 9, 2025
@charIeszhao charIeszhao changed the title fix(core): return one-time token status as expired when expiration ti… fix(core): return one-time token status as expired when expiration time is passed Apr 9, 2025
Copy link

github-actions bot commented Apr 9, 2025

COMPARE TO master

Total Size Diff 📈 +1.84 KB

Diff by File
Name Diff
packages/core/src/libraries/one-time-token.ts 📈 +135 Bytes
packages/core/src/queries/one-time-tokens.ts 📈 +976 Bytes
packages/core/src/routes/one-time-tokens.ts 📈 +12 Bytes
packages/integration-tests/src/tests/api/one-time-tokens.test.ts 📈 +1.01 KB

@charIeszhao charIeszhao force-pushed the charles-log-11241-fetched-token-status-does-not-updated-to-expired branch 2 times, most recently from db069f1 to 03776ba Compare April 9, 2025 08:22
@charIeszhao charIeszhao force-pushed the charles-log-11241-fetched-token-status-does-not-updated-to-expired branch from 03776ba to 0d47ce5 Compare April 9, 2025 08:39
@charIeszhao charIeszhao merged commit a922517 into master Apr 9, 2025
33 of 34 checks passed
@charIeszhao charIeszhao deleted the charles-log-11241-fetched-token-status-does-not-updated-to-expired branch April 9, 2025 09:20
Comment on lines +88 to +120
const buildFindEntitiesSql = (
where: Partial<Pick<OneTimeToken, 'id' | 'token' | 'email' | 'status'>>,
pagination?: { limit: number; offset: number }
) => {
const whereEntries = Object.entries(where).filter(([_, value]) => Boolean(value));
return sql`
select
${sql.join(
Object.values(fields).filter((field) => field !== fields.status),
sql`, `
)},
case
when
${fields.status} = ${OneTimeTokenStatus.Active} and
${fields.expiresAt} < now()
then ${OneTimeTokenStatus.Expired}
else ${fields.status}
end as "status"
from ${table}
${conditionalSql(
whereEntries.length > 0 && whereEntries,
(whereEntries) =>
sql`where ${sql.join(
whereEntries.map(([column, value]) =>
conditionalSql(value, (value) => sql`${sql.identifier([column])} = ${value}`)
),
sql` and `
)}`
)}
order by ${fields.createdAt} desc
${conditionalSql(pagination, ({ limit, offset }) => sql`limit ${limit} offset ${offset}`)}
`;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also update the status? o/w the query response will be different than the DB data.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid there will be a performance issue, since this logic is also used when fetching list, and there're chances that it ends up updating all the entities in the entire table. We can have an offline discussion if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants