11import * as cookie from 'cookie' ;
22import flru from 'flru' ;
33import { 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 */
1516export 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 */
4449export 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 */
6570export 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