19

I have a function where I want to perform some server-side validations, but I"m not sure how to do this? Any suggestions where I should look. THere is nothing in the documentation of how to do it?

rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
Kamilski81
  • 14,409
  • 33
  • 108
  • 161

3 Answers3

21

The best way to do this is to create a "pending" node and a "completed" node in Firebase. Every time the client takes an action that requires server validation, have the client add an entry into the pending node. On the server side, you can use the Firebase node.js client (or Java SDK) to listen for changes on the "pending" node, validate the action, and then put it in the "complete" node if the validation succeeds. You'll need to setup your security rules such that only the server code can add items into the "complete" node (for example, by using the secret) - learn more about the Firebase security rules here: https://www.firebase.com/docs/security/security-rules.html

If your validations are fairly simple to perform, you may be able to do the validation using the security rules themselves - they provide simple string/integer/boolean validation.

Anant
  • 7,408
  • 1
  • 30
  • 30
  • 4
    Hi Anant Since I want to integrate through payment service provider I need to have some server APIs. Can I write server code on firebase like Parse and expose it through API or something? I don't have my own server!! – user1010819 Apr 13 '16 at 13:54
10

It's late. However, just for someone passing by. Firebase has introduce Cloud Function a month ago. Try to check the official link out. It allow you to put some logics in server side.

https://firebase.google.com/docs/functions/

In my understanding instead of thinking about the communication to server as common Request and Response you need to see it as Database trigger event. You can set up the function that will be called when specific action happen.

Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
2

Pattern 2 in this blog article might help. https://firebase.googleblog.com/2013/03/where-does-firebase-fit-in-your-app.html

In this architecture, Firebase sits between the server and clients. Your servers can connect to Firebase and interact with the data just like any other client would. In other words, your server communicates with clients by manipulating data in Firebase. Our Security and Firebase Rules language lets you assign full access to your data to your server. Your server code can then listen for any changes to data made by clients, and respond appropriately.

Yuichi Kato
  • 863
  • 7
  • 16