@@ -65,7 +65,6 @@ class LoginViewController: UIViewController {
6565 print ( " Authorization successful. " )
6666
6767 // If login is successful, continue to main app
68- // @FIXME: "NavController on SFViewController, whose view is not in the window hierarchy!"
6968 self . loginFlow ( )
7069
7170 }
@@ -89,6 +88,9 @@ class LoginViewController: UIViewController {
8988 " redirect_uris " : [ " brickhack-ios://oauth/callback " ] ,
9089 " scope " : " Access-your-bricks " ] as OAuth2JSON )
9190
91+ // User model instance
92+ var currentUser : User ?
93+
9294 // @TODO: Nonexistent value is 0 by default, maybe wrap somehow to nil?
9395 var userID : Int {
9496 get {
@@ -106,7 +108,9 @@ class LoginViewController: UIViewController {
106108 if hasInternetAccess ( ) {
107109
108110 // Only continue automatically if authenticated, AND user data is persisted
109- if oauthGrant. hasUnexpiredAccessToken ( ) && userID != 0 {
111+ if oauthGrant. hasUnexpiredAccessToken ( ) && currentUser != nil {
112+
113+ print ( " currentUser: \( currentUser) " )
110114
111115 // Continue to main app if authorized, don't show spinner
112116 self . performSegue ( withIdentifier: " authSuccessSegue " , sender: self )
@@ -120,7 +124,18 @@ class LoginViewController: UIViewController {
120124 override func prepare( for segue: UIStoryboardSegue , sender: Any ? ) {
121125 if ( segue. identifier == " authSuccessSegue " ) {
122126
127+ // Make the destination be fullscreen
128+ segue. destination. modalPresentationStyle = . fullScreen
129+
130+ // Pass data forward (temp to main screen)
131+ if let eventsVC = segue. destination as? EventsViewController {
132+ print ( " passed user object " )
133+ eventsVC. currentUser = self . currentUser
134+ }
135+
136+
123137 // Check for MainTabBarController (skip through nav controller)
138+ // Note: not used!
124139 if let tabVC = segue. destination. children. first as? MainTabBarController {
125140
126141 // Check if valid user (on error, user will reauth)
@@ -141,19 +156,10 @@ class LoginViewController: UIViewController {
141156
142157 func loginFlow( ) {
143158
144- // Generate signed request for userID
145- let idRequest = signURLRequest ( withRoute: Routes . currentUser)
146- guard let signedIDRequest = idRequest else {
147- DispatchQueue . main. async {
148- MessageHandler . showAuthSigningError ( )
149- }
150- return
151- }
152-
153159 // Generate signed request for username
154160 // (user id is added in network chain)
155161 let nameRequest = signURLRequest ( withRoute: Routes . questionnaire)
156- guard var signedNameRequest = nameRequest else {
162+ guard let signedNameRequest = nameRequest else {
157163 DispatchQueue . main. async {
158164 MessageHandler . showAuthSigningError ( )
159165 }
@@ -165,10 +171,9 @@ class LoginViewController: UIViewController {
165171 SVProgressHUD . show ( )
166172 }
167173
168- // Networking!
169- // First, grab the user id from teh server.
170- URLSession . shared. dataTask ( with: signedIDRequest) { ( data, response, error) in
174+ URLSession . shared. dataTask ( with: signedNameRequest) { ( data, response, error) in
171175
176+ // MARK: Error checking
172177 guard error == nil else {
173178 DispatchQueue . main. async {
174179 MessageHandler . showNetworkError ( withText: error!. localizedDescription)
@@ -179,112 +184,53 @@ class LoginViewController: UIViewController {
179184
180185 guard let data = data else {
181186 DispatchQueue . main. async {
182- MessageHandler . showNetworkError ( withText : error! . localizedDescription )
187+ MessageHandler . showUserDataParsingError ( )
183188 SVProgressHUD . dismiss ( )
184189 }
185190 return
186191 }
187192
188- // Convert server data to JSON
189- var json : [ String : Any ]
190- do {
191- json = try JSON ( data: data) . dictionaryObject!
192- } catch {
193+ // Check response code
194+ guard let httpResponse = response as? HTTPURLResponse else {
193195 DispatchQueue . main. async {
194- MessageHandler . showNetworkError ( withText: error . localizedDescription )
196+ MessageHandler . showNetworkError ( withText: " Invalid response code " )
195197 SVProgressHUD . dismiss ( )
196198 }
197199 return
198200 }
199201
200- // Grab our integer from it
201- let userIDConverted = json [ " resource_owner_id " ] as? Int
202-
203- // Check cast
204- guard let userID = userIDConverted else {
202+ guard httpResponse. statusCode != 404 else {
205203 DispatchQueue . main. async {
206- MessageHandler . showNetworkError ( withText: error! . localizedDescription )
204+ MessageHandler . showNetworkError ( withText: " User account not found " )
207205 SVProgressHUD . dismiss ( )
208206 }
209207 return
210208 }
211209
212- // Save userID
213- UserDefaults . standard. set ( userID, forKey: " userID " )
214- print ( " userID: \( userID) " )
215-
216- // @FIXME: Bypass name functionality for now
217- // Segue to main app
218- DispatchQueue . main. async {
219- self . performSegue ( withIdentifier: " authSuccessSegue " , sender: self )
220- }
221-
222- // Now that we have the user ID, append it and
223- // request the user info.
224- signedNameRequest. url? . appendPathComponent ( " \( userID) .json " )
225- signedNameRequest. addValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
226210
227- // Note: spinner is still visible!
228- URLSession . shared. dataTask ( with: signedNameRequest) { ( data, response, error) in
211+ // MARK: Data conversion
229212
230- guard error == nil else {
231- DispatchQueue . main. async {
232- MessageHandler . showNetworkError ( withText: error!. localizedDescription)
233- SVProgressHUD . dismiss ( )
234- }
235- return
236- }
237-
238- guard let data = data else {
239- DispatchQueue . main. async {
240- MessageHandler . showUserDataParsingError ( )
241- SVProgressHUD . dismiss ( )
242- }
243- return
244- }
245-
246- // Check response code
247- if let httpResponse = response as? HTTPURLResponse {
248- DispatchQueue . main. async {
249- MessageHandler . showNetworkError ( withText: httpResponse. statusString)
250- SVProgressHUD . dismiss ( )
251- }
252- return
253- }
254-
255- // Convert server data to JSON
256- var json : JSON
257-
258- do {
259- json = try JSON ( data: data, options: . allowFragments)
260- } catch {
261- DispatchQueue . main. async {
262- MessageHandler . showUserDataParsingError ( withText: " Unable to convert JSON " )
263- SVProgressHUD . dismiss ( )
264- }
265- return
266- }
267-
268- // @FIXME
269- // Hello, Developer.
270- // At this point, the code should have errored out.
271- // The backend has not implemented the functionality needed for the preivous code to work.
272- // But, just in case something happens, this should do it.
213+ // Convert server data to our User object
214+ do {
215+ self . currentUser = try JSONDecoder ( ) . decode ( User . self, from: data)
216+ } catch ( let error) {
273217 DispatchQueue . main. async {
274- MessageHandler . showUserDataParsingError ( )
218+ print ( " parsing error: \( error) " )
219+ MessageHandler . showUserDataParsingError ( withText: " Unable to convert JSON " )
275220 SVProgressHUD . dismiss ( )
276221 }
277-
278222 return
223+ }
279224
225+ // Now that we have the user data, go to the main screen,
226+ // passing the data forward!
227+ DispatchQueue . main. async {
228+ SVProgressHUD . dismiss ( )
229+ self . performSegue ( withIdentifier: " authSuccessSegue " , sender: self )
230+ }
280231
281- // This is some debugging code for the time being, that will not be reached:
282- let contents = String ( data: data, encoding: . ascii)
283- print ( json)
284- print ( contents!)
285- print ( " JSON data: " )
232+ return
286233
287- } . resume ( )
288234 } . resume ( )
289235
290236 }
0 commit comments