      */
     public func logoutAction(success: Bool, error: NSError?) {
         if (success) {
-            self.removeAuthenticatedSubviews()
-            self.setupAnonymousView()
+            self.showAnonymousSubviews()
         } else {
             print("failed to logout")
         }
         self.logoutRequestCompleted(success: success, error: error)
     }
 
+
+    public func cancelAction() {
+        self.cancelButtonHit()
+    }
 
     override public func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()

}
public class waterwheelLoginViewController: UIViewController {

    let usernameField = UITextField()
    let passwordField = UITextField()
    let submitButton = waterwheelAuthButton()

    /// Provide a closure for when a Login Request is completed.
    public var loginRequestCompleted: (success: Bool, error: NSError?) -> () = { _ in }
    /// Provide a closure for when a Logout Request is completed.
    public var logoutRequestCompleted: (success: Bool, error: NSError?) -> () = { _ in }

    override public func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .whiteColor()
        if !waterwheel.isLoggedIn() {
            self.setupAnonymousView()
        } else {
            self.setupAuthenticatedView()
        }
        // Incase of logout or login, we attach to the notification Center for the purpose of seeing requests.
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: #selector(configure),
            name: waterwheelNotifications.waterwheelDidFinishRequest.rawValue,
            object: nil)
    }
    /**
     Overridden viewDidAppear where decide if were logged in or not.

     - parameter animated: Is the view animated
     */
    override public func viewDidAppear(animated: Bool) {
        self.configure()
    }

    /**
     Configure this viewcontrollers view based on auth state.
     */

    public func configure() {
        if !waterwheel.isLoggedIn() {
            self.setupAnonymousView()
        } else {
            self.setupAuthenticatedView()
        }
    }

    /**
     Setup the authenticated Subviews.
     */
    public func setupAuthenticatedView() {
        self.configureAuthenticatedSubviews()
        self.addAuthenticatedSubviews()
        self.layoutAuthenticatedSubviews()

    }
    /**
     Setup the Anonymous Subviews.
     */
    public func setupAnonymousView() {
        self.configureAnonymousSubviews()
        self.addAnonymousSubviews()
        self.layoutAnonymousSubviews()
    }

    /**
     Configure authenticated Subviews.
     */
    public func configureAuthenticatedSubviews() {
        self.configureAuthenticatedButton()
    }

    /**
     Configure Anonymous Subviews.
     */
    public func configureAnonymousSubviews() {
        self.configureUsernameField()
        self.configurePasswordField()
        self.configureAnonymousButton()
    }

    /**
     Configure the username field defaults.
     */
    public func configureUsernameField() {
        usernameField.autocorrectionType = .No
        usernameField.attributedPlaceholder = NSAttributedString(string: "Email")
        usernameField.translatesAutoresizingMaskIntoConstraints = false;
        usernameField.backgroundColor = UIColor.lightGrayColor()
        usernameField.textAlignment = .Center
        usernameField.placeholder = "Username"
    }
    /**
     Configure the password field defaults.
     */
    public func configurePasswordField() {
        passwordField.secureTextEntry = true
        passwordField.autocorrectionType = .No
        passwordField.attributedPlaceholder = NSAttributedString(string: "Password")
        passwordField.backgroundColor = UIColor.lightGrayColor()
        passwordField.textAlignment = .Center
        passwordField.translatesAutoresizingMaskIntoConstraints = false;
        passwordField.placeholder = "test"
    }

    /**
     Configure Anonymous button defaults.
     */
    public func configureAnonymousButton() {
        submitButton.translatesAutoresizingMaskIntoConstraints = false;
        submitButton.backgroundColor = UIColor.darkGrayColor()
        submitButton.didPressLogin = {
            self.loginAction()
        }
    }

    /**
     Configure Authenticated Button defaults.
     */
    public func configureAuthenticatedButton() {
        submitButton.translatesAutoresizingMaskIntoConstraints = false;
        submitButton.backgroundColor = UIColor.darkGrayColor()
        submitButton.didPressLogout = { (success, error) in
            self.logoutAction(success, error: error)
        }
    }

    /**
     Layout the Anonymous Subviews.
     */
    public func layoutAnonymousSubviews() {
        self.layoutLoginField()
        self.layoutPasswordField()
        self.layoutAnonymousButton()
    }

    /**
     Layout the Authenticated Subviews.
     */
    public func layoutAuthenticatedSubviews() {
        self.layoutAuthenticatedButton()
    }

    /**
     Lays out the authenticated button.
     */
    public func layoutAuthenticatedButton() {
        submitButton.constrainEqual(.LeadingMargin, to: view)
        submitButton.constrainEqual(.TrailingMargin, to: view)
        submitButton.constrainEqual(.Bottom, to: view, .Bottom, multiplier: 1.0, constant: -100)
        submitButton.heightAnchor.constraintEqualToConstant(50.0).active = true
    }

    /**

    public func loginAction() {
        waterwheel.login(usernameField.text!, password: passwordField.text!) { (success, response, json, error) in
            if (success) {
                self.hideAnonymousSubviews()
            } else {
                print("failed to login")
            }
