Skip to content

Commit

Permalink
fix: add better github auth checks to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhenry committed Mar 1, 2024
1 parent 6bc0353 commit 22230e0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/server/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { TRPCError } from '@trpc/server'
import { personalOctokit } from 'bot/octokit'
import { logger } from 'utils/logger'

const middlewareLogger = logger.getSubLogger({ name: 'middleware' })

export const checkGitHubAuth = async (accessToken: string | undefined) => {
if (!accessToken) {
middlewareLogger.error('No access token provided')
throw new TRPCError({ code: 'UNAUTHORIZED' })
}

// Check validity of token
const octokit = personalOctokit(accessToken)
const user = await octokit.rest.users.getAuthenticated()

if (!user) {
try {
const user = await octokit.rest.users.getAuthenticated()
if (!user) {
middlewareLogger.error('No user found')
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
} catch (error) {
middlewareLogger.error('Error checking github auth', error)
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
}

0 comments on commit 22230e0

Please sign in to comment.