-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
154 lines (135 loc) · 4.18 KB
/
auth.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
document.querySelector("#signout").addEventListener("click", () => {
signOut();
});
const register = () => {
const email = document.querySelector("#registrationEmail").value;
const password = document.querySelector("#registrationPassword").value;
if (email.trim() == "") {
alert("Enter Email");
} else if (password.trim().length < 7) {
alert("Password must be at least 7 characters");
} else if (email != reemail) {
alert("emails do not match");
} else {
auth
.createUserWithEmailAndPassword(email, password)
.catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
// ...
});
}
};
const name = document.querySelector("#institute").value;
const number = document.querySelector("#registrationNumber").value;
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// Updates the user attributes:
user
.updateProfile({
// <-- Update Method here
instutue: name,
phoneNumber: phone,
// photoURL: "https://example.com/jane-q-user/profile.jpg",
})
.then(
function () {
// Profile updated successfully!
// "NEW USER NAME"
var displayName = user.displayName;
// "https://example.com/jane-q-user/profile.jpg"
var photoURL = user.photoURL;
},
function (error) {
// An error happened.
}
);
}
});
document.querySelector("#register").addEventListener("click", () => {
register();
});
//register when you hit the enter key
document
.querySelector("#registration-password")
.addEventListener("keyup", (e) => {
if (event.keyCode === 13) {
e.preventDefault();
register();
}
});
const login = () => {
const email = document.querySelector("#login-email").value;
const password = document.querySelector("#login-password").value;
if (email.trim() == "") {
alert("Enter Email");
} else if (password.trim() == "") {
alert("Enter Password");
} else {
authenticate(email, password);
}
};
document.querySelector("#login").addEventListener("click", () => {
login();
});
//sign in when you hit enter
document.querySelector("#login-password").addEventListener("keyup", (e) => {
if (event.keyCode === 13) {
e.preventDefault();
login();
}
});
const authenticate = (email, password) => {
// const auth = firebase.auth();
auth.signInWithEmailAndPassword(email, password);
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
});
};
const showHomepage = () => {
document.querySelector("#registration-page").classList.add("hide");
document.querySelector("#login-page").classList.add("hide");
document.querySelector("#homepage").classList.remove("hide");
};
const signOut = () => {
firebase
.auth()
.signOut()
.then(function () {
location.reload();
})
.catch(function (error) {
alert("error signing out, check network connection");
});
};
auth.onAuthStateChanged((firebaseUser) => {
if (firebaseUser) {
showHomepage();
}
});
document.querySelector("#forgot-password").addEventListener("click", () => {
const email = document.querySelector("#login-email").value;
if (email.trim() == "") {
alert("Enter Email");
} else {
forgotPassword(email);
}
});
const forgotPassword = (email) => {
auth
.sendPasswordResetEmail(email)
.then(function () {
alert("email sent");
})
.catch(function (error) {
alert("invalid email or bad network connection");
});
};