2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 01:44:04 +00:00
books/fyo/core/authHandler.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Fyo } from 'fyo';
import { AuthDemux } from 'fyo/demux/auth';
import { AuthDemuxBase } from 'utils/auth/types';
import { Creds } from 'utils/types';
import { AuthDemuxConstructor } from './types';
2022-03-22 09:28:36 +00:00
2022-03-22 05:31:03 +00:00
interface AuthConfig {
serverURL: string;
backend: string;
port: number;
}
interface Session {
user: string;
token: string;
}
2022-03-22 09:28:36 +00:00
export class AuthHandler {
2022-03-22 05:31:03 +00:00
#config: AuthConfig;
#session: Session;
fyo: Fyo;
#demux: AuthDemuxBase;
#creds?: Creds;
2022-03-22 05:31:03 +00:00
constructor(fyo: Fyo, Demux?: AuthDemuxConstructor) {
this.fyo = fyo;
2022-03-22 05:31:03 +00:00
this.#config = {
serverURL: '',
backend: 'sqlite',
port: 8000,
};
this.#session = {
user: '',
token: '',
};
if (Demux !== undefined) {
this.#demux = new Demux(fyo.isElectron);
} else {
this.#demux = new AuthDemux(fyo.isElectron);
}
2022-03-22 05:31:03 +00:00
}
set user(value: string) {
this.#session.user = value;
}
get user(): string {
return this.#session.user;
}
2022-03-22 05:31:03 +00:00
get session(): Readonly<Session> {
return { ...this.#session };
}
get config(): Readonly<AuthConfig> {
return { ...this.#config };
}
init() {
return null;
}
async getCreds(): Promise<Creds> {
if (!this.#creds) {
this.#creds = await this.#demux.getCreds();
}
return this.#creds;
}
2022-03-22 05:31:03 +00:00
}