Skip to content

Commit 8921e46

Browse files
Use access token of the user when logged in
1 parent a05ba25 commit 8921e46

File tree

1 file changed

+18
-2
lines changed
  • src/app/(public)/repos/[language]

1 file changed

+18
-2
lines changed

src/app/(public)/repos/[language]/page.tsx

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Pagination } from './_components/pagination';
1111
import { Suspense } from 'react';
1212
import type { RepoData, RepoItem, RepoResponse } from '@/types';
1313
import type { Metadata } from 'next';
14+
import { auth } from '@/auth';
1415

1516
interface 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

Comments
 (0)