Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Simplifying Login with Swift Enums

David East
October 27, 2015

Simplifying Login with Swift Enums

Apps that support only one authentication method can often get one-star reviews. This is because on-boarding users isn't just about getting them to tap the login button. It's about building trust, and trust is established through options - Just adding Facebook or Google login isn't going cut it.

Unless your app deeply integrates with the provider there's no excuse not to support multiple authentication methods, but supporting multiple methods brings multiple complexities.

Learn how to simplify the process using classic patterns like delegation and view controller subclassing. By the end of the talk you'll have everything you need to building a trustworthy authentication flow.

David East

October 27, 2015
Tweet

Other Decks in Technology

Transcript

  1. Enums enum LoginProvider { case Facebook case Email case Google

    case Twitter } Not defaulted to 0,1,2,3. Each is a fully-fledged value with an explicit type of LoginProvider.
  2. enum LoginProvider: String { case Facebook = "facebook" case Email

    = "email" case Google = "google" case Twitter = "twitter" } let provider = LoginProvider.init(rawValue: "email")
  3. enum LoginProvider { case Facebook case Email: (String, String) case

    Google case Twitter } let provider = LoginProvider.Email("[email protected]", "pass")
  4. struct LoginUser { let email: String let password: String func

    isValid() -> Bool { return email != "" && password != "" } }
  5. enum LoginProvider { case Facebook case Email: (LoginUser) case Google

    case Twitter } let user = LoginUser(email: "[email protected]", pass: "pass") let provider = LoginProvider.Email(user)
  6. switch provider { case let .Email(user) where user.isValid(): // login!

    break case let .Email(user) where !user.isValid(): // don’t login! break }
  7. enum LoginProvider { func login() { switch self { case

    let .Email(user) where user.isValid(): // login! break } } }
  8. Login SDKs Facebook - Async call to login, async call

    for user info Google - Requires two delegates Twitter - Async call to get accounts, async call to login Email & Password - Totally custom
  9. protocol LoginProviderDelegate { func loginProvider(loginProvider: LoginProvider, didSucceed user: User) func

    loginProvider(loginProvider: LoginProvider, didError error: NSError) }
  10. func login(delegate: LoginProviderDelegate) { switch self { case let .Email(loginUser)

    where loginUser.isValid(): // make server call to login LoginProvider.emailHelper.login(user) { (user, error) in delegate.loginProvider(self, didSucceed: user) } } }
  11. class ViewController: UIViewController, LoginProviderDelegate { let provider = LoginProvider.Facebook @IBAction

    func loginDidTouch() { provider.login(self) } func loginProvider(loginProvider: LoginProvider, didSucceed: User) { print(user) }