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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more