-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminlogin.js
More file actions
240 lines (236 loc) · 6.81 KB
/
Copy pathadminlogin.js
File metadata and controls
240 lines (236 loc) · 6.81 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
class AdminLogin {
constructor() {
backHistory();
backRoot();
this.view = getElement("workbox");
new ThemeSwitch("darkmode");
this.emailField = new TextInput(
"email_fieldset",
"Email Address",
"youremail@example.domain",
validType.email
);
this.passField = new TextInput(
"password_fieldset",
"Password",
"Your account password",
validType.nonempty,
true
);
this.uiidField = new TextInput(
"uiid_fieldset",
"UIID",
"Your institution's identifier",
validType.nonempty
);
getElement("createaccount").onclick = (_) => {
showadminregistration(
true,
this.emailField.getInput(),
this.uiidField.getInput()
);
};
this.logInButton = getElement("loginAdminButton");
this.logInLoader = getElement("loginLoader");
this.target = String(getElement("target").innerHTML);
if (!stringIsValid(this.target, validType.nonempty)) {
this.target = locate.admin.target.dashboard;
}
this.section = String(getElement("section").innerHTML);
if (!stringIsValid(this.section, validType.nonempty)) {
this.section = locate.admin.section.account;
}
this.nexturl = getElement("nexturl").innerHTML || false;
this.emailField.validate((_) => {
this.passField.inputFocus();
});
this.passField.validate((_) => {
this.uiidField.inputFocus();
});
this.uiidField.validate();
this.passField.onTextInput((_) => {
this.passField.normalize();
hide(this.passField.forgot);
});
resumeElementRestriction(
this.passField.forgot,
key.admin.forgotpassword,
(_) => {
this.passField.forgot.onclick = (_) => {
this.linkSender();
};
}
);
this.logInButton.onclick = (_) => {
this.loginAdmin();
};
}
linkSender() {
if (!this.emailField.isValid())
return this.emailField.showError(
"Please provide your valid email address to help us reset your password."
);
snackBar(
"To reset your password, a link will be sent to your provided email address.",
"Send Link",
true,
(_) => {
this.passField.forgot.onclick = (_) => {};
this.passField.forgot.innerHTML = "Sending...";
snackBar(`Sending link to ${this.emailField.getInput()}...`);
postJsonData(post.admin.manage, {
external: true,
type: "resetpassword",
action: action.send,
email: this.emailField.getInput(),
}).then((resp) => {
if (resp.event == code.mail.ERROR_MAIL_NOTSENT) {
this.passField.forgot.onclick = (_) => {
this.linkSender();
};
return snackBar("An error occurred", "Report");
}
snackBar(
"If your email address was correct, you'll receive an email from us in a few moments.",
"Hide"
);
restrictElement(
this.passField.forgot,
120,
key.admin.forgotpassword,
(_) => {
this.passField.forgot.innerHTML = "Forgot?";
this.passField.forgot.onclick = (_) => {
this.linkSender();
};
}
);
});
}
);
}
loader = (show = true) => {
visibilityOf(this.logInLoader, show);
visibilityOf(this.logInButton, !show);
opacityOf(this.view, show ? 0.5 : 1);
};
loginAdmin() {
if (
!(
this.emailField.isValid() &&
this.passField.isValid() &&
this.uiidField.isValid()
)
) {
this.emailField.validateNow((_) => {
this.passField.inputFocus();
});
this.passField.validateNow((_) => {
this.uiidField.inputFocus();
});
this.uiidField.validateNow();
} else {
this.loader();
hide(this.passField.forgot);
postJsonData(post.admin.auth, {
action: action.login,
email: String(this.emailField.getInput()).trim(),
password: this.passField.getInput(),
uiid: String(this.uiidField.getInput()).trim(),
target: this.target,
section: this.section,
nexturl: this.nexturl,
})
.then((result) => {
this.handleAuthResult(result);
})
.catch((error) => {
this.loader(false);
});
}
}
handleAuthResult = (result) => {
if (result.event == code.auth.AUTH_SUCCESS) {
saveDataLocally(result.user);
if (result.nexturl) {
return relocate(result.nexturl);
}
return relocate(locate.admin.session, {
u: result.user.uid,
target: result.target,
section: result.section,
});
}
this.loader(false);
switch (result.event) {
case code.auth.WRONG_PASSWORD: {
this.passField.showError(constant.nothing);
show(this.passField.forgot);
return (this.logInButton.innerHTML = "Retry");
}
case code.auth.WRONG_UIID: {
return this.uiidField.showError("Incorrect UIID.");
}
case code.auth.REQ_LIMIT_EXCEEDED: {
snackBar(
"Too many unsuccessfull attempts, try again after a while.",
"Hide",
actionType.negative
);
return (this.logInButton.innerHTML = "Disabled");
}
case code.auth.USER_NOT_EXIST: {
this.emailField.showError("Account not found.");
this.logInButton.textContent = "Retry";
return snackBar(
"Try registering a new account?",
"Create Account",
true,
(_) => {
showadminregistration(
true,
this.emailField.getInput(),
this.uiidField.getInput()
);
}
);
}
case code.auth.EMAIL_INVALID: {
return validateTextField(this.emailField, validType.email);
}
case code.auth.ACCOUNT_RESTRICTED: {
this.logInButton.textContent = "Retry";
return snackBar(
"This account has been disabled. You might want to contact us directly.",
"Help",
false,
(_) => {
feedBackBox(
true,
getLogInfo(
result.event,
"This account has been disabled. You might want to contact us directly."
),
true
);
}
);
}
case code.auth.AUTH_REQ_FAILED: {
this.logInButton.textContent = "Retry";
return snackBar("Request failed.", null, false);
}
default: {
this.logInButton.textContent = "Retry";
show(this.passField.forgot);
return snackBar(result.event + ":" + result.msg, "Help", false, (_) => {
feedBackBox(true, result.event, true);
});
}
}
};
}
window.onload = (_) => {
theme.setNav();
new AdminLogin();
};