Skip to content

Commit 4286cda

Browse files
few bugs fixed 🐞
1 parent 0507f4c commit 4286cda

File tree

3 files changed

+71
-52
lines changed

3 files changed

+71
-52
lines changed

src/routes/profile/+page.server.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { redirect } from '@sveltejs/kit';
2+
import type { PageServerLoad } from './$types';
3+
4+
export const prerender = 'auto';
5+
6+
export const load: PageServerLoad = async (event) => {
7+
const session = await event.locals.auth();
8+
9+
if (!session?.user) {
10+
throw redirect(303, '/login');
11+
}
12+
13+
const fetchFollowers = async () => {
14+
try {
15+
const res = await fetch(`https://api.github.com/user/followers`, {
16+
headers: {
17+
Accept: 'application/vnd.github+json',
18+
//@ts-ignore
19+
Authorization: `Bearer ${session?.access_token}`,
20+
'X-Github-Api-Version': '2022-11-28'
21+
}
22+
});
23+
24+
if (!res.ok) throw new Error('Failed to fetch followers');
25+
return await res.json();
26+
} catch (error) {
27+
console.error('Error fetching followers:', error);
28+
return null;
29+
}
30+
};
31+
32+
const fetchUserInfo = async () => {
33+
try {
34+
const res = await fetch(`https://api.github.com/user`, {
35+
headers: {
36+
Accept: 'application/vnd.github+json',
37+
//@ts-ignore
38+
Authorization: `Bearer ${session?.access_token}`,
39+
'X-Github-Api-Version': '2022-11-28'
40+
}
41+
});
42+
43+
if (!res.ok) throw new Error('Failed to fetch user info');
44+
return await res.json();
45+
} catch (error) {
46+
console.error('Error fetching user info:', error);
47+
return null;
48+
}
49+
};
50+
51+
return {
52+
user: await fetchUserInfo(),
53+
session,
54+
followers: await fetchFollowers()
55+
};
56+
};

src/routes/profile/+page.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/routes/profile/+server.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import type { RequestHandler } from '@sveltejs/kit';
12

2-
export async function GET({ locals }) {
3-
//@ts-ignore
4-
const session = locals.session;
5-
6-
if (!session) {
7-
return new Response('Unauthorized', { status: 401 });
8-
}
9-
10-
return new Response(JSON.stringify(session), { status: 200 });
3+
export const GET: RequestHandler = async ({ locals }) => {
4+
//@ts-ignore
5+
if (!locals.session) {
6+
return new Response('Unauthorized', { status: 401 });
117
}
12-
8+
9+
// Assuming 'session' in locals has a proper type that includes user data
10+
//@ts-ignore
11+
return new Response(JSON.stringify(locals.session), {
12+
headers: {
13+
'Content-Type': 'application/json'
14+
},
15+
status: 200
16+
});
17+
};

0 commit comments

Comments
 (0)