mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-13 08:13:49 +10:00
57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
/* eslint-disable no-underscore-dangle */
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
import { delay } from '../../../src/utils/index';
|
|
import Constants from '../constants/auth';
|
|
|
|
const singleton = Symbol('');
|
|
const singletonEnforcer = Symbol('');
|
|
|
|
class Auth {
|
|
constructor(enforcer) {
|
|
if (enforcer !== singletonEnforcer) {
|
|
throw new Error('Cannot construct singleton');
|
|
}
|
|
|
|
this._uuid = uuidv4();
|
|
this._onAuthStateChangedObservers = [];
|
|
}
|
|
|
|
static get instance() {
|
|
if (!this[singleton]) {
|
|
this[singleton] = new Auth(singletonEnforcer);
|
|
}
|
|
|
|
return this[singleton];
|
|
}
|
|
|
|
get uuid() {
|
|
return this._uuid;
|
|
}
|
|
|
|
get onAuthStateChangedObservers() {
|
|
return this._onAuthStateChangedObservers;
|
|
}
|
|
|
|
onAuthStateChanged(observer) {
|
|
this.onAuthStateChangedObservers.push(observer);
|
|
|
|
return () => {
|
|
this._onAuthStateChangedObservers =
|
|
this.onAuthStateChangedObservers.filter((obs) => obs !== observer);
|
|
};
|
|
}
|
|
|
|
async signInAnonymously() {
|
|
const user = Constants.anonymousUser1;
|
|
|
|
await delay(Constants.defaultDelayInMilliseconds);
|
|
|
|
this.onAuthStateChangedObservers.forEach((observer) => observer(user));
|
|
|
|
return user;
|
|
}
|
|
}
|
|
|
|
export default Auth;
|