Skip to content

Commit 3e1926b

Browse files
committed
improve solution
1 parent 70140b7 commit 3e1926b

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

epicshop/epic-me/app/routes/introspect.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ export async function action({ request, context }: Route.LoaderArgs) {
2121

2222
const info = tokenData as Token
2323

24-
if (info.expiresAt < Date.now()) return { active: false }
24+
if (info.expiresAt * 1000 < Date.now()) {
25+
return { active: false }
26+
}
2527

2628
return {
2729
active: true,
2830
client_id: info.grant.clientId,
2931
scope: info.grant.scope.join(' '),
3032
sub: info.userId,
31-
exp: Math.floor(info.expiresAt / 1000),
32-
iat: Math.floor(info.createdAt / 1000),
33+
exp: info.expiresAt,
34+
iat: info.createdAt,
3335
}
3436
}
3537

exercises/99.finished/99.solution/src/auth.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import { EPIC_ME_AUTH_SERVER_URL } from './client.ts'
33

44
export type AuthInfo = NonNullable<Awaited<ReturnType<typeof getAuthInfo>>>
55

6+
const introspectResponseSchema = z.discriminatedUnion('active', [
7+
z.object({
8+
active: z.literal(true),
9+
client_id: z.string(),
10+
scope: z.string(),
11+
sub: z.string(),
12+
exp: z.number(),
13+
}),
14+
z.object({
15+
active: z.literal(false),
16+
}),
17+
])
18+
619
export async function getAuthInfo(request: Request) {
720
const token = request.headers.get('authorization')?.replace(/^Bearer\s+/i, '')
821
if (!token) return undefined
@@ -17,15 +30,9 @@ export async function getAuthInfo(request: Request) {
1730

1831
const rawData = await resp.json()
1932

20-
const data = z
21-
.object({
22-
active: z.boolean(),
23-
client_id: z.string(),
24-
scope: z.string(),
25-
sub: z.string(),
26-
exp: z.number(),
27-
})
28-
.parse(rawData)
33+
const data = introspectResponseSchema.parse(rawData)
34+
35+
if (!data.active) return undefined
2936

3037
const { sub, client_id, scope, exp } = data
3138

0 commit comments

Comments
 (0)