Skip to content

Commit d733167

Browse files
authored
feat: update date-fns, use @date-fns/tz (#354)
1 parent d99ea63 commit d733167

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

package-lock.json

Lines changed: 24 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363
"@trpc/server": "10.45.0",
6464
"@types/lodash": "^4.14.202",
6565
"@types/validator": "^13.11.7",
66-
"date-fns": "^3.6.0",
67-
"date-fns-tz": "^3.1.3",
6866
"iron-session": "^8.0.1",
6967
"lodash": "^4.17.21",
7068
"nanoid": "^5.0.6",
@@ -89,6 +87,7 @@
8987
"@babel/preset-env": "^7.23.8",
9088
"@babel/preset-typescript": "^7.23.3",
9189
"@chakra-ui/cli": "^2.4.1",
90+
"@date-fns/tz": "^1.2.0",
9291
"@electric-sql/pglite": "^0.2.12",
9392
"@ianvs/prettier-plugin-sort-imports": "^4.4.0",
9493
"@playwright/test": "^1.40.1",
@@ -108,6 +107,7 @@
108107
"@typescript-eslint/parser": "^8.14.0",
109108
"@vitest/coverage-istanbul": "^2.1.1",
110109
"clean-webpack-plugin": "^4.0.0",
110+
"date-fns": "^4.1.0",
111111
"dotenv-cli": "^7.3.0",
112112
"eslint": "^8.57.1",
113113
"eslint-config-next": "15.0.3",

src/server/modules/auth/email/email.router.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { TZDate } from '@date-fns/tz'
12
import { TRPCError } from '@trpc/server'
2-
import { formatInTimeZone } from 'date-fns-tz'
3+
import { format } from 'date-fns/format'
34

45
import { getBaseUrl } from '~/utils/getBaseUrl'
56
import { normaliseEmail } from '~/utils/zod'
@@ -23,7 +24,10 @@ export const emailSessionRouter = router({
2324
.mutation(async ({ ctx, input: { email } }) => {
2425
// TODO: instead of storing expires, store issuedAt to calculate when the next otp can be re-issued
2526
// TODO: rate limit this endpoint also
26-
const expires = new Date(Date.now() + env.OTP_EXPIRY * 1000)
27+
const expires = new TZDate(
28+
Date.now() + env.OTP_EXPIRY * 1000,
29+
'Asia/Singapore',
30+
)
2731
const token = createVfnToken()
2832
const otpPrefix = createVfnPrefix()
2933
const hashedToken = createTokenHash(token, email)
@@ -50,10 +54,9 @@ export const emailSessionRouter = router({
5054
}),
5155
sendMail({
5256
subject: `Sign in to ${url.host}`,
53-
body: `Your OTP is ${otpPrefix}-<b>${token}</b>. It will expire on ${formatInTimeZone(
57+
body: `Your OTP is ${otpPrefix}-<b>${token}</b>. It will expire on ${format(
5458
expires,
55-
'Asia/Singapore',
56-
'dd MMM yyyy, hh:mmaaa',
59+
'dd MMM yyyy, h:mmaaa',
5760
)}.
5861
Please use this to login to your account.
5962
<p>If your OTP does not work, please request for a new one.</p>`,

0 commit comments

Comments
 (0)