adding personal access token

This commit is contained in:
Ryan Nystrom
2017-08-25 23:37:11 -04:00
parent 3a2a6a1ae7
commit d9e5406aff
7 changed files with 76 additions and 7 deletions

View File

@@ -58,14 +58,16 @@ final class LoginSplashViewController: UIViewController, GithubSessionListener {
@IBAction func onPersonalAccessTokenButton(_ sender: Any) {
let alert = UIAlertController(
title: NSLocalizedString("Personal Access Token", comment: ""),
message: NSLocalizedString("To log in using a Personal Access Token, enter it here:", comment: ""),
message: NSLocalizedString("To sign in using a Personal Access Token, enter it here:", comment: ""),
preferredStyle: .alert
)
alert.addTextField()
alert.addTextField { (textField) in
textField.placeholder = NSLocalizedString("Personal Access Token", comment: "")
}
alert.addAction(UIAlertAction(title: Strings.cancel, style: .cancel))
let logInTitle = NSLocalizedString("Log In", comment: "")
let logInTitle = NSLocalizedString("Sign In", comment: "")
alert.addAction(UIAlertAction(title: logInTitle, style: .default) { [weak alert, weak self] _ in
alert?.actions.forEach { $0.isEnabled = false }

View File

@@ -284,7 +284,13 @@
<outlet property="delegate" destination="Pin-sZ-TSO" id="wQZ-LB-ZNX"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Accounts" id="56A-2S-vCm"/>
<navigationItem key="navigationItem" title="Accounts" id="56A-2S-vCm">
<barButtonItem key="rightBarButtonItem" systemItem="add" id="9Pr-sR-RZb">
<connections>
<action selector="onAdd:" destination="Pin-sZ-TSO" id="Y9Z-Jo-9Ee"/>
</connections>
</barButtonItem>
</navigationItem>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="YHM-PS-ThT" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>

View File

@@ -11,7 +11,55 @@ import UIKit
final class SettingsAccountsViewController: UITableViewController {
var sessionManager: GithubSessionManager!
var client: GithubClient!
// MARK: Private API
@IBAction func onAdd(_ sender: Any) {
let alert = UIAlertController(
title: NSLocalizedString("Add Another Account", comment: ""),
message: NSLocalizedString("To sign in with another account, please add a new Personal Access Token.", comment: ""),
preferredStyle: .alert
)
alert.addTextField { (textField) in
textField.placeholder = NSLocalizedString("Personal Access Token", comment: "")
}
alert.addAction(UIAlertAction(title: Strings.cancel, style: .cancel))
let logInTitle = NSLocalizedString("Sign In", comment: "")
alert.addAction(UIAlertAction(title: logInTitle, style: .default) { [weak alert, weak self] _ in
alert?.actions.forEach { $0.isEnabled = false }
let token = alert?.textFields?.first?.text ?? ""
self?.client.verifyPersonalAccessToken(token: token) { result in
self?.handle(result: result, authMethod: .pat)
}
})
present(alert, animated: true)
}
private func handle(result: GithubClient.AccessTokenResult, authMethod: GithubUserSession.AuthMethod) {
switch result {
case .failure: handleError()
case .success(let user): finishLogin(token: user.token, authMethod: authMethod, username: user.username)
}
}
private func handleError() {
let alert = UIAlertController(
title: NSLocalizedString("Error", comment: ""),
message: NSLocalizedString("There was an error adding another account. Please try again.", comment: ""),
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: Strings.ok, style: .default))
present(alert, animated: true)
}
private func finishLogin(token: String, authMethod: GithubUserSession.AuthMethod, username: String) {
sessionManager.authenticate(token, authMethod: authMethod, username: username)
}
// MARK: UITableViewDataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

View File

@@ -12,6 +12,7 @@ import SafariServices
final class SettingsViewController: UITableViewController {
var sessionManager: GithubSessionManager!
var client: GithubClient!
weak var rootNavigationManager: RootNavigationManager? = nil
@IBOutlet weak var versionLabel: UILabel!
@@ -50,6 +51,7 @@ final class SettingsViewController: UITableViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? SettingsAccountsViewController {
controller.client = client
controller.sessionManager = sessionManager
}
}

View File

@@ -17,6 +17,8 @@ final class RootNavigationManager: GithubSessionListener {
// weak refs to avoid cycles
weak private var rootViewController: UISplitViewController?
private var lastClient: GithubClient? = nil
init(
sessionManager: GithubSessionManager,
rootViewController: UISplitViewController
@@ -49,6 +51,7 @@ final class RootNavigationManager: GithubSessionListener {
guard let userSession = userSession else { return }
let client = newGithubClient(sessionManager: sessionManager, userSession: userSession)
lastClient = client
fetchUsernameForMigrationIfNecessary(client: client, userSession: userSession, sessionManager: sessionManager)
@@ -128,7 +131,13 @@ final class RootNavigationManager: GithubSessionListener {
}
@objc private func onSettings() {
let settings = newSettingsRootViewController(sessionManager: sessionManager, rootNavigationManager: self)
guard let client = lastClient else { return }
let settings = newSettingsRootViewController(
sessionManager: sessionManager,
rootNavigationManager: self,
client: client
)
settings.modalPresentationStyle = .formSheet
rootViewController?.present(settings, animated: true)
}

View File

@@ -10,7 +10,8 @@ import UIKit
func newSettingsRootViewController(
sessionManager: GithubSessionManager,
rootNavigationManager: RootNavigationManager
rootNavigationManager: RootNavigationManager,
client: GithubClient
) -> UIViewController {
guard let controller = UIStoryboard(name: "Settings", bundle: nil).instantiateInitialViewController()
else { fatalError("Could not unpack settings storyboard") }
@@ -18,6 +19,7 @@ func newSettingsRootViewController(
if let nav = controller as? UINavigationController,
let first = nav.viewControllers.first as? SettingsViewController {
first.sessionManager = sessionManager
first.client = client
first.rootNavigationManager = rootNavigationManager
}

View File

@@ -32,7 +32,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1469</string>
<string>1472</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>