Skip to content

Commit 2cf2494

Browse files
committed
fix #533
1 parent 3e184b5 commit 2cf2494

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import { dev } from '$app/environment';
2-
import { SUPABASE_URL, SUPABASE_KEY } from '$env/static/private';
1+
import { building } from '$app/environment';
2+
import { env } from '$env/dynamic/private';
33
import { createClient } from '@supabase/supabase-js';
44

5-
const client_enabled = !!(!dev || (SUPABASE_URL && SUPABASE_KEY));
5+
const enabled = !building && env.SUPABASE_URL && env.SUPABASE_KEY;
6+
7+
if (!enabled) {
8+
console.warn(
9+
`Missing SUPABASE_URL and SUPABASE_KEY environment variables. Loading and saving in the playground is disabled`
10+
);
11+
}
612

713
/**
814
* @type {import('@supabase/supabase-js').SupabaseClient<any, "public", any>}
915
*/
1016
// @ts-ignore-line
1117
export const client =
12-
client_enabled &&
13-
createClient(SUPABASE_URL, SUPABASE_KEY, {
18+
enabled &&
19+
createClient(env.SUPABASE_URL, env.SUPABASE_KEY, {
1420
global: { fetch },
1521
auth: { persistSession: false }
1622
});

apps/svelte.dev/src/lib/db/session.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as cookie from 'cookie';
22
import flru from 'flru';
33
import { client } from './client.js';
4+
import { error } from '@sveltejs/kit';
45

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

@@ -13,27 +14,31 @@ const session_cache = flru(1000);
1314
* @param {import('./types').GitHubUser} user
1415
*/
1516
export async function create(user) {
16-
const { data, error } = await client.rpc('login', {
17+
if (!client) {
18+
error(500, 'Database client is not configured');
19+
}
20+
21+
const result = await client.rpc('login', {
1722
user_github_id: user.github_id,
1823
user_github_name: user.github_name,
1924
user_github_login: user.github_login,
2025
user_github_avatar_url: user.github_avatar_url
2126
});
2227

23-
if (error) {
24-
throw new Error(error.message);
28+
if (result.error) {
29+
throw new Error(result.error.message);
2530
}
2631

27-
session_cache.set(data.sessionid, {
28-
id: data.userid,
32+
session_cache.set(result.data.sessionid, {
33+
id: result.data.userid,
2934
github_name: user.github_name,
3035
github_login: user.github_login,
3136
github_avatar_url: user.github_avatar_url
3237
});
3338

3439
return {
35-
sessionid: data.sessionid,
36-
expires: new Date(data.expires)
40+
sessionid: result.data.sessionid,
41+
expires: new Date(result.data.expires)
3742
};
3843
}
3944

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

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

6469
/** @param {string} sessionid */
6570
export async function destroy(sessionid) {
66-
const { error } = await client.rpc('logout', { sessionid });
71+
if (!client) {
72+
error(500, 'Database client is not configured');
73+
}
74+
75+
const result = await client.rpc('logout', { sessionid });
6776

68-
if (error) {
69-
throw new Error(error.message);
77+
if (result.error) {
78+
throw new Error(result.error.message);
7079
}
7180

7281
session_cache.set(sessionid, null);

0 commit comments

Comments
 (0)