-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
167 lines (139 loc) ยท 4.12 KB
/
Copy pathscript.js
File metadata and controls
167 lines (139 loc) ยท 4.12 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
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
160
161
162
163
164
165
166
167
// ๐น DEBUG
console.log("script.js loaded");
// ๐น Firebase config
const firebaseConfig = {
apiKey: "AIzaSyAhhMuccY9MnOxSDhlJgnAdpVOQYubGLbg",
authDomain: "skillswap-e13ee.firebaseapp.com",
projectId: "skillswap-e13ee",
storageBucket: "skillswap-e13ee.firebasestorage.app",
messagingSenderId: "966257721764",
appId: "1:966257721764:web:eeffd75d8322265dffc131"
};
// ๐น Initialize Firebase
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
// ๐น Firebase services
const auth = firebase.auth();
const db = firebase.firestore();
// ================= SIGNUP =================
function signup() {
console.log("Signup clicked");
const email = document.getElementById("signup-email").value.trim();
const password = document.getElementById("signup-password").value;
const name = document.getElementById("signup-name").value;
const branch = document.getElementById("branch").value;
const year = document.getElementById("year").value;
if (!email || !password || !name || !branch || !year) {
alert("Please fill all fields");
return;
}
// โ
Allow only Somaiya emails
const allowedDomains = ["@somaiya.edu", "@somaiya.edu.in"];
const isAllowed = allowedDomains.some(domain => email.endsWith(domain));
if (!isAllowed) {
alert("Please use your official Somaiya email ID");
return;
}
auth.createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
const user = userCredential.user;
return db.collection("users").doc(user.uid).set({
name,
email,
branch,
year,
points: 0
});
})
.then(() => {
alert("Account created successfully!");
window.location.href = "login.html";
})
.catch((error) => {
console.error(error);
alert(error.message);
});
}
// ================= LOGIN =================
function login() {
console.log("Login clicked");
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value;
if (!email || !password) {
alert("Please enter email and password");
return;
}
auth.signInWithEmailAndPassword(email, password)
.then(() => {
window.location.href = "dashboard.html";
})
.catch((error) => {
alert(error.message);
});
}
// ================= LOGOUT =================
function logoutUser() {
auth.signOut()
.then(() => {
window.location.href = "login.html";
})
.catch(error => {
alert(error.message);
});
}
// ================= AUTH PROTECTION =================
auth.onAuthStateChanged(user => {
const path = window.location.pathname;
// Protect dashboard & internal pages
if (
!user &&
(path.includes("dashboard") ||
path.includes("profile") ||
path.includes("browse") ||
path.includes("requests") ||
path.includes("reward") ||
path.includes("points"))
) {
window.location.href = "login.html";
}
});
// ================= POST SKILL =================
function postSkill() {
console.log("Post Skill clicked");
const skillName = document.getElementById("skillName").value.trim();
const days = document.getElementById("days").value.trim();
const time = document.getElementById("time").value.trim();
const desc = document.getElementById("desc").value.trim();
// ๐ Basic validation
if (!skillName || !days || !time || !desc) {
alert("Please fill all fields");
return;
}
const user = auth.currentUser;
if (!user) {
alert("You must be logged in");
return;
}
// ๐ฆ Save skill in Firestore
db.collection("skills").add({
skillName: skillName,
days: days,
time: time,
description: desc,
mentorId: user.uid,
createdAt: firebase.firestore.FieldValue.serverTimestamp()
})
.then(() => {
alert("Skill posted successfully ๐");
// clear inputs
document.getElementById("skillName").value = "";
document.getElementById("days").value = "";
document.getElementById("time").value = "";
document.getElementById("desc").value = "";
})
.catch(error => {
console.error(error);
alert("Error posting skill");
});
}