Skip to content

Commit

Permalink
ExperimentalAuth WIP: add chain of signIn() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunanoordin committed Nov 8, 2024
1 parent d4165bd commit 736a4eb
Showing 1 changed file with 88 additions and 4 deletions.
92 changes: 88 additions & 4 deletions packages/lib-panoptes-js/src/experimental-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,69 @@ Based on PJC: https://github.com/zooniverse/panoptes-javascript-client/blob/8157
*/

const globalStore = {
eventListeners: {}
eventListeners: {},
_currentUserPromise: null,
//_bearerToken: '',
//_bearerTokenExpiration: NaN,
//_refreshToken: '',
//_tokenRefreshPromise: null,
}

async function checkCurrent (_store) {
const store = _store || globalStore
console.log('+++ experimental auth client: checkCurrent()')

broadcastEvent('change', 'wah wah') // TEST
if (!store._currentUserPromise) {
console.log('Checking current user')
store._currentUserPromise = _getUser(store)
await store._currentUserPromise
broadcastEvent('change', store._currentUserPromise, store)
}

return store._currentUserPromise
/*
Orignal PJC code
if (!this._currentUserPromise) {
console.log('Checking current user');
this._currentUserPromise = this._getUser();
await this._currentUserPromise;
this.emit('change', this._currentUserPromise);
}
// return Promise.resolve(undefined) // TODO: not sure if this is what should be returned by oh well
return this._currentUserPromise;
*/
}

async function _getUser (_store) {
const store = _store || globalStore
console.log('+++ experimental auth client: getUser()')

try {
const token = await _getBearerToken(store)
return _getSession(store)
} catch (error) {
// Nobody's signed in. This isn't an error.
console.info('No current user')
return null
}
}

async function _getBearerToken (_store) {
const store = _store || globalStore
console.log('+++ experimental auth client: getUser()')

// TODO
}

async function _getSession (_store) {
const store = _store || globalStore
console.log('+++ experimental auth client: _getSession()')

// TODO
}


/*
Adds event listener.
*/
Expand Down Expand Up @@ -62,9 +113,42 @@ function broadcastEvent (eventType, args, _store) {
})
}

function signIn (login, password) {
async function signIn (login, password, _store) {
// TODO
const store = _store || globalStore
console.log('+++ experimental auth client: signIn() ', login, password)

const user = await checkCurrent(_store)

console.log('+++ user: ', user)

/*
Original PJC code:
const user = await this.checkCurrent();
if (user) {
await this.signOut();
return this.signIn(credentials);
} else {
console.log('Signing in', credentials.login);
const token = await getCSRFToken(config.host)
const data = {
authenticity_token: token,
user: {
login: credentials.login,
password: credentials.password,
remember_me: true,
},
};
const signInRequest = this._makeSignInRequest(data);
this._currentUserPromise = signInRequest.catch(() => null);
await this._currentUserPromise;
this.emit('change', this._currentUserPromise);
return signInRequest;
}
*/
}

export {
Expand Down

0 comments on commit 736a4eb

Please sign in to comment.