wip: add db classes; can't load SQLite driver.
This commit is contained in:
parent
36f7b82806
commit
fdae502ec7
14 changed files with 3443 additions and 33 deletions
61
apps/vdb-backend/src/auth.ts
Normal file
61
apps/vdb-backend/src/auth.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
|
||||
import { promises as fs } from "fs";
|
||||
import * as path from "path";
|
||||
import { authenticate } from "@google-cloud/local-auth";
|
||||
import { google } from "googleapis";
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
|
||||
|
||||
//setup google auth
|
||||
const SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"];
|
||||
const TOKEN_PATH = path.join(process.cwd(), "res/token.secret.json");
|
||||
const CREDENTIALS_PATH = path.join(process.cwd(), "res/credentials.secret.json");
|
||||
|
||||
|
||||
/**
|
||||
* Load or request or authorization to call APIs.
|
||||
*/
|
||||
|
||||
export async function authorize() {
|
||||
let client: any = await loadSavedCredentialsIfExist();
|
||||
if (client) {
|
||||
return client;
|
||||
}
|
||||
client = await authenticate({
|
||||
scopes: SCOPES,
|
||||
keyfilePath: CREDENTIALS_PATH,
|
||||
});
|
||||
if (client.credentials) {
|
||||
await saveCredentials(client);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
async function loadSavedCredentialsIfExist() {
|
||||
try {
|
||||
const content = await fs.readFile(TOKEN_PATH);
|
||||
const credentials = JSON.parse(content.toString());
|
||||
return google.auth.fromJSON(credentials);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes credentials to a file compatible with GoogleAuth.fromJSON.
|
||||
*
|
||||
* @param {OAuth2Client} client
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async function saveCredentials(client: OAuth2Client) {
|
||||
const content = await fs.readFile(CREDENTIALS_PATH);
|
||||
const keys = JSON.parse(content.toString());
|
||||
const key = keys.installed || keys.web;
|
||||
const payload = JSON.stringify({
|
||||
type: "authorized_user",
|
||||
client_id: key.client_id,
|
||||
client_secret: key.client_secret,
|
||||
refresh_token: client.credentials.refresh_token,
|
||||
});
|
||||
await fs.writeFile(TOKEN_PATH, payload);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue