@@ -136,23 +136,6 @@ class LoginViewController: UIViewController {
136136 print ( " passed user object " )
137137 eventsVC. currentUser = self . currentUser
138138 }
139-
140-
141- // Check for MainTabBarController (skip through nav controller)
142- // Note: not used!
143- /*
144- if let tabVC = segue.destination.children.first as? MainTabBarController {
145-
146- // Check if valid user (on error, user will reauth)
147- guard userID != 0 else {
148- MessageHandler.showInvalidUserError()
149- return
150- }
151-
152- // Pass data to the tab bar controller, which will handle passing its own children
153- tabVC.userID = userID
154- tabVC.oauthGrant = oauthGrant
155- } */
156139 }
157140 }
158141
@@ -176,13 +159,15 @@ class LoginViewController: UIViewController {
176159 SVProgressHUD . show ( )
177160 }
178161
162+ // MARK: User data request
179163 URLSession . shared. dataTask ( with: signedNameRequest) { ( data, response, error) in
180164
181165 // MARK: Error checking
182166 guard error == nil else {
183167 DispatchQueue . main. async {
184168 MessageHandler . showNetworkError ( withText: error!. localizedDescription)
185169 SVProgressHUD . dismiss ( )
170+ self . logout ( )
186171 }
187172 return
188173 }
@@ -191,6 +176,7 @@ class LoginViewController: UIViewController {
191176 DispatchQueue . main. async {
192177 MessageHandler . showUserDataParsingError ( )
193178 SVProgressHUD . dismiss ( )
179+ self . logout ( )
194180 }
195181 return
196182 }
@@ -200,6 +186,7 @@ class LoginViewController: UIViewController {
200186 DispatchQueue . main. async {
201187 MessageHandler . showNetworkError ( withText: " Invalid response code " )
202188 SVProgressHUD . dismiss ( )
189+ self . logout ( )
203190 }
204191 return
205192 }
@@ -208,30 +195,50 @@ class LoginViewController: UIViewController {
208195 DispatchQueue . main. async {
209196 MessageHandler . showNetworkError ( withText: " User account not found " )
210197 SVProgressHUD . dismiss ( )
198+ self . logout ( )
211199 }
212200 return
213201 }
214202
215-
216203 // MARK: Data conversion
217204
205+ var unknownErrorOccured = false
206+
218207 // Convert server data to our User object
219208 do {
220209 self . currentUser = try JSONDecoder ( ) . decode ( User . self, from: data)
221- } catch ( let error) {
210+ } catch {
211+
212+ print ( " Got in error " )
213+
214+ // This error might be fixable!
215+ // Admin accounts don't have a questionnaire, and if the user was
216+ // able to login, then this is almost certainly an admin account.
217+ // (Or their questionnaire doesn't exist).
218+
219+ unknownErrorOccured = true
222220 DispatchQueue . main. async {
223- print ( " parsing error: \( error) " )
224- MessageHandler . showUserDataParsingError ( withText: " Unable to convert JSON " )
225- SVProgressHUD . dismiss ( )
221+ MessageHandler . showUnknownUserDataError ( )
226222 }
227- return
223+
224+ // In this case, we just assign some blank data.
225+ self . currentUser = User ( firstName: " Mystery " , lastName: " User " , major: " Bachelors of codeRIT " )
228226 }
229227
230- // Now that we have the user data, go to the main screen,
231- // passing the data forward!
232- DispatchQueue . main. async {
233- SVProgressHUD . dismiss ( )
234- self . performSegue ( withIdentifier: " authSuccessSegue " , sender: self )
228+ // Now that we have the user data (or fake admin data),
229+ // go to the main screen, and pass the data forward!
230+
231+ // If error occured, give user like three seconds to read it before moving on
232+ if unknownErrorOccured {
233+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + . seconds( 3 ) ) {
234+ SVProgressHUD . dismiss ( )
235+ self . performSegue ( withIdentifier: " authSuccessSegue " , sender: self )
236+ }
237+ } else {
238+ DispatchQueue . main. async {
239+ SVProgressHUD . dismiss ( )
240+ self . performSegue ( withIdentifier: " authSuccessSegue " , sender: self )
241+ }
235242 }
236243
237244 return
0 commit comments