add basic db schema + auth + oauth with osu

This commit is contained in:
2026-02-28 01:00:22 +01:00
parent 844fd590e1
commit 0a6695fc26
4 changed files with 216 additions and 18 deletions

View File

@@ -4,17 +4,65 @@ import { sveltekitCookies } from 'better-auth/svelte-kit';
import { env } from '$env/dynamic/private';
import { getRequestEvent } from '$app/server';
import { db } from '$lib/server/db';
import { genericOAuth } from 'better-auth/plugins';
export const auth = betterAuth({
baseURL: env.ORIGIN,
secret: env.BETTER_AUTH_SECRET,
database: drizzleAdapter(db, { provider: 'pg' }),
emailAndPassword: { enabled: true },
socialProviders: {
github: {
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET
}
},
plugins: [sveltekitCookies(getRequestEvent)] // make sure this is the last plugin in the array
plugins: [
genericOAuth({
config: [
{
providerId: 'osu',
clientId: env.OSU_CLIENT_ID!,
clientSecret: env.OSU_CLIENT_SECRET!,
authorizationUrl: 'https://osu.ppy.sh/oauth/authorize',
tokenUrl: 'https://osu.ppy.sh/oauth/token',
userInfoUrl: 'https://osu.ppy.sh/api/v2/me',
scopes: ['identify', 'public'],
pkce: false,
authentication: 'post',
redirectURI: 'http://localhost:5173/api/auth/oauth2/callback/osu',
getToken: async ({ code, redirectURI }) => {
const res = await fetch('https://osu.ppy.sh/oauth/token', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
client_id: env.OSU_CLIENT_ID!,
client_secret: env.OSU_CLIENT_SECRET!,
code,
grant_type: 'authorization_code',
redirect_uri: redirectURI
})
});
const data = await res.json();
if (!res.ok) {
console.error('osu! token exchange failed:', data);
throw new Error('Token exchange failed');
}
return {
accessToken: data.access_token,
refreshToken: data.refresh_token,
accessTokenExpiresAt: new Date(Date.now() + data.expires_in * 1000),
scopes: ['identify', 'public'],
raw: data
};
},
mapProfileToUser(profile) {
return {
name: profile.username,
image: profile.avatar_url,
email: `${profile.id}@osu.local`
};
}
}
]
}),
sveltekitCookies(getRequestEvent) // make sure this is the last plugin in the array
]
});