@@ -46,51 +46,7 @@ domReady.then(() => {
46
46
47
47
// Check for former authentication
48
48
cookieData = Cookies . getJSON ( "user_data" ) ;
49
-
50
- if (
51
- // If there is former data, check if it is not outdated.
52
- cookieData
53
-
54
- // See if current date is later than origin date + expiration period
55
- && Date . now ( ) < cookieData . access_granted + cookieData . expires_in * 1000
56
- ) {
57
- // Request a fresh token
58
- var xhr = new XMLHttpRequest ( ) ;
59
- xhr . onreadystatechange = function ( ) {
60
- if ( this . readyState === 4 && this . status === 200 ) {
61
- let response = JSON . parse ( xhr . responseText ) ;
62
-
63
- if ( response . authorized && response . refreshed && response . tokenBody ) {
64
- response . tokenBody . id = cookieData . id ;
65
- response . tokenBody . username = cookieData . username ;
66
- response . tokenBody . discriminator = cookieData . discriminator ;
67
- response . tokenBody . avatar = cookieData . avatar ;
68
- let days = ( response . tokenBody . expires_in / 62400 ) - 0.1 ; // seconds to days minus some slack
69
- Cookies . set ( "user_data" , response . tokenBody , {
70
- expires : days ,
71
- path : "/" ,
72
- domain : window . location . hostname ,
73
- secure : config . ssl
74
- } ) ;
75
- cookieData = response . tokenBody ;
76
- }
77
-
78
- // Redundant check as non-authorized requests are returned as a 400
79
- if ( response . authorized ) {
80
- onAuthorization ( cookieData ) ;
81
- }
82
- }
83
- } ;
84
- xhr . open ( "POST" , "/chat" , true ) ;
85
- xhr . setRequestHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
86
- xhr . send (
87
- JSON . stringify ( {
88
- "type" : "refresh_token" ,
89
- "id" : cookieData . id ,
90
- "access_token" : cookieData . access_token
91
- } )
92
- ) ;
93
- }
49
+ checkAuthentication ( ) ;
94
50
95
51
let lastMessageSent = Date . now ( ) ;
96
52
let sendMessage = function ( message ) {
@@ -189,24 +145,25 @@ domReady.then(() => {
189
145
} , false ) ;
190
146
191
147
// Make log out button functional
192
- let chatButtonLogOut = document . getElementById ( "buttonLogOut" ) ;
193
- chatButtonLogOut . addEventListener ( "click" , function ( ) {
194
- if ( confirm ( "Do you really wish to log out?" ) ) {
195
- Cookies . remove ( "user_data" ,
196
- {
197
- path : "/" ,
198
- domain : window . location . hostname
148
+ for ( let chatButtonLogOut of document . getElementsByClassName ( "buttonLogOut" ) ) {
149
+ chatButtonLogOut . addEventListener ( "click" , function ( ) {
150
+ if ( confirm ( "Do you really wish to log out?" ) ) {
151
+ Cookies . remove ( "user_data" ,
152
+ {
153
+ path : "/" ,
154
+ domain : window . location . hostname
155
+ }
156
+ ) ;
157
+
158
+ // Send to parent if applicable
159
+ if ( inIframe ( ) ) {
160
+ window . top . postMessage ( userState . AUTH_CHANGED , `${ window . location . origin } /client` ) ;
199
161
}
200
- ) ;
201
162
202
- // Send to parent if applicable
203
- if ( inIframe ( ) ) {
204
- window . top . postMessage ( userState . AUTH_CHANGED , `${ window . location . origin } /client` ) ;
163
+ window . location . reload ( true ) ;
205
164
}
206
-
207
- window . location . reload ( true ) ;
208
- }
209
- } , false ) ;
165
+ } , false ) ;
166
+ }
210
167
} ) ;
211
168
212
169
let authWindow ;
@@ -224,9 +181,9 @@ function authenticationWindow() {
224
181
window . addEventListener ( "message" , receiveMessage , false ) ;
225
182
function receiveMessage ( event ) {
226
183
if ( event . data && event . data . success && event . origin === window . location . origin ) {
227
- onAuthorization ( event . data . response ) ;
228
184
cookieData = Cookies . getJSON ( "user_data" ) ;
229
185
authWindow . close ( ) ;
186
+ checkAuthentication ( ) ;
230
187
231
188
// Send to parent if applicable
232
189
if ( inIframe ( ) ) {
@@ -240,3 +197,55 @@ function onAuthorization(data) {
240
197
document . getElementById ( "userName" ) . innerText = `${ data . username } #${ data . discriminator } ` ;
241
198
document . getElementById ( "chatInputContainer" ) . className = "authorized" ;
242
199
}
200
+
201
+ function onBanned ( ) {
202
+ document . getElementById ( "chatInputContainer" ) . className = "banned" ;
203
+ }
204
+
205
+ function checkAuthentication ( ) {
206
+ if (
207
+ // If there is former data, check if it is not outdated.
208
+ cookieData
209
+
210
+ // See if current date is later than origin date + expiration period
211
+ && Date . now ( ) < cookieData . access_granted + cookieData . expires_in * 1000
212
+ ) {
213
+ // Request a fresh token
214
+ var xhr = new XMLHttpRequest ( ) ;
215
+ xhr . onreadystatechange = function ( ) {
216
+ if ( this . readyState === 4 && this . status === 200 ) {
217
+ let response = JSON . parse ( xhr . responseText ) ;
218
+
219
+ if ( response . authorized && response . refreshed && response . tokenBody ) {
220
+ response . tokenBody . id = cookieData . id ;
221
+ response . tokenBody . username = cookieData . username ;
222
+ response . tokenBody . discriminator = cookieData . discriminator ;
223
+ response . tokenBody . avatar = cookieData . avatar ;
224
+ let days = ( response . tokenBody . expires_in / 62400 ) - 0.1 ; // seconds to days minus some slack
225
+ Cookies . set ( "user_data" , response . tokenBody , {
226
+ expires : days ,
227
+ path : "/" ,
228
+ domain : window . location . hostname ,
229
+ secure : config . ssl
230
+ } ) ;
231
+ cookieData = response . tokenBody ;
232
+ }
233
+
234
+ if ( response . authorized ) {
235
+ onAuthorization ( cookieData ) ;
236
+ } else if ( response . banned ) {
237
+ onBanned ( ) ;
238
+ }
239
+ }
240
+ } ;
241
+ xhr . open ( "POST" , "/chat" , true ) ;
242
+ xhr . setRequestHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
243
+ xhr . send (
244
+ JSON . stringify ( {
245
+ "type" : "refresh_token" ,
246
+ "id" : cookieData . id ,
247
+ "access_token" : cookieData . access_token
248
+ } )
249
+ ) ;
250
+ }
251
+ }
0 commit comments