1
1
import * as cookie from 'cookie' ;
2
2
import flru from 'flru' ;
3
3
import { client } from './client.js' ;
4
+ import { error } from '@sveltejs/kit' ;
4
5
5
6
/** @typedef {import('./types').User } User */
6
7
@@ -13,27 +14,31 @@ const session_cache = flru(1000);
13
14
* @param {import('./types').GitHubUser } user
14
15
*/
15
16
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' , {
17
22
user_github_id : user . github_id ,
18
23
user_github_name : user . github_name ,
19
24
user_github_login : user . github_login ,
20
25
user_github_avatar_url : user . github_avatar_url
21
26
} ) ;
22
27
23
- if ( error ) {
24
- throw new Error ( error . message ) ;
28
+ if ( result . error ) {
29
+ throw new Error ( result . error . message ) ;
25
30
}
26
31
27
- session_cache . set ( data . sessionid , {
28
- id : data . userid ,
32
+ session_cache . set ( result . data . sessionid , {
33
+ id : result . data . userid ,
29
34
github_name : user . github_name ,
30
35
github_login : user . github_login ,
31
36
github_avatar_url : user . github_avatar_url
32
37
} ) ;
33
38
34
39
return {
35
- sessionid : data . sessionid ,
36
- expires : new Date ( data . expires )
40
+ sessionid : result . data . sessionid ,
41
+ expires : new Date ( result . data . expires )
37
42
} ;
38
43
}
39
44
@@ -42,7 +47,7 @@ export async function create(user) {
42
47
* @returns {Promise<User | null> }
43
48
*/
44
49
export async function read ( sessionid ) {
45
- if ( ! sessionid ) return null ;
50
+ if ( ! sessionid || ! client ) return null ;
46
51
47
52
if ( ! session_cache . get ( sessionid ) ) {
48
53
session_cache . set (
@@ -63,10 +68,14 @@ export async function read(sessionid) {
63
68
64
69
/** @param {string } sessionid */
65
70
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 } ) ;
67
76
68
- if ( error ) {
69
- throw new Error ( error . message ) ;
77
+ if ( result . error ) {
78
+ throw new Error ( result . error . message ) ;
70
79
}
71
80
72
81
session_cache . set ( sessionid , null ) ;
0 commit comments