|
|
|
@ -2,15 +2,17 @@ import * as cookie from 'cookie';
|
|
|
|
|
import flru from 'flru';
|
|
|
|
|
import { client } from './client.js';
|
|
|
|
|
|
|
|
|
|
const session_cache = flru(1000);
|
|
|
|
|
|
|
|
|
|
/** @typedef {import('./types').User} User */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {User} user
|
|
|
|
|
* @param {string} access_token
|
|
|
|
|
* @type {import('flru').flruCache<User>}
|
|
|
|
|
*/
|
|
|
|
|
export async function create(user, access_token) {
|
|
|
|
|
const session_cache = flru(1000);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {import('./types').GitHubUser} user
|
|
|
|
|
*/
|
|
|
|
|
export async function create(user) {
|
|
|
|
|
const { data, error } = await client.rpc('login', {
|
|
|
|
|
user_github_id: user.github_id,
|
|
|
|
|
user_github_name: user.github_name,
|
|
|
|
@ -45,22 +47,20 @@ export async function read(sessionid) {
|
|
|
|
|
if (!session_cache.get(sessionid)) {
|
|
|
|
|
session_cache.set(
|
|
|
|
|
sessionid,
|
|
|
|
|
client
|
|
|
|
|
await client
|
|
|
|
|
.rpc('get_user', { sessionid })
|
|
|
|
|
.then(({ data, error }) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
session_cache.set(sessionid, null);
|
|
|
|
|
throw new Error(error.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data.id && data;
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
session_cache.set(sessionid, null);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await session_cache.get(sessionid);
|
|
|
|
|
return session_cache.get(sessionid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {string} sessionid */
|
|
|
|
@ -74,7 +74,7 @@ export async function destroy(sessionid) {
|
|
|
|
|
session_cache.set(sessionid, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @type {string | null} str */
|
|
|
|
|
/** @param {string | null} str */
|
|
|
|
|
export function from_cookie(str) {
|
|
|
|
|
if (!str) return null;
|
|
|
|
|
return read(cookie.parse(str).sid);
|
|
|
|
|