-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
262 lines (224 loc) · 6.01 KB
/
popup.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
$(document).ready(function() {
setInitialUI();
$('#enter-email-button').click(() => {
showLoader()
chrome.runtime.sendMessage({
action: 'com.duo.verifyEmail',
payload: {
email: $('#enter-email-text-field').val()
}
}, data => {
hideLoader()
if (data.error)
return flashError(data.error)
// Otherwise we got a user back; check if they're an admin
console.log(`Got user: ${JSON.stringify(data)}`)
chrome.storage.sync.set({duoUserData: data})
if (data.is_admin || data.signed_up)
showEnterPassword(data)
else
showSignUp(data)
})
})
$('#sign-up-button').click(() => {
getDuoUserData().then(duoUserData => {
if (!duoUserData || !duoUserData.email) {
showEnterEmail(() => flashError('Someting went wrong. Please try again.'))
return
}
if (!duoUserData.name)
return flashError('There is no name associated with your account. Please contact your administrator.')
const password = $('#sign-up-password-input').val()
if (!password)
return flashError('You must enter a password.')
const confirmPassword = $('#sign-up-password-confirm-input').val()
if (password !== confirmPassword)
return flashError('Passwords must match.')
showLoader()
chrome.runtime.sendMessage({
action: 'com.duo.signUp',
payload: {
id: duoUserData.id,
email: duoUserData.email,
password
}
}, data => {
hideLoader()
if (data.error)
flashError(data.error)
else {
showEnterEmail(() => flashSuccess('Successfully signed up. Please log in.'))
}
})
})
})
$('#enter-password-button').click(() => {
showLoader()
getDuoUserData().then(loginData => {
if (!loginData || !loginData.email) {
showEnterEmail(() => flashError('Something went wrong. Please try again.'))
return
}
const password = $('#login-password-input').val()
chrome.runtime.sendMessage({
action: 'com.duo.login',
payload: {
email: loginData.email,
password,
}
}, data => {
hideLoader()
if (data.error)
flashError(data.error)
else
loginData.is_admin ? showAdminHome() : showHome()
})
})
})
});
/** Utitlies */
const fetchCurrentUser = () => {
return new Promise(res => {
chrome.runtime.sendMessage({action: 'com.duo.fetchCurrentUser'}, res)
})
}
const getDuoUserData = () => {
return new Promise(res => [
chrome.storage.sync.get(['duoUserData'], ({ duoUserData }) =>
res(duoUserData)
)
])
}
const logout = () => chrome.runtime.sendMessage({action: 'com.duo.logout'})
/** UI ELEMENTS */
const showLoader = () => {
show($('#loader'))
show($('#loader-spinner'))
}
const hideLoader = () => {
hide($('#loader'))
hide($('#loader-spinner'))
}
const flashError = message => {
$('#flash').css('background-color', '#f44336')
showFlash(message)
}
const flashSuccess = message => {
$('#flash').css('background-color', '#4caf50')
showFlash(message)
}
const showFlash = message => {
$('#flash-text').text(message)
$('#flash').show()
}
const hide = element => {
element.css('visibility', 'hidden')
}
const show = element => element.css('visibility', 'visible')
const setInitialUI = async () => {
hideAllPages()
const user = await fetchCurrentUser()
if (user) {
const data = await getDuoUserData()
if (!data) return showEnterEmail()
data.is_admin ? showAdminHome() : showHome()
} else {
showEnterEmail()
}
}
/** SHOWING PAGES */
const showHome = () => {
showPage('home', {
action: {
icon: 'exit_to_app',
onClick: () => {
logout()
showEnterEmail()
}
}
})
fetchCurrentUser().then(user => $('#home .subtitle').text(user.email))
}
const showAdminHome = () => {
showPage('admin-home', {
action: {
icon: 'exit_to_app',
onClick: () => {
logout()
showEnterEmail()
}
}
})
fetchCurrentUser().then(user => $('#admin-home .subtitle').text('Admin: ' + user.email))
}
const showEnterEmail = (cb) => {
chrome.storage.sync.get(['duoUserData'], ({ duoUserData: data }) => {
showPage('enter-email', null, null, () => {
if (data != null)
$('#enter-email-text-field').val(data.email)
if (cb) cb()
})
})
}
const showSignUp = loginData => {
showPage(
'sign-up',
{subtitle: `Welcome, ${getFirstName(loginData.name)}! Please create a password.`},
showEnterEmail
)
}
const showEnterPassword = loginData => {
showPage(
'enter-password',
{subtitle: `Welcome, ${loginData.is_admin ? 'Admin' : getFirstName(loginData.name)}! Please enter your password.`},
showEnterEmail
)
}
/**
* Can create a right-side bar button item by passing the 'action' key in as data.
* Its value should be an object formatted like so:
*
* {
* icon: string
* onClick: () => {}
* }
*
* @param {String} page
* @param {Object} data Takes keys: subtitle, action
* @param {Function} backButtonListener
* @param {Function} onLoad
*/
const showPage = (page, data, backButtonListener, onLoad) => {
// Handle data
const actionButton = $('#title-action-button')
actionButton.hide()
if (data) {
if (data.subtitle)
$(`#${page} .subtitle`).text(data.subtitle)
console.log(data.action)
if (data.action) {
actionButton.show()
actionButton.off().click(data.action.onClick)
actionButton.text(data.action.icon)
}
}
const backButton = $('#back-button')
if (backButtonListener) {
backButton.show()
backButton.off().click(backButtonListener)
} else {
backButton.hide()
}
hideAllPages()
$(`#${page}`).show()
onLoad && onLoad()
}
const hideAllPages = () => {
$('#enter-email').hide()
$('#sign-up').hide()
$('#enter-password').hide()
$('#home').hide()
$('#admin-home').hide()
// Also hide the error flash
$('#flash').hide()
}