Skip to content

Commit

Permalink
move getting account to requireGitHubAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
icedevera committed May 6, 2024
1 parent f1914bc commit d3545f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
26 changes: 2 additions & 24 deletions src/server/actions/github/getGithub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@ import {Octokit} from 'octokit'
import {z} from 'zod'

import {requireGitHubAuth} from '@/server/utils/auth'
import {db} from '@/server/db'
import {ServerError} from '@/lib/types/error'
import {serializeServerErrors} from '@/lib/utils/common/error'

export type GetPublicReposResponse = Awaited<
ReturnType<typeof getPublicReposAction>
>

const getPublicReposAction = async () => {
const session = await requireGitHubAuth()

const account = await db.query.accounts.findFirst({
columns: {access_token: true},
where: (account, {eq, and}) =>
and(eq(account.userId, session.user.id), eq(account.provider, 'github')),
})

if (!account) {
throw new ServerError('GitHub account not found')
}
const {account} = await requireGitHubAuth()

const octokit = new Octokit({
auth: account.access_token,
Expand Down Expand Up @@ -58,20 +46,10 @@ const getRepoFilesParamsSchema = z.object({
export type GetRepoFilesParams = z.infer<typeof getRepoFilesParamsSchema>

const getRepoFilesAction = async (request: GetRepoFilesParams) => {
const session = await requireGitHubAuth()
const {account} = await requireGitHubAuth()

const {repo, owner, defaultBranch} = getRepoFilesParamsSchema.parse(request)

const account = await db.query.accounts.findFirst({
columns: {access_token: true},
where: (account, {eq, and}) =>
and(eq(account.userId, session.user.id), eq(account.provider, 'github')),
})

if (!account) {
throw new ServerError('GitHub account not found')
}

const octokit = new Octokit({
auth: account.access_token,
})
Expand Down
13 changes: 12 additions & 1 deletion src/server/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Session, getServerSession} from 'next-auth'

import {authOptions} from '../authOptions'
import {UserRole} from '../db/models'
import {db} from '../db'

import {ServerError} from '@/lib/types/error'

Expand Down Expand Up @@ -55,5 +56,15 @@ export const requireGitHubAuth = async () => {
)
}

return session
const account = await db.query.accounts.findFirst({
columns: {access_token: true},
where: (account, {eq, and}) =>
and(eq(account.userId, session.user.id), eq(account.provider, 'github')),
})

if (!account) {
throw new ServerError('GitHub account not found.')
}

return {session, account}
}

0 comments on commit d3545f7

Please sign in to comment.