adding access endpoint

This commit is contained in:
balzack 2024-07-12 22:30:36 -07:00
parent 7e3433e65a
commit f0fc6c183c
3 changed files with 22 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import { BotModule } from './bot';
import { ConsoleLogging } from './logging';
import { type Store, OfflineStore, OnlineStore, NoStore } from './store';
import { setLogin } from './net/setLogin';
import { setAccess } from './net/setAccess';
import { addAccount } from './net/addAccount';
import type { Session, Node, Bot, SqlStore, WebStore, Crypto, Logging } from './api';
import type { SessionParams } from './types';
@ -28,31 +29,29 @@ export class DatabagSDK {
public async initOfflineStore(sql: SqlStore): Promise<Session | null> {
this.store = new OfflineStore(this.log, sql);
const login = await this.store.init();
if (!login) {
return null;
}
return new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp);
return login ? new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp) : null
}
public async initOnlineStore(web: WebStore): Promise<Session | null> {
this.store = new OnlineStore(this.log, web);
const login = await this.store.init();
if (!login) {
return null;
}
return new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp);
return login ? new SessionModule(this.store, this.crypto, this.log, login.token, login.url, login.timestamp) : null
}
public async login(handle: string, password: string, url: string, mfaCode: string | null, params: SessionParams): Promise<Session> {
const { appName, version, deviceId, deviceToken, pushType, notifications } = params;
const { guid, appToken, created, pushSupported } = await setLogin(url, handle, password, mfaCode, appName, version, deviceId, deviceToken, pushType, notifications);
const login: Login = { guid, url, token: appToken, timestamp: created, pushSupported };
this.store.setLogin(login);
await this.store.setLogin(login);
return new SessionModule(this.store, this.crypto, this.log, appToken, url, created);
}
public async access(url: string, token: string, params: SessionParams): Promise<Session> {
return new SessionModule(this.store, this.crypto, this.log, '', '', 0);
const { appName, version, deviceId, deviceToken, pushType, notifications } = params;
const { guid, appToken, created, pushSupported } = await setAccess(url, token, appName, version, deviceId, deviceToken, pushType, notifications);
const login: Login = { guid, url, token: appToken, timestamp: created, pushSupported };
await this.store.setLogin(login);
return new SessionModule(this.store, this.crypto, this.log, appToken, url, created);
}
public async create(handle: string, password: string, url: string, token: string | null, params: SessionParams): Promise<Session> {
@ -60,7 +59,7 @@ export class DatabagSDK {
const { appName, version, deviceId, deviceToken, pushType, notifications } = params;
const { guid, appToken, created, pushSupported } = await setLogin(url, handle, password, null, appName, version, deviceId, deviceToken, pushType, notifications);
const login: Login = { guid, url, token: appToken, timestamp: created, pushSupported };
this.store.setLogin(login);
await this.store.setLogin(login);
return new SessionModule(this.store, this.crypto, this.log, appToken, url, created);
}

View File

@ -6,8 +6,6 @@ export async function addAccount(url: string, username: string, password: string
const endpoint = `${url}/account/profile${access}`
const auth = encode(`${username}:${password}`)
console.log("ADD ACCOUNT: ", endpoint);
const response = await Promise.race<{ status: number }>([
axios.post(endpoint, null, { headers: { 'Credentials' : `Basic ${auth}` } }).catch(err => { throw new Error('addAccount failed') }),
new Promise((_, reject) => setTimeout(() => reject(new Error('addAccount timeout')), 60000))

View File

@ -0,0 +1,12 @@
import axios from 'redaxios';
import { encode } from './base64';
export async function setAccess(url: string, token: string, appName: string, appVersion: string, platform: string, deviceToken: string, pushType: string, notifications: { event: string, messageTitle: string }[]): Promise<{ guid: string, appToken: string, created: number, pushSupported: boolean }> {
const endpoint = `${url}/account/access?token=${token}&appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}&pushType=${pushType}`
const response = await axios.put(endpoint, notifications)
if (response.status >= 400 && response.status < 600) {
throw new Error('setAccess failed')
}
return response.data;
}