|
|
@ -18,9 +18,12 @@ |
|
|
|
const functions = require('firebase-functions'); |
|
|
|
const cookieParser = require('cookie-parser'); |
|
|
|
const crypto = require('crypto'); |
|
|
|
const firebase = require('firebase'); |
|
|
|
firebase.initializeApp({ |
|
|
|
serviceAccount: require('./service-account.json'), |
|
|
|
|
|
|
|
// Firebase Setup
|
|
|
|
const admin = require('firebase-admin'); |
|
|
|
const serviceAccount = require('./service-account.json'); |
|
|
|
admin.initializeApp({ |
|
|
|
credential: admin.credential.cert(serviceAccount), |
|
|
|
databaseURL: `https://${process.env.GCLOUD_PROJECT}.firebaseio.com` |
|
|
|
}); |
|
|
|
|
|
|
@ -54,7 +57,6 @@ exports.redirect = functions.https().onRequest((req, res) => { |
|
|
|
const oauth2 = instagramOAuth2Client(); |
|
|
|
|
|
|
|
cookieParser()(req, res, () => { |
|
|
|
try { |
|
|
|
const state = req.cookies.state || crypto.randomBytes(20).toString('hex'); |
|
|
|
console.log('Setting verification state:', state); |
|
|
|
res.cookie('state', state.toString(), {maxAge: 3600000, secure: true, httpOnly: true}); |
|
|
@ -65,9 +67,6 @@ exports.redirect = functions.https().onRequest((req, res) => { |
|
|
|
}); |
|
|
|
console.log('Redirecting to:', redirectUri); |
|
|
|
res.redirect(redirectUri); |
|
|
|
} catch (e) { |
|
|
|
res.status(500).send(e.toString()); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
@ -95,12 +94,17 @@ exports.token = functions.https().onRequest((req, res) => { |
|
|
|
redirect_uri: OAUTH_REDIRECT_URI |
|
|
|
}).then(results => { |
|
|
|
console.log('Auth code exchange result received:', results); |
|
|
|
const token = createFirebaseToken(results.user.id); |
|
|
|
res.jsonp({ |
|
|
|
token: token, |
|
|
|
displayName: results.user.full_name, |
|
|
|
photoURL: results.user.profile_picture, |
|
|
|
instagramAccessToken: results.access_token |
|
|
|
|
|
|
|
// We have an Instagram access token and the user identity now.
|
|
|
|
const accessToken = results.access_token; |
|
|
|
const instagramUserID = results.user.id; |
|
|
|
const profilePic = results.user.profile_picture; |
|
|
|
const userName = results.user.full_name; |
|
|
|
|
|
|
|
// Create a Firebase account and get the Custom Auth Token.
|
|
|
|
createFirebaseAccount(instagramUserID, userName, profilePic, accessToken).then(firebaseToken => { |
|
|
|
// Serve an HTML page that signs the user in and updates the user profile.
|
|
|
|
res.jsonp({token: firebaseToken}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -110,16 +114,41 @@ exports.token = functions.https().onRequest((req, res) => { |
|
|
|
}); |
|
|
|
|
|
|
|
/** |
|
|
|
* Creates a Firebase custom auth token for the given Instagram user ID. |
|
|
|
* Creates a Firebase account with the given user profile and returns a custom auth token allowing |
|
|
|
* signing-in this account. |
|
|
|
* Also saves the accessToken to the datastore at /instagramAccessToken/$uid |
|
|
|
* |
|
|
|
* @returns String The Firebase custom auth token. |
|
|
|
* @returns {Promise<string>} The Firebase custom auth token in a promise. |
|
|
|
*/ |
|
|
|
function createFirebaseToken(instagramUID) { |
|
|
|
function createFirebaseAccount(instagramID, displayName, photoURL, accessToken) { |
|
|
|
// The UID we'll assign to the user.
|
|
|
|
const uid = `instagram:${instagramUID}`; |
|
|
|
const uid = `instagram:${instagramID}`; |
|
|
|
|
|
|
|
// Save the access token tot he Firebase Realtime Database.
|
|
|
|
const databaseTask = admin.database().ref(`/instagramAccessToken/${uid}`) |
|
|
|
.set(accessToken); |
|
|
|
|
|
|
|
// Create the custom token.
|
|
|
|
const token = firebase.app().auth().createCustomToken(uid); |
|
|
|
// Create or update the user account.
|
|
|
|
const userCreationTask = admin.auth().updateUser(uid, { |
|
|
|
displayName: displayName, |
|
|
|
photoURL: photoURL |
|
|
|
}).catch(error => { |
|
|
|
// If user does not exists we create it.
|
|
|
|
if (error.code === 'auth/user-not-found') { |
|
|
|
return admin.auth().createUser({ |
|
|
|
uid: uid, |
|
|
|
displayName: displayName, |
|
|
|
photoURL: photoURL |
|
|
|
}); |
|
|
|
} |
|
|
|
throw error; |
|
|
|
}); |
|
|
|
|
|
|
|
// Wait for all async task to complete then generate and return a custom auth token.
|
|
|
|
return Promise.all([userCreationTask, databaseTask]).then(() => { |
|
|
|
// Create a Firebase custom auth token.
|
|
|
|
const token = admin.auth().createCustomToken(uid); |
|
|
|
console.log('Created Custom token for UID "', uid, '" Token:', token); |
|
|
|
return token; |
|
|
|
}); |
|
|
|
} |
|
|
|