adding add account endpoint

This commit is contained in:
balzack 2024-07-12 22:14:51 -07:00
parent 07277a0fd7
commit 7e3433e65a
3 changed files with 27 additions and 3 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 { addAccount } from './net/addAccount';
import type { Session, Node, Bot, SqlStore, WebStore, Crypto, Logging } from './api';
import type { SessionParams } from './types';
import type { Login } from './entities';
@ -21,7 +22,7 @@ export class DatabagSDK {
this.crypto = crypto;
this.store = new NoStore();
this.log = log ? log : new ConsoleLogging();
this.log.info("new new databag sdk");
this.log.info("databag sdk");
}
public async initOfflineStore(sql: SqlStore): Promise<Session | null> {
@ -55,7 +56,12 @@ export class DatabagSDK {
}
public async create(handle: string, password: string, url: string, token: string | null, params: SessionParams): Promise<Session> {
return new SessionModule(this.store, this.crypto, this.log, '', '', 0);
await addAccount(url, handle, password, token);
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);
return new SessionModule(this.store, this.crypto, this.log, appToken, url, created);
}
public async logout(session: Session): Promise<void> {

View File

@ -0,0 +1,18 @@
import axios from 'redaxios';
import { encode } from './base64';
export async function addAccount(url: string, username: string, password: string, token: string | null): Promise<void> {
const access = token ? `?token=${token}` : ''
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))
]);
if (response.status >= 400 && response.status < 600) {
throw new Error('addAccount failed');
}
}

View File

@ -2,7 +2,7 @@ import axios from 'redaxios';
import { encode } from './base64';
export async function setLogin(url: string, username: string, password: string, code: string | null, 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 mfa = code ? `$code=${code}` : '';
const mfa = code ? `&code=${code}` : '';
const endpoint = `${url}/account/apps?appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}&pushType=${pushType}${mfa}`;
const auth = encode(`${username}:${password}`);
const response = await axios.post(endpoint, notifications, { auth: `Basic ${auth}` });