Skip to content

Instantly share code, notes, and snippets.

@BadPirate
BadPirate / index.js
Created April 13, 2020 01:50
Custom Firebase Hasura Claims with Firestore
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const cors = require('cors')({
origin: (origin, callback) => {
const allowed = ['http://localhost:3000', 'https://yourfinalwebsite.com']
if (allowed.indexOf(origin) !== -1) {
return callback(null, true)
}
return callback(new Error(`CORS Policy denies ${origin}`), false)
},
@BadPirate
BadPirate / YourClient.js
Created April 12, 2020 20:08
Hasura Firebase Token Refresh
firebase.auth().onAuthStateChanged((firebaseUser) => {
if (firebaseUser) {
return firebaseUser.getIdToken().then((token) => firebase.auth().currentUser.getIdTokenResult()
.then((result) => {
if (result.claims['https://hasura.io/jwt/claims']) {
return token
}
const endpoint = 'https://xxx.cloudfunctions.net/refreshToken'
return fetch(`${endpoint}?uid=${firebaseUser.uid}`).then((res) => {
if (res.status === 200) {
@BadPirate
BadPirate / firebase-jwt-basic-claims.js
Last active April 12, 2020 21:13
Cloud function to allow for realtime update of basic claims
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const cors = require('cors')({ origin: true })
admin.initializeApp(functions.config().firebase)
const updateClaims = (uid) => admin.auth().setCustomUserClaims(uid, {
'https://hasura.io/jwt/claims': {
'x-hasura-default-role': 'user',
'x-hasura-allowed-roles': ['user'],
@BadPirate
BadPirate / README.md
Created January 28, 2020 19:22
A utility class for capturing iOS App Will Suspend, and App Did Un-suspend events

Summary

iOS doesn't report when an app will be suspended (placed from background into a non-processing state) nor does it seem to fire a notification once the app has resumed. There is some confusion about this as there is a notification when the app becomes "active" or will resign the "active" state, however this is not always the right value needed. iOS Apps have a number of states:

  1. Active: App is in the foreground (frontmost) and there are no notifications or menu's pulled over it. Pulling a menu down or getting an external notification or text message will cause the app to "resign" active, and resume active once the alert has been dealt with.
  2. Background: App is not in the foreground but still processing. This happens briefly before suspend if there are no background tasks running, or can be a permanent state if there is a long running background mode (audio, location, etc) running.