@@ -11,6 +11,7 @@ import { Pagination } from './_components/pagination';
1111import { Suspense } from 'react' ;
1212import type { RepoData , RepoItem , RepoResponse } from '@/types' ;
1313import type { Metadata } from 'next' ;
14+ import { auth } from '@/auth' ;
1415
1516interface ReposPageProps {
1617 params : { language : string } ;
@@ -72,6 +73,8 @@ async function getRepos(
7273 language : string ,
7374 searchParams : ReposPageProps [ 'searchParams' ]
7475) : Promise < RepoResponse > {
76+ const client = getXataClient ( ) ;
77+ const session = await auth ( ) ;
7578 const {
7679 p : page = '1' ,
7780 s : sort = '' ,
@@ -103,9 +106,22 @@ async function getRepos(
103106 const headers : HeadersInit = {
104107 Accept : 'application/vnd.github.mercy-preview+json'
105108 } ;
106-
107- if ( env . AUTH_GITHUB_TOKEN )
109+ const userId = session ?. user ?. id ;
110+
111+ if ( userId ) {
112+ const account = await client . db . nextauth_accounts
113+ . select ( [ 'access_token' ] )
114+ . filter ( { 'user.id' : userId } )
115+ . getFirst ( ) ;
116+
117+ if ( account && account . access_token ) {
118+ headers . Authorization = `Bearer ${ account . access_token } ` ;
119+ } else if ( env . AUTH_GITHUB_TOKEN ) {
120+ headers . Authorization = `Bearer ${ env . AUTH_GITHUB_TOKEN } ` ;
121+ }
122+ } else if ( env . AUTH_GITHUB_TOKEN ) {
108123 headers . Authorization = `Bearer ${ env . AUTH_GITHUB_TOKEN } ` ;
124+ }
109125
110126 const res = await fetch ( apiUrl , { headers } ) ;
111127 if ( ! res . ok ) notFound ( ) ;
0 commit comments