Skip to content

Commit

Permalink
fix type error
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Dec 12, 2024
1 parent 64fd022 commit d8c350f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/openauth/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { SubjectSchema } from "./session.js"
import type { v1 } from "@standard-schema/spec"
import {
InvalidAccessTokenError,
InvalidAuthorizationCodeError,
InvalidRefreshTokenError,
InvalidSessionError,
Expand Down Expand Up @@ -123,8 +124,11 @@ export function createClient(input: {
) {
if (!opts?.access) {
const decoded = decodeJwt(refresh)
if (!decoded) {
throw new InvalidAccessTokenError()
}
// allow 30s window for expiration
if (decoded.exp < Date.now() / 1000 + 30) {
if ((decoded.exp || 0) < Date.now() / 1000 + 30) {
return
}
}
Expand Down Expand Up @@ -191,7 +195,7 @@ export function createClient(input: {
} catch (e) {
if (e instanceof errors.JWTExpired && options?.refresh) {
const tokens = await this.refresh(options.refresh)

if (!tokens) throw new InvalidRefreshTokenError()
const verified = await result.verify(subjects, tokens.access, {
refresh: tokens.refresh,
issuer,
Expand Down
6 changes: 6 additions & 0 deletions packages/openauth/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export class InvalidRefreshTokenError extends Error {
}
}

export class InvalidAccessTokenError extends Error {
constructor() {
super("Invalid access token")
}
}

export class InvalidAuthorizationCodeError extends Error {
constructor() {
super("Invalid authorization code")
Expand Down

0 comments on commit d8c350f

Please sign in to comment.