-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebaseauth.js
More file actions
93 lines (84 loc) · 3.2 KB
/
Copy pathfirebaseauth.js
File metadata and controls
93 lines (84 loc) · 3.2 KB
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
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.1/firebase-app.js";
import {getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword} from "https://www.gstatic.com/firebasejs/10.11.1/firebase-auth.js";
import{getFirestore, setDoc, doc} from "https://www.gstatic.com/firebasejs/10.11.1/firebase-firestore.js"
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDlzbgw-yiXXxfDc99DzHWF8_JR0nlXGx8",
authDomain: "megadrop-d7f9f.firebaseapp.com",
projectId: "megadrop-d7f9f",
storageBucket: "megadrop-d7f9f.firebasestorage.app",
messagingSenderId: "726965576147",
appId: "1:726965576147:web:c601f7db7e2a1e18f44f26"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
function showMessage(message, divId){
var messageDiv=document.getElementById(divId);
messageDiv.style.display="block";
messageDiv.innerHTML=message;
messageDiv.style.opacity=1;
setTimeout(function(){
messageDiv.style.opacity=0;
},5000);
}
const signUp=document.getElementById('submitSignUp');
signUp.addEventListener('click', (event)=>{
event.preventDefault();
const email=document.getElementById('rEmail').value;
const password=document.getElementById('rPassword').value;
const firstName=document.getElementById('fName').value;
const lastName=document.getElementById('lName').value;
const auth=getAuth();
const db=getFirestore();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential)=>{
const user=userCredential.user;
const userData={
email: email,
firstName: firstName,
lastName:lastName
};
showMessage('Account Created Successfully', 'signUpMessage');
const docRef=doc(db, "users", user.uid);
setDoc(docRef,userData)
.then(()=>{
window.location.href='index.html';
})
.catch((error)=>{
console.error("error writing document", error);
});
})
.catch((error)=>{
const errorCode=error.code;
if(errorCode=='auth/email-already-in-use'){
showMessage('Email Address Already Exists !!!', 'signUpMessage');
}
else{
showMessage('unable to create User', 'signUpMessage');
}
})
});
const signIn=document.getElementById('submitSignIn');
signIn.addEventListener('click', (event)=>{
event.preventDefault();
const email=document.getElementById('email').value;
const password=document.getElementById('password').value;
const auth=getAuth();
signInWithEmailAndPassword(auth, email,password)
.then((userCredential)=>{
showMessage('login is successful', 'signInMessage');
const user=userCredential.user;
localStorage.setItem('loggedInUserId', user.uid);
window.location.href='homepage.html';
})
.catch((error)=>{
const errorCode=error.code;
if(errorCode==='auth/invalid-credential'){
showMessage('Incorrect Email or Password', 'signInMessage');
}
else{
showMessage('Account does not Exist', 'signInMessage');
}
})
})