Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions apps/svelte.dev/src/lib/db/client.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { dev } from '$app/environment';
import { SUPABASE_URL, SUPABASE_KEY } from '$env/static/private';
import { building } from '$app/environment';
import { env } from '$env/dynamic/private';
import { createClient } from '@supabase/supabase-js';

const client_enabled = !!(!dev || (SUPABASE_URL && SUPABASE_KEY));
const enabled = !building && env.SUPABASE_URL && env.SUPABASE_KEY;

if (!enabled) {
console.warn(
`Missing SUPABASE_URL and SUPABASE_KEY environment variables. Loading and saving in the playground is disabled`
);
}

/**
* @type {import('@supabase/supabase-js').SupabaseClient<any, "public", any>}
*/
// @ts-ignore-line
export const client =
client_enabled &&
createClient(SUPABASE_URL, SUPABASE_KEY, {
enabled &&
createClient(env.SUPABASE_URL, env.SUPABASE_KEY, {
global: { fetch },
auth: { persistSession: false }
});
31 changes: 20 additions & 11 deletions apps/svelte.dev/src/lib/db/session.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as cookie from 'cookie';
import flru from 'flru';
import { client } from './client.js';
import { error } from '@sveltejs/kit';

/** @typedef {import('./types').User} User */

Expand All @@ -13,27 +14,31 @@ const session_cache = flru(1000);
* @param {import('./types').GitHubUser} user
*/
export async function create(user) {
const { data, error } = await client.rpc('login', {
if (!client) {
error(500, 'Database client is not configured');
}

const result = await client.rpc('login', {
user_github_id: user.github_id,
user_github_name: user.github_name,
user_github_login: user.github_login,
user_github_avatar_url: user.github_avatar_url
});

if (error) {
throw new Error(error.message);
if (result.error) {
throw new Error(result.error.message);
}

session_cache.set(data.sessionid, {
id: data.userid,
session_cache.set(result.data.sessionid, {
id: result.data.userid,
github_name: user.github_name,
github_login: user.github_login,
github_avatar_url: user.github_avatar_url
});

return {
sessionid: data.sessionid,
expires: new Date(data.expires)
sessionid: result.data.sessionid,
expires: new Date(result.data.expires)
};
}

Expand All @@ -42,7 +47,7 @@ export async function create(user) {
* @returns {Promise<User | null>}
*/
export async function read(sessionid) {
if (!sessionid) return null;
if (!sessionid || !client) return null;

if (!session_cache.get(sessionid)) {
session_cache.set(
Expand All @@ -63,10 +68,14 @@ export async function read(sessionid) {

/** @param {string} sessionid */
export async function destroy(sessionid) {
const { error } = await client.rpc('logout', { sessionid });
if (!client) {
error(500, 'Database client is not configured');
}

const result = await client.rpc('logout', { sessionid });

if (error) {
throw new Error(error.message);
if (result.error) {
throw new Error(result.error.message);
}

session_cache.set(sessionid, null);
Expand Down
Loading