Kodeco Forums

User Authentication on iOS with Ruby on Rails and Swift

Learn how to secure your iOS app by adding user accounts using Swift and a custom Ruby on Rails backend.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2068-user-authentication-on-ios-with-ruby-on-rails-and-swift

I’d be grateful if the Code is updated to Latest Swift syntaxes. The Selfie-Starter code is producing around 17 issues when tried to Build. I’m new to Swift. Otherwise, I’d have tried debugging it myself.

Thank you.

Could you please upgrade your swift project to the latest version so that it can be used? Thanks

I fixed all the errors so that the example app will work on the latest Xcode. Check it out on my github. GitHub - jaekwangseo/user-authentication-in-swift2: Updated for Swift 2.3.

Beware though, I am quite new to ios development/swift so the code might not be optimal.

Subhransu, please let me know if you don’t want your source code on my github account. I will take it down. Thanks.

Hi,

Is there any reason for using NSUserDefaults instead of just checking the keychain for a token?

Best regards,
Jens

The token is not stored in NSUserDefaults. It’s only checking for a boolean flag if user is logged in or not. User defaults is meant for such things to store non-senstive user preferences or flags. Storing this in Keychain will be bit of a overkill as there is nothing senstive about whether is a user is logged or not.

Having said that you should never store any sensitive information in NSUserDefaults.

Please run it through Ray and request him for an update tutorial. I left the tutorial team sometime last year so won’t have time to look into this. However, if you run into any major issues feel free to ping me.

@subhransu Why are you using http basic auth for the sign in and sign up requests? Are you concerned about hackers hitting those API endpoints and creating accounts? I’m guessing that if were doing FB authentication, then you wouldn’t need that level of security, as you’d be passing a new FB auth token for the server to verify. I can see some reason to use http auth for non-FB authentication so that a robot program couldn’t create fake accounts.

Thoughts?

@crzjaek thanks ! But Swift 2 version cannot run with Swift 3. Would you please update it to Swift 3 ?

Xcode with Swift 3 updates. Pretty much went to debugger and hit return, since recommendations preventing build were all literal syntax, instead of context

https://github.com/tristanbnewman/rwenderlich-auth-selfie

@subhransu I see that your code checks for the token expiry date and then forces the user to log in again if this has expired. Could you explain a little about how one would keep a token valid? A scenario would be to always ensure that the session never expires - as happens on many apps such as Facebook, where once you’ve logged in, you stay logged in, even if you don’t use the app for a very long time.
Is this something the app needs to do in the background every so often? What happens if the user kills the app completely (though doesn’t actually remove it)?

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]