logmein
Viewing readonly version: 281View latest version
Script
999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { extractProjectValInfo } from "https://esm.town/v/maxm/extractProjectValInfo";
import { sqlite } from "https://esm.town/v/stevekrouse/sqlite?v=13";
const projectInfo = extractProjectValInfo(import.meta.url);
export type Config = {
login_type: "email" | "password";
id: number;
};
export class DB {
#tableNamePrefix: string;
constructor(tableNamePrefix: string) {
this.#tableNamePrefix = tableNamePrefix;
}
async getConfig(): Promise<Config | undefined> {
const res = await sqlite
.execute(`SELECT * FROM ${this.#tableNamePrefix}_logmein_config LIMIT 1`);
if (res.rows.length === 0) {
return undefined;
}
return res.rows[0] as unknown as Config;
}
async login(email: string, config: Config | undefined): Promise<string> {
const resp = await fetch("https://api.val.town/v1/me", {
headers: {
"Authorization": `Bearer ${Deno.env.get("valtown")}`,
},
});
let respBody = await resp.json();
if (email !== respBody.email) {
throw new Error("Login email does not match the email on the account.");
}
const [, session] = await Promise.all([this.writeConfig("email"), this.newSession()]);