Skip to content

Commit

Permalink
Refactor dismissLock flag into dispatch func
Browse files Browse the repository at this point in the history
Remove success banner display when auto closing
Added autoClose to options for specifying mode auto closing
Added validation for 'impossible to close Lock' miss configurations
Added more dispatch tests in DatabaseInteractor and DatabasePasswordInteractor
Login will always auto close for all connection types
  • Loading branch information
cocojoe committed Jan 13, 2017
1 parent d7e3c1b commit 95f8cf8
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Lock/Auth0OAuth2Interactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct Auth0OAuth2Interactor: OAuth2Authenticatable {
.start { result in
switch result {
case .success(let credentials):
self.dispatcher.dispatch(result: .auth(credentials))
self.dispatcher.dispatch(result: .auth(credentials), dismissLock: true)
callback(nil)
case .failure(WebAuthError.userCancelled):
callback(.cancelled)
Expand Down
4 changes: 3 additions & 1 deletion Lock/DatabaseForgotPasswordPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class DatabaseForgotPasswordPresenter: Presentable, Loggable {
self.logger.error("Failed with error \(error)")
} else {
let message = "We've just sent you an email to reset your password".i18n(key: "com.auth0.lock.database.forgot.success.message", comment: "forgot password email sent")
self.messagePresenter?.showSuccess(message)
if self.options.allow.contains(.Login) || !self.options.autoClose.contains(.ResetPassword) {
self.messagePresenter?.showSuccess(message)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Lock/DatabaseInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ struct DatabaseInteractor: DatabaseAuthenticatable, DatabaseUserCreator, Loggabl
"verified": user.verified
]
extra["username"] = user.username
self.dispatcher.dispatch(result: .signUp(user.email, extra, !self.options.allow.contains(.Login)))
self.dispatcher.dispatch(result: .signUp(user.email, extra), dismissLock: !self.options.allow.contains(.Login) && self.options.autoClose.contains(.Signup))
callback(nil, nil)
}
case .failure(let cause as AuthenticationError) where cause.isPasswordNotStrongEnough:
Expand Down Expand Up @@ -204,7 +204,7 @@ struct DatabaseInteractor: DatabaseAuthenticatable, DatabaseUserCreator, Loggabl
case .success(let credentials):
self.logger.info("Authenticated user <\(self.identifier)>")
callback(nil)
self.dispatcher.dispatch(result: .auth(credentials))
self.dispatcher.dispatch(result: .auth(credentials), dismissLock: true)
}
}
}
2 changes: 1 addition & 1 deletion Lock/DatabasePasswordInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct DatabasePasswordInteractor: PasswordRecoverable {
.resetPassword(email: email, connection: connection)
.start {
guard case .success = $0 else { return callback(.emailNotSent) }
self.dispatcher.dispatch(result: .forgotPassword(email, !self.options.allow.contains(.Login)))
self.dispatcher.dispatch(result: .forgotPassword(email), dismissLock: !self.options.allow.contains(.Login) && self.options.autoClose.contains(.ResetPassword))
callback(nil)
}
}
Expand Down
4 changes: 3 additions & 1 deletion Lock/DatabasePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ class DatabasePresenter: Presentable, Loggable {
if let databaseView = self.databaseView {
self.showLogin(inView: databaseView, identifier: self.creator.identifier)
}
self.messagePresenter?.showSuccess(message)
if self.options.allow.contains(.Login) || !self.options.autoClose.contains(.Signup) {
self.messagePresenter?.showSuccess(message)
}
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion Lock/EnterpriseActiveAuthInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct EnterpriseActiveAuthInteractor: DatabaseAuthenticatable, Loggable {
case .success(let credentials):
self.logger.info("Authenticated user <\(self.identifier)>")
callback(nil)
self.dispatcher.dispatch(result: .auth(credentials))
self.dispatcher.dispatch(result: .auth(credentials), dismissLock: true)
}
}

Expand Down
1 change: 1 addition & 0 deletions Lock/LockOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct LockOptions: OptionBuildable {
var scope: String = "openid"
var parameters: [String : Any] = [:]
var allow: DatabaseMode = [.Login, .Signup, .ResetPassword]
var autoClose: DatabaseMode = [.Login, .Signup, .ResetPassword]
var initialScreen: DatabaseScreen = .login
var usernameStyle: DatabaseIdentifierStyle = [.Username, .Email]
var customSignupFields: [CustomTextField] = []
Expand Down
2 changes: 1 addition & 1 deletion Lock/LockViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class LockViewController: UIViewController {

header.showClose = self.router.lock.options.closable
header.onClosePressed = { [weak self] in
self?.router.lock.observerStore.dispatch(result: .cancel)
self?.router.lock.observerStore.dispatch(result: .cancel, dismissLock: true)
}
header.apply(style: style)

Expand Down
2 changes: 1 addition & 1 deletion Lock/MultifactorInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct MultifactorInteractor: MultifactorAuthenticatable {
callback(.couldNotLogin)
case .success(let credentials):
callback(nil)
self.dispatcher.dispatch(result: .auth(credentials))
self.dispatcher.dispatch(result: .auth(credentials), dismissLock: true)
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions Lock/ObserverStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ struct ObserverStore: Dispatcher {

weak var controller: UIViewController?

func dispatch(result: Result) {
func dispatch(result: Result, dismissLock: Bool) {
let closure: () -> ()
switch result {
case .auth(let credentials):
closure = dismiss(from: controller?.presentingViewController, completion: { self.onAuth(credentials) })
closure = { self.onAuth(credentials) }
case .error(let error):
closure = { self.onFailure(error) }
case .cancel:
closure = dismiss(from: controller?.presentingViewController, completion: { self.onCancel() })
case .signUp(let email, let attributes, let dismissLock):
closure = dismissLock ? dismiss(from: controller?.presentingViewController, completion: { self.onSignUp(email, attributes) }) : { self.onSignUp(email, attributes) }
case .forgotPassword(let email, let dismissLock):
closure = dismissLock ? dismiss(from: controller?.presentingViewController, completion: { self.onForgetPassword(email) }) : { self.onForgetPassword(email) }
closure = { self.onCancel() }
case .signUp(let email, let attributes):
closure = { self.onSignUp(email, attributes) }
case .forgotPassword(let email):
closure = { self.onForgetPassword(email) }
}
Queue.main.async(closure)

Queue.main.async(dismissLock ? dismiss(from: controller?.presentingViewController, completion: closure) : closure)
}

private func dismiss(from controller: UIViewController?, completion: @escaping () -> ()) -> () -> () {
Expand All @@ -59,10 +60,10 @@ enum Result {
case auth(Credentials)
case error(Error)
case cancel
case signUp(String, [String: Any], Bool)
case forgotPassword(String, Bool)
case signUp(String, [String: Any])
case forgotPassword(String)
}

protocol Dispatcher {
func dispatch(result: Result)
func dispatch(result: Result, dismissLock: Bool)
}
4 changes: 4 additions & 0 deletions Lock/OptionBuildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public protocol OptionBuildable: Options {
/// What database modes are allowed and must be at least one. By default all modes are allowed.
var allow: DatabaseMode { get set }

/// What database modes can be automatically closed upon success. By default all modes are auto closable.
var autoClose: DatabaseMode { get set }

/// Initial screen displayed by Lock when a database connection is available. By default is Login
var initialScreen: DatabaseScreen { get set }

Expand Down Expand Up @@ -87,6 +90,7 @@ internal extension OptionBuildable {
func validate() -> UnrecoverableError? {
guard !self.allow.isEmpty else { return UnrecoverableError.invalidOptions(cause: "Must allow at least one database mode") }
guard !self.usernameStyle.isEmpty else { return UnrecoverableError.invalidOptions(cause: "Must specify at least one username style") }
guard self.allow.contains(.Login) || self.closable || self.autoClose.contains(self.allow) else { return UnrecoverableError.invalidOptions(cause: "Must enable autoclose for database mode or enable closable") }
return nil
}
}
Expand Down
1 change: 1 addition & 0 deletions Lock/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public protocol Options {
var scope: String { get }
var parameters: [String: Any] { get }
var allow: DatabaseMode { get }
var autoClose: DatabaseMode { get }
var initialScreen: DatabaseScreen { get }
var usernameStyle: DatabaseIdentifierStyle { get }
var customSignupFields: [CustomTextField] { get }
Expand Down
Loading

0 comments on commit 95f8cf8

Please sign in to comment.