-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-introduction.txt
141 lines (107 loc) · 3.95 KB
/
firebase-introduction.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
firebase documentation
- https://firebase.google.com/docs/web/setup
firebase link
- https://firebase.google.com
Create New Project
- give a name to Project
-After Creating project you will get home page panel
-After reaching that particular project select scripting where you want to implememt the project.
-Change in Rule
auth == null;
-Add firebase in out script
like :-
<!--Firebase js file -->
<script src="https://www.gstatic.com/firebasejs/3.6.7/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyB-efHBg6Abr5q_PC3PR_Ow4s6UzpBPbW0",
authDomain: "snappy-7328a.firebaseapp.com",
databaseURL: "https://snappy-7328a.firebaseio.com",
storageBucket: "snappy-7328a.appspot.com",
messagingSenderId: "133956073878"
};
firebase.initializeApp(config);
</script>
-Create a database in Firebase
step 1 :- Create database on firebase
step 2 :- Make url in the app.js
var database = firebase.dataBase();
step 3 :- Make Database Reference
-it will create an object of the key which specify.
var databaseRef = firebase.ref("users");
step 4 :- Take snapshot
databaseRef.on('value',function(snapshot){
console.log(snapshot);
});
/* for access value of snapshot */
databaseRef.on('value',function(snapshot){
console.log(snapshot.val());
});
/* Data which you are getting in console is your real time data */
step 5 :- write data with set property with databaseRef
databaseRef.set({
"email":"dhiralkaniya3146@gmail.com"
});
step 6 :- Create userRef is the branch as root with the child so we can add multple record from our website.
//create userRef
var userRef = database.ref("users");
//Create its branch
userRef.child("1st-child").set({
"Name":"Dhiral Kaniya"
})
userRef.child("2nd-child").set({
"Name":"Harmeet Singh",
"Email":"harmeet@gmail.com"
});
-sing in with Google
step 1 :- Go to Authentication
step 2 :- Enable Email/password which is specify google password
step 3 :- Add your local Domain to use of Add Domain
step 4 :- go to authentication documentation
- Google Sign-in authenticaiton
step 5 :- Take Provider from the documentation
step 6 :- Create provider object in the function where we want to authenticate
step 7 :- Take firebase authentication from the documentation
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
createUser(user.uid,user.displayName,user.email);
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
//By using above step you can easily embedded google+ in your application with the use of firebase
step 8 :- if the user is successfull create user with the
function createUser(uid,name,email){
// Get a reference to the database service
/*
This function is call when user is click on sign-in-with google button and its authorised used.
*/
var database = firebase.database();
var userRef = database.ref("users");
var user = {
id : uid,
name : name,
email : email
}
userRef.child(uid).set(user).then(function(){
redirect("chat.html");
});
}
step 9 :- Change the database authentication rule with auth != null
-got to the Manage user
-user get the currently signed-in user method
-create ifUserisLoggedIn function
-go through that function
-take that code in to our function
-call that method from the app.js