ExpressJS HTTPS Firebase Auth Restful Webservices Framework

So, if you've read through my blog you'd, know by now, that I do make Restful web services for Java / PHP and I've recently started working with Node.JS and Firebase. Hence, I've decided to create my own HTTPS Restful web services framework that is built on expressJS for Firebase.


As follows.


var express = require('express');// Enable Express

var app = express();


var fs = require('fs');//Enable Https

var https = require('https');



var cors = require('cors'); // Add CORS for browser requests



var firebase = require('firebase');// Enable Firebase

 firebase.initializeApp({

 serviceAccount: 'Something.json here', // Add Firebase key JSON

 databaseURL: 'Firebase db endpoint here' // Add Firebase endpoint

});



// Add endpoint for CORS

app.use(cors({origin: 'Script origin here'}));



// Create Https Server

https.createServer({

   key: fs.readFileSync('key.pem'),

   cert: fs.readFileSync('cert.pem')

  }, app).listen(8081);




app.post("/YOUR_ENDPOINT_NAME", function (req, res) {

firebase.auth().verifyIdToken(req.body.Token).then(function(decodedToken) {

 var uid = decodedToken.uid;

 if(uid ===req.body.uid){

  /*

  *

  *

  *

* Do your work here.

*

*

*

*

*/


 }else{

// If token UID doesnt Match

var info={

message:"Bad Token",

success:false

}

    res.json(info);

 }

}).catch(function(error) { // If theres a Token Error

var info={

message:(error.code + ' - '+ error.message),

success:false

}

res.json(info);

});

});




This is a simple Node.JS Restful framework that is built on expressJS and uses HTTPS.

It has been built to simplify Firebase web services deployment and demonstrate secure Restful web service creation with Firebase.

Ideally, it is geared towards Firebase developers who need to create backend web services, as a mechanism for fast deployment.


Dependencies:


Node.JS

ExpressJS

CORS

https

fs

Firebase



Resources:


Firebase - https://www.firebase.com/docs/

Express - https://expressjs.com/

Cors - https://www.npmjs.com/package/cors

HTTPS - https://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/

FS - https://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/


Deployment:


You will need to create a node project and add the dependencies to your node project and configure the package, json accordingly.

Further, you will need to create and add the HTTPS certificate to the service, for this it is advisable to read up on the HTTPS resource blog post

by Minko Gechev here - https://blog.mgechev.com/2014/02/19/create-https-tls-ssl-application-with-express-nodejs/


Note *** your request body should contain the following fields.


Token: From Firebase session

uid: From Firebase session


Happy Coding :)


Github - https://github.com/bkraad47/ExpressJS-HTTPS-Firebase-Auth-Restful-Framework


要查看或添加评论,请登录

Badruddin Kamal的更多文章

社区洞察

其他会员也浏览了