Files
Reactive-Resume/__mocks__/gatsby-plugin-firebase/auth/auth.js
Amruth Pillai 2c22c13f3e - update dependencies
- upgrade gatsby v2 to v3
- update functions
2021-05-29 11:47:34 +05:30

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;