Published
Edited
Feb 4, 2022
2 forks
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { authorize_link, state } with {
CLIENT_ID,
CLIENT_ID as REDIRECT_URI,
AUTHORIZE_URL,
TOKEN_URL,
TOKEN_PARAMS
} from '@tomlarkworthy/oauth'
Insert cell
Insert cell
Insert cell
Insert cell
userWithNull = Generators.queue(next => {
firebase.auth().onAuthStateChanged(auth => {
console.log("Auth state changed", auth)
next(auth)
})
})
Insert cell
user = {
return userWithNull ? userWithNull : invalidation
}
Insert cell
Insert cell
Insert cell
decoded = state && state.access_token ? JSON.parse(atob(state.access_token.split('.')[1])): undefined
Insert cell
exchange = {
if (state && state.access_token && (
decoded.uid !== userWithNull?.uid || hasNewClaims(decoded.claims, currentClaims)
)) {
console.log("Using access_token for Firebase signin")
firebase.auth().signInWithCustomToken(state.access_token)
}
}
Insert cell
function hasNewClaims(newClaims, oldClaims) {
return Object.keys(newClaims || {}).some(newClaim => {
if (!oldClaims[newClaim]) return true
if (!deepEqual(newClaims[newClaim], oldClaims[newClaim])) return true
})
}
Insert cell
// https://stackoverflow.com/a/38416465/862295 Thanks Frank Tan!
function deepEqual(a,b)
{
if( (typeof a == 'object' && a != null) &&
(typeof b == 'object' && b != null) ) {
var count = [0,0];
for( var key in a) count[0]++;
for( var key in b) count[1]++;
if ( count[0]-count[1] != 0) {return false;}
for( var key in a) {
if (!(key in b) || !deepEqual(a[key],b[key])) {return false;}
}
for( var key in b) {
if (!(key in a) || !deepEqual(b[key],a[key])) {return false;}
}
return true;
} else {
return a === b;
}
}
Insert cell
currentClaims = userWithNull ? JSON.parse(atob((await userWithNull.getIdToken()).split(".")[1]))
: {}
Insert cell
user
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more