Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signing endpoint: Firebase #1388

Closed
1 change: 0 additions & 1 deletion lib/plugins/userIDB.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function userIDB(plugin, mapview) {
})

mapp.utils.userIndexedDB.put('locales', locale)

alert(`User ${mapp.user.email} IndexedDB updated.`)

}}>
Expand Down
7 changes: 4 additions & 3 deletions mod/sign/_sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ The sign module provides access to different request signer methods.
*/

const cloudinary = require('./cloudinary')
const firebase = require('./firebase')

const signerModules = {
cloudinary
cloudinary,
firebase
}

/**

@function signer
@async

@description
The signer method looks up a signerModules method matching the signer request parameter and passes the req/res objects as argument to the matched method.

Expand Down
64 changes: 64 additions & 0 deletions mod/sign/firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
## /sign/firebase

A module for producing signed urls for use with firebase object storage.

@module /sign/firebase
*/

module.exports = async req => sign_firebase(req)

/**
@function sign_firebase

@description Creates a signed url for object storage within firebase.

@param {req} req.email - The users email address.
@param {req} req.instance - The instance the user is on.
@returns {Object} Object containing the signed url e.g {signedUrl: xxxxxx}
*/
async function sign_firebase(req) {

//Check for keys needed to make the request in the workspace
let firebase_keys = 'FIREBASE_AUTH_URL,FIREBASE_API_KEY,TRANSPORT_EMAIL,FIREBASE_PASSWORD,FIREBASE_DB_URL'

Object.keys(process.env).forEach(
element => {
if (firebase_keys.includes(element)) {

//If the key is found remove it from the list
firebase_keys = firebase_keys.replace(`,${element}`, '')
firebase_keys = firebase_keys.replace(`${element},`, '')
firebase_keys = firebase_keys.replace(element, '')
}
}
)

//Send missing keys if something is not available for the request
if (firebase_keys.length > 4) {
return { signedUrl: null, missing_keys: firebase_keys.split(',') }
}

let auth_url = `${process.env.FIREBASE_AUTH_URL}${process.env.FIREBASE_API_KEY}`

Check failure on line 42 in mod/sign/firebase.js

View workflow job for this annotation

GitHub Actions / Run ESLint

'auth_url' is never reassigned. Use 'const' instead
let email = process.env.TRANSPORT_EMAIL

Check failure on line 43 in mod/sign/firebase.js

View workflow job for this annotation

GitHub Actions / Run ESLint

'email' is never reassigned. Use 'const' instead
let password = process.env.FIREBASE_PASSWORD

Check failure on line 44 in mod/sign/firebase.js

View workflow job for this annotation

GitHub Actions / Run ESLint

'password' is never reassigned. Use 'const' instead

//Use the private key to determine whether its a dev or live instance
let environment = process.env.PRIVATE.includes('pg.a') ? 'live' : 'dev'

Check failure on line 47 in mod/sign/firebase.js

View workflow job for this annotation

GitHub Actions / Run ESLint

'environment' is never reassigned. Use 'const' instead

const response = await fetch(auth_url, {
method: 'POST',
body: JSON.stringify(
{
email: email,
password: password,
returnSecureToken: true
}),
});

const responseJSON = await response.json()

const url = `${process.env.FIREBASE_DB_URL}/${environment}_workspaces/${req.params.instance}/${req.params.email}.json?auth=${responseJSON.idToken}`

return { signedUrl: url }
}
Loading