-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use header for getting access token in verify-token (#51)
- Loading branch information
Showing
3 changed files
with
69 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { MiddlewareHandler } from 'hono' | ||
import { HonoEnv } from 'load-context' | ||
|
||
const AUTHORIZATION_REGEX = /^Bearer (.+)$/ | ||
|
||
export const authMiddleware: MiddlewareHandler<HonoEnv> = async (c, next) => { | ||
const authorization = c.req.header('Authroization') | ||
const accessToken = authorization?.match(AUTHORIZATION_REGEX)?.[1] | ||
if (!accessToken) { | ||
return c.text('Unauthorized', 401) | ||
} | ||
|
||
const nowUnixMs = Date.now() | ||
const nowDate = new Date(nowUnixMs) | ||
|
||
const tokenInfo = await c.var.dbClient.query.token.findFirst({ | ||
where: (token, { eq, and, gt }) => | ||
and( | ||
eq(token.access_token, accessToken), | ||
gt(token.code_expires_at, nowDate), | ||
), | ||
with: { | ||
client: true, | ||
scopes: { | ||
with: { | ||
scope: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (!tokenInfo) { | ||
return c.text('Unauthorized', 401) | ||
} | ||
|
||
c.set('tokenInfo', tokenInfo) | ||
|
||
await next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters