Initial, working login and logout with spotify
This commit is contained in:
43
src/lib/server/auth/spotify.ts
Normal file
43
src/lib/server/auth/spotify.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { PUBLIC_CLIENT_ID, PUBLIC_REDIRECT_URI } from "$env/static/public";
|
||||
|
||||
export const generateRandomString = (length: number) => {
|
||||
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const values = crypto.getRandomValues(new Uint8Array(length));
|
||||
return values.reduce((acc, x) => acc + possible[x % possible.length], "");
|
||||
}
|
||||
|
||||
export const sha256 = async (plain: string) => {
|
||||
const encoder = new TextEncoder()
|
||||
const data = encoder.encode(plain)
|
||||
return crypto.subtle.digest('SHA-256', data)
|
||||
}
|
||||
|
||||
export const base64encode = (input: ArrayBuffer) => {
|
||||
return btoa(String.fromCharCode(...new Uint8Array(input)))
|
||||
.replace(/=/g, '')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_');
|
||||
}
|
||||
|
||||
export const getToken = async (code: string, codeVerifier: string) => {
|
||||
const url = "https://accounts.spotify.com/api/token";
|
||||
const payload = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
client_id: PUBLIC_CLIENT_ID,
|
||||
grant_type: 'authorization_code',
|
||||
code,
|
||||
redirect_uri: PUBLIC_REDIRECT_URI,
|
||||
code_verifier: codeVerifier
|
||||
})
|
||||
};
|
||||
|
||||
const body = await fetch(url, payload);
|
||||
const response = await body.json();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
10
src/lib/server/db/index.ts
Normal file
10
src/lib/server/db/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||
import Database from 'better-sqlite3';
|
||||
import * as schema from './schema';
|
||||
import { env } from '$env/dynamic/private';
|
||||
|
||||
if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');
|
||||
|
||||
const client = new Database(env.DATABASE_URL);
|
||||
|
||||
export const db = drizzle(client, { schema });
|
||||
55
src/lib/server/db/schema.ts
Normal file
55
src/lib/server/db/schema.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { relations } from 'drizzle-orm';
|
||||
import { sqliteTable, integer, text, type AnySQLiteColumn, primaryKey } from 'drizzle-orm/sqlite-core';
|
||||
|
||||
export const usersTable = sqliteTable('users', {
|
||||
email: text('email').primaryKey(),
|
||||
username: text('username'),
|
||||
});
|
||||
|
||||
export const sessionsTable = sqliteTable('sessions', {
|
||||
id: text('id').primaryKey(),
|
||||
accessToken: text('access_token'),
|
||||
refreshToken: text('refresh_token'),
|
||||
userEmail: text('user_email').references((): AnySQLiteColumn => usersTable.email)
|
||||
});
|
||||
|
||||
export const sessionsRelations = relations(sessionsTable, ({ one }) => ({
|
||||
user: one(usersTable, { fields: [sessionsTable.userEmail], references: [usersTable.email] })
|
||||
}))
|
||||
|
||||
export const userRelations = relations(usersTable, ({ one, many }) => ({
|
||||
session: one(sessionsTable),
|
||||
usersInLobby: many(usersInLobby)
|
||||
}));
|
||||
|
||||
export const lobbysTable = sqliteTable('lobbys', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
hostEmail: text('host_email').references((): AnySQLiteColumn => usersTable.email),
|
||||
});
|
||||
|
||||
export const lobbysRelations = relations(lobbysTable, ({ many }) => ({
|
||||
usersInLobby: many(usersInLobby)
|
||||
}));
|
||||
|
||||
export const usersInLobby = sqliteTable('user_in_lobby', {
|
||||
userEmail: text('user_email').notNull().references((): AnySQLiteColumn => usersTable.email),
|
||||
lobbyId: integer('lobby_id').notNull().references((): AnySQLiteColumn => lobbysTable.id)
|
||||
}, (t) => [
|
||||
primaryKey({ columns: [t.userEmail, t.lobbyId] })
|
||||
])
|
||||
|
||||
export const usersToLobbysRelations = relations(usersInLobby, ({ one }) => ({
|
||||
lobby: one(lobbysTable, {
|
||||
fields: [usersInLobby.lobbyId],
|
||||
references: [lobbysTable.id]
|
||||
}),
|
||||
user: one(usersTable, {
|
||||
fields: [usersInLobby.userEmail],
|
||||
references: [usersTable.email]
|
||||
})
|
||||
}));
|
||||
|
||||
export const states = sqliteTable('auth_states', {
|
||||
id: text('id').primaryKey(),
|
||||
codeVerifier: text('code_verifier').notNull()
|
||||
})
|
||||
11
src/lib/server/spotify/base.ts
Normal file
11
src/lib/server/spotify/base.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const getJson = async (accessToken: string, subUri: string) => {
|
||||
const baseUrl = new URL("https://api.spotify.com/");
|
||||
const requestUrl = new URL(subUri, baseUrl);
|
||||
|
||||
return await fetch(requestUrl, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
"Authorization": `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
5
src/lib/server/spotify/users.ts
Normal file
5
src/lib/server/spotify/users.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { getJson } from "./base"
|
||||
|
||||
export const getCurrentUserProfile = async (accessToken: string) => {
|
||||
return await (await getJson(accessToken, "/v1/me")).json()
|
||||
}
|
||||
Reference in New Issue
Block a user