Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { defineMiddleware } from 'astro/middleware'
import { verifyToken, getCookieName, type SessionUser } from './lib/auth'
import { env } from 'cloudflare:workers'

export const onRequest = defineMiddleware(async (context, next) => {
const cookieName = getCookieName()
const token = context.cookies.get(cookieName)?.value
const env = context.locals.runtime?.env

if (token && env) {
const user = await verifyToken(token, env)
if (token) {
const user = await verifyToken(token, env as any)
if (user) {
context.locals.user = user
}
Expand Down
12 changes: 6 additions & 6 deletions src/pages/api/auth/callback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const prerender = false

import type { APIRoute } from 'astro'
import { env } from 'cloudflare:workers'
import {
createToken,
createTokensCookie,
Expand All @@ -12,8 +13,7 @@ import {
type GitHubTokens,
} from '../../../lib/auth'

export const GET: APIRoute = async ({ url, redirect, cookies, locals }) => {
const env = locals.runtime.env
export const GET: APIRoute = async ({ url, redirect, cookies }) => {
const clientId = env.GITHUB_CLIENT_ID
const clientSecret = env.GITHUB_CLIENT_SECRET
const callbackUrl = env.GITHUB_CALLBACK_URL
Expand Down Expand Up @@ -68,8 +68,8 @@ export const GET: APIRoute = async ({ url, redirect, cookies, locals }) => {
name: githubUser.name,
}

const userToken = await createToken(sessionUser, env)
cookies.set(getCookieName(), userToken, getCookieOptions(env))
const userToken = await createToken(sessionUser, env as any)
cookies.set(getCookieName(), userToken, getCookieOptions(env as any))

if (refreshToken) {
const tokens: GitHubTokens = {
Expand All @@ -78,8 +78,8 @@ export const GET: APIRoute = async ({ url, redirect, cookies, locals }) => {
expires_at: Date.now() + (tokenData.expires_in || 3600) * 1000,
scope: tokenData.scope || '',
}
const tokensCookie = await createTokensCookie(tokens, env)
cookies.set(getTokensCookieName(), tokensCookie, getTokensCookieOptions(env))
const tokensCookie = await createTokensCookie(tokens, env as any)
cookies.set(getTokensCookieName(), tokensCookie, getTokensCookieOptions(env as any))
}

return redirect('/')
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const prerender = false

import type { APIRoute } from 'astro'
import { env } from 'cloudflare:workers'

export const GET: APIRoute = async ({ redirect, locals }) => {
const env = locals.runtime.env
export const GET: APIRoute = async ({ redirect }) => {
const clientId = env.GITHUB_CLIENT_ID
const clientSecret = env.GITHUB_CLIENT_SECRET
const callbackUrl = env.GITHUB_CALLBACK_URL
Expand Down
14 changes: 7 additions & 7 deletions src/pages/api/auth/me.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const prerender = false

import type { APIRoute } from 'astro'
import { env } from 'cloudflare:workers'
import {
verifyToken,
verifyTokensCookie,
Expand All @@ -11,8 +12,7 @@ import {
getTokensCookieOptions,
} from '../../../lib/auth'

export const GET: APIRoute = async ({ cookies, locals }) => {
const env = locals.runtime.env
export const GET: APIRoute = async ({ cookies }) => {
const sessionToken = cookies.get(getCookieName())?.value
const tokensToken = cookies.get(getTokensCookieName())?.value

Expand All @@ -23,26 +23,26 @@ export const GET: APIRoute = async ({ cookies, locals }) => {
})
}

const user = await verifyToken(sessionToken, env)
const user = await verifyToken(sessionToken, env as any)
if (!user) {
return new Response(JSON.stringify({ user: null, accessToken: null }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}

let tokens = await verifyTokensCookie(tokensToken, env)
let tokens = await verifyTokensCookie(tokensToken, env as any)

if (tokens) {
const isExpired = Date.now() >= tokens.expires_at
const shouldRefresh = tokens.refresh_token && isExpired

if (shouldRefresh) {
const newTokens = await refreshAccessToken(tokens.refresh_token, env)
const newTokens = await refreshAccessToken(tokens.refresh_token, env as any)
if (newTokens) {
tokens = newTokens
const newTokensCookie = await createTokensCookie(newTokens, env)
cookies.set(getTokensCookieName(), newTokensCookie, getTokensCookieOptions(env))
const newTokensCookie = await createTokensCookie(newTokens, env as any)
cookies.set(getTokensCookieName(), newTokensCookie, getTokensCookieOptions(env as any))
}
}
}
Expand Down
Loading