-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
135 lines (114 loc) · 4.4 KB
/
firebase.js
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
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-app.js";
import {
signInWithEmailAndPassword,
getAuth,
} from "https://www.gstatic.com/firebasejs/10.4.0/firebase-auth.js";
import { getDatabase, ref, child, get } from "https://hackisuv2tnt-default-rtdb.firebaseio.com/";
const database = getDatabase();
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyCr1y5hKS0XcP6aMHlSE8WF0NuF_rpn0N8",
authDomain: "hackisuv2tnt.firebaseapp.com",
projectId: "hackisuv2tnt",
storageBucket: "hackisuv2tnt.appspot.com",
messagingSenderId: "853060428664",
appId: "1:853060428664:web:74a1fcbd2464353eb95e8d",
measurementId: "G-VDP80W1NK8",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Applicant Login
window.loginApplicant = function () {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const loginError = document.getElementById("loginError");
const auth = getAuth(app);
// Sign in with Firebase Authentication
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// User signed in successfully
const user = userCredential.user;
const uid = user.id;
console.log("User signed in:", user);
//loginSuccess.innerText = "Login successful!";
window.location.href = "index_applicant.html";
})
.catch((error) => {
// Handle sign-in errors
const errorMessage = error.message;
console.error("Sign-in error:", errorMessage);
loginError.innerText = errorMessage;
});
return false; // Prevent form submission
};
// Company Login
window.loginCompany = function () {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const loginError = document.getElementById("loginError");
const auth = getAuth(app);
// Sign in with Firebase Authentication
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// User signed in successfully
const user = userCredential.user;
const uid = user.id;
console.log("User signed in:", user);
//loginSuccess.innerText = "Login successful!";
window.location.href = "index_company.html";
})
.catch((error) => {
// Handle sign-in errors
const errorMessage = error.message;
console.error("Sign-in error:", errorMessage);
// loginError.innerText = errorMessage;
});
return false; // Prevent form submission
};
// Function to handle resume upload
function uploadResume() {
const fileInput = document.getElementById("resume");
const uploadStatus = document.getElementById("uploadStatus");
if (fileInput.files.length === 0) {
uploadStatus.textContent = "Please select a file to upload.";
return;
}
const file = fileInput.files[0];
const allowedFileTypes = [
"application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
];
if (!allowedFileTypes.includes(file.type)) {
uploadStatus.textContent =
"Invalid file type. Please upload a PDF or Word document.";
return;
}
uploadStatus.textContent = "File uploaded successfully.";
}
// Retrieve user data from Firebase
// var userId = "USER_ID";
var userRef = database.ref('user/' + 1);
const dbRef = ref(getDatabase());
get(child(dbRef, `users/${userId}`)).then((snapshot) => {
if (snapshot.exists()) {
console.log(snapshot.val());
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
userRef.on('value', function(snapshot) {
var userData = snapshot.val();
// Populate HTML fields with Firebase data
document.getElementById('username').textContent = userData.username || 'N/A';
document.getElementById('name').textContent = userData.name || 'N/A';
document.getElementById('email').textContent = userData.email || 'N/A';
document.getElementById('phone').textContent = userData.phone || 'N/A';
document.getElementById('location').textContent = userData.location || 'N/A';
document.getElementById('school').textContent = userData.school || 'N/A';
document.getElementById('major').textContent = userData.major || 'N/A';
document.getElementById('gradYear').textContent = userData.gradYear || 'N/A';
});