Skip to content

Instantly share code, notes, and snippets.

@pipiscrew
Last active October 22, 2016 10:35
Show Gist options
  • Save pipiscrew/9483082 to your computer and use it in GitHub Desktop.
Save pipiscrew/9483082 to your computer and use it in GitHub Desktop.
[CSharp] Firebase *Custom Login* via REST API
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace FBDesktop
{
public partial class Form2 : Form
{
string token = null;
string internalURL = null;
public Form2(string URL)
{
InitializeComponent();
textBox1.Text = URL;
getCurrentValue();
}
private void generateToken()
{
//https://github.com/firebase/firebase-token-generator-php
//https://github.com/johnsheehan/jwt
var tokenGenerator = new Firebase.TokenGenerator("-your-secret-here");
var authPayload = new Dictionary<string, object>()
{
{ "app_user_id" , 1234},
{"isADMIN" , true }
};
//----token needed, when RULES applied to database----
token = tokenGenerator.CreateToken(authPayload);
//edit - field name
//http://domainio.com/people/21/name/.json?auth=" + token
//or to edit people/21 priority
//http://domainio.com/people/21/.priority/.json?auth=" + token
internalURL = textBox1.Text + "/.priority/.json?auth=" + token;
}
private void getCurrentValue()
{
try
{
if (token == null)
generateToken();
var http = (HttpWebRequest)WebRequest.Create(new Uri(internalURL));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "GET";
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
textBox3.Text = textBox2.Text = content.Replace("\"", "");
}
catch (Exception Exception)
{
label3.Text = "";
MessageBox.Show(Exception.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
var http = (HttpWebRequest)WebRequest.Create(new Uri(internalURL));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "PUT";
//when hit http://domainio.com/people/21/
//overwrite existing fields, delete the existing fields that doesnt exist to our JSON PUT!!
//string json = "{\"approved_by_admin_id\":\"true\"}";
//string json = "{\"approved_by_admin_id\":\"true\",\".priority\":\"123\"}";
//when hit http://domainio.com/people/21/name/.json?auth=" + token
//updates only field name, and keep existing fields
string json = "\"" + textBox3.Text + "\""; //pure value with quotes (no json)
string parsedContent = json;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
label3.Text = content;
label3.ForeColor = Color.Red;
}
catch (Exception Exception)
{
label3.Text = "";
MessageBox.Show(Exception.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment