I'm using Algolia to run my search requests on my Firebase Database. I have Algolia hosted on a Heroku server with a Node.js file. Algolia connects to Firebase via that Node.js app.js file.
For Firebase to authenticate Algolia I need a token which I generate after the user signs in and is authenticated on the client.
The Firebase docs says to send the token to my backend via Https-
Firebase: Get Tokens and Send to Https
I found a similar question asked on Stackoverflow but it doesn't give any code on how to do it. I'm not a Node.js dev and only lightly delved into it so I need more context
How to send Firebase token to backend?
In the function I created below func sendTokenToHerokuAppJsFile()
I can send the token after it’s created. I can then use a url (it’s the second line inside the function) to send the token to but I don’t know where to get the url from.
How do I get the URL to send the token to my Heroku app.js file?
Update: @Freya in the comments very helpfully suggested I create an api on the Node side but I’m not a Node dev
How do I create a API in Node.js to receive the token?
iOS Client Swift File:
@IBAction fileprivate func signinButtonTapped(_ sender: UIButton) {
Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: {
(user, error) in
if error != nil {
return
}
guard let currentUser = user else {
return
}
currentUser?.getIDTokenForcingRefresh(true, completion: {
(idToken, error) in
if error != nil{
return
}
// Send token to your backend via HTTPS
self.sendTokenToHerokuAppJsFile(idToken)
})
}
}
fileprivate func sendTokenToHerokuAppJsFile(_ idToken: String){
let json: [String:Any] = ["token" : idToken]
// HOW DO I GET THIS URL??
let url = URL(string: "https://www.linkToMyHerokuAppJsFile")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
}catch let error as NSError{
print(error.localizedDescription)
return
}
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
guard let data = data, error == nil else{
//do something
return
}
do{
let result = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
print(result as Any)
}catch let error{
print(error.localizedDescription)
}
}
task.resume()
}
Heroku App.Js Server file:
var idToken;
//Firebase-Admin Initialization
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert({
projectId: "********",
clientEmail: "********",
privateKey: "-----BEGIN PRIVATE KEY-----\n********\n-----END PRIVATE KEY-----\n"
}),
databaseURL: "https://********.firebaseio.com/"
});
//Firebase Initialization
var firebase = require('firebase');
var config = {
apiKey: "********",
authDomain: "********.firebaseapp.com",
databaseURL: "https://********.firebaseio.com/",
storageBucket: "********.appspot.com",
messagingSenderId: "********"
};
firebase.initializeApp(config);
admin.auth().verifyIdToken(idToken)
.then(function(decodedToken) {
var uid = decodedToken.uid;
firebase.auth().authenticateWithCustomToken(uid)
}).catch(function(error) {
// Handle error
});
//Algolia Initialization
var algoliasearch = require('algoliasearch');
var algolia = algoliasearch('********', '********');