-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add better github auth checks to middleware
- Loading branch information
Showing
1 changed file
with
12 additions
and
3 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 |
---|---|---|
@@ -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' }) | ||
} | ||
} |