-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.html
160 lines (143 loc) · 5.61 KB
/
copy.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" ></script>
<!-- Bootstrap theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>How to create Firebase login and register?</title>
</head>
<body>
<div class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="https://www.shinerweb.com">Shinerweb Technologies</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" >
<ul class="nav navbar-nav navbar-right">
<li><a href="#" id="logout" style="display: none;">Log Out</a></li>
</ul>
</div>
</div>
<br>
<div class="container">
<form name="registration_form" id="registration_form" method="post" action="#" enctype="multipart/form-data" >
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" id="email" class="form-control" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="password" >Password</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Enter your password">
</div>
<button type="button" id="register" name="register" class="btn btn-success">Register Now</button>
</div><!-- end col -->
</form>
<div class="col-sm-4">
<img src="firebase_auth.png">
</div>
<form name="login_form" id="login_form" method="post" action="#" enctype="multipart/form-data" >
<div class="col-sm-4">
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="login_email" id="login_email" class="form-control" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="login_password" id="login_password" class="form-control" placeholder="Enter your password">
</div>
<button type="button" id="login" name="login" class="btn btn-success">Login</button>
</div><!-- end col -->
</div><!-- end row -->
</form>
</div>
<br>
</body>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-analytics.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-auth.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyBTYAGYuoJv0KGiPRu23c-P3pzJZn6rkCA",
authDomain: "fir-c0309.firebaseapp.com",
databaseURL: "https://fir-c0309-default-rtdb.firebaseio.com",
projectId: "fir-c0309",
storageBucket: "fir-c0309.appspot.com",
messagingSenderId: "1093776995940",
appId: "1:1093776995940:web:51f1d91839c4f8eaeb1171"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth();
console.log(app);
//----- New Registration code start
document.getElementById("register").addEventListener("click", function() {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
//For new registration
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
console.log(user);
alert("Registration successfully!!");
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
console.log(errorMessage);
alert(error);
});
});
//----- End
//----- Login code start
document.getElementById("login").addEventListener("click", function() {
var email = document.getElementById("login_email").value;
var password = document.getElementById("login_password").value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
console.log(user);
alert(user.email+" Login successfully!!!");
document.getElementById('logout').style.display = 'block';
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(errorMessage);
alert(errorMessage);
});
});
//----- End
//----- Logout code start
document.getElementById("logout").addEventListener("click", function() {
signOut(auth).then(() => {
// Sign-out successful.
console.log('Sign-out successful.');
alert('Sign-out successful.');
document.getElementById('logout').style.display = 'none';
}).catch((error) => {
// An error happened.
console.log('An error happened.');
});
});
</script>
</html>