Skip to content

Commit dda7f1d

Browse files
few bugs fixed 🐞
1 parent 4286cda commit dda7f1d

File tree

6 files changed

+84
-54
lines changed

6 files changed

+84
-54
lines changed

src/app.d.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// See https://svelte.dev/docs/kit/types#app.d.ts
2-
// for information about these interfaces
31
declare global {
42
namespace App {
5-
// interface Error {}
6-
// interface Locals {}
7-
// interface PageData {}
8-
// interface PageState {}
9-
// interface Platform {}
3+
interface Locals {
4+
auth(): Promise<import('@auth/core').Session | null>;
5+
}
6+
interface PageData {
7+
session: import('@auth/core').Session | null;
8+
}
109
}
11-
}
12-
13-
export {};
10+
}
11+
12+
export {};

src/auth/auth.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
import { SvelteKitAuth } from "@auth/sveltekit"
2-
import GitHub from "@auth/sveltekit/providers/github"
3-
import { GITHUB_ID,GITHUB_SECRET,AUTH_SECRET } from "$env/static/private"
1+
import { SvelteKitAuth } from "@auth/sveltekit";
2+
import GitHub from "@auth/core/providers/github";
3+
import { GITHUB_ID, GITHUB_SECRET, AUTH_SECRET } from "$env/static/private";
44

5-
6-
7-
export const { handle, signIn, signOut } = SvelteKitAuth({
5+
export const { handle, signIn, signOut } = SvelteKitAuth({
86
providers: [
97
GitHub({
10-
clientId:GITHUB_ID,
11-
clientSecret:GITHUB_SECRET
12-
})],
13-
14-
secret: AUTH_SECRET,
15-
trustHost: true,
16-
17-
callbacks: {
18-
async jwt({ token, account }) {
19-
20-
if (account) {
21-
token.accessToken = account.access_token
22-
}
23-
return token
24-
},
25-
async session({ session, token }) {
26-
return {
27-
...session,
28-
access_token: token.accessToken
8+
clientId: GITHUB_ID,
9+
clientSecret: GITHUB_SECRET,
10+
authorization: {
11+
params: {
12+
scope: "read:user user:email user:follow"
2913
}
30-
} }
31-
})
14+
}
15+
})
16+
],
17+
secret: AUTH_SECRET,
18+
trustHost: true,
19+
callbacks: {
20+
async jwt({ token, account }) {
21+
if (account) {
22+
token.accessToken = account.access_token;
23+
}
24+
return token;
25+
},
26+
async session({ session, token }: any) {
27+
if (token) {
28+
session.access_token = token.accessToken;
29+
}
30+
return session;
31+
}
32+
}
33+
});

src/hooks.server.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
// hooks.server.ts
21
import { sequence } from '@sveltejs/kit/hooks';
3-
import { handle as authHandle } from '../src/auth/auth';
2+
import { handle as authHandle } from './auth/auth';
3+
import type { Handle } from '@sveltejs/kit';
44

5-
// If you need to add custom middleware
6-
//@ts-ignore
7-
const customHandle = async ({ event, resolve }) => {
8-
// Add any custom middleware logic here before auth
5+
const customHandle: Handle = async ({ event, resolve }) => {
96
return resolve(event);
107
};
118

12-
// Export the sequence of handlers
13-
export const handle = sequence(customHandle, authHandle);
9+
export const handle = sequence(customHandle, authHandle);
File renamed without changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// src/routes/auth/[...auth]/+server.ts
2+
import { SvelteKitAuth } from "@auth/sveltekit";
3+
import GitHub from "@auth/core/providers/github";
4+
import { GITHUB_ID, GITHUB_SECRET, AUTH_SECRET } from "$env/static/private";
5+
6+
const handler = SvelteKitAuth({
7+
providers: [
8+
GitHub({
9+
clientId: GITHUB_ID,
10+
clientSecret: GITHUB_SECRET,
11+
authorization: {
12+
params: {
13+
scope: "read:user user:email user:follow"
14+
}
15+
}
16+
})
17+
],
18+
secret: AUTH_SECRET,
19+
trustHost: true,
20+
callbacks: {
21+
async jwt({ token, account }) {
22+
if (account) {
23+
token.accessToken = account.access_token;
24+
}
25+
return token;
26+
},
27+
async session({ session, token }: any) {
28+
if (token) {
29+
session.access_token = token.accessToken;
30+
}
31+
return session;
32+
}
33+
}
34+
});
35+
36+
export const GET = handler.handle;
37+
export const POST = handler.handle;

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { redirect } from '@sveltejs/kit';
22
import type { PageServerLoad } from './$types';
33

4-
export const prerender = 'auto';
5-
6-
export const load: PageServerLoad = async (event) => {
7-
const session = await event.locals.auth();
4+
export const load: PageServerLoad = async ({ locals }) => {
5+
const session = await locals.auth();
86

97
if (!session?.user) {
108
throw redirect(303, '/login');
@@ -15,8 +13,7 @@ export const load: PageServerLoad = async (event) => {
1513
const res = await fetch(`https://api.github.com/user/followers`, {
1614
headers: {
1715
Accept: 'application/vnd.github+json',
18-
//@ts-ignore
19-
Authorization: `Bearer ${session?.access_token}`,
16+
Authorization: `Bearer ${session.access_token}`,
2017
'X-Github-Api-Version': '2022-11-28'
2118
}
2219
});
@@ -34,8 +31,7 @@ export const load: PageServerLoad = async (event) => {
3431
const res = await fetch(`https://api.github.com/user`, {
3532
headers: {
3633
Accept: 'application/vnd.github+json',
37-
//@ts-ignore
38-
Authorization: `Bearer ${session?.access_token}`,
34+
Authorization: `Bearer ${session.access_token}`,
3935
'X-Github-Api-Version': '2022-11-28'
4036
}
4137
});
@@ -53,4 +49,4 @@ export const load: PageServerLoad = async (event) => {
5349
session,
5450
followers: await fetchFollowers()
5551
};
56-
};
52+
};

0 commit comments

Comments
 (0)