Last active
December 13, 2016 03:22
-
-
Save hungdv136/d313d14a345923b00e6a7e5402e2a072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AuthenticationActions.swift | |
// rx.redux.github-browser-example | |
// | |
// Created by Hung Dinh on 12/02/16. | |
// Copyright © 2016 chucuoi.net. All rights reserved. | |
// | |
import RxRedux | |
import RxReduxRouter | |
import Octokit | |
struct AuthenticateActionCreator { | |
func authenticateUser() -> (@escaping GetState, @escaping DispatchFunction) -> Void { | |
return { getState, dispatch in | |
guard let state = getState() as? AppState, let config = state.authenticationState?.oAuthConfig else { return } | |
let authResult = config.authenticate() | |
if let authResult = authResult { | |
_ = dispatch(AuthenticationAction.setOAuthURL(oAuthUrl: authResult)) | |
_ = dispatch(NavigationActions.setRouteAction(route: [loginRoute, oAuthRoute], animated: true)) | |
} | |
} | |
} | |
func handleOpenURL(url: URL) -> (@escaping GetState, @escaping DispatchFunction) -> Void { | |
return { getState, dispatch in | |
guard let state = getState() as? AppState else { return } | |
state.authenticationState?.oAuthConfig?.handleOpenURL(url: url) { (config: TokenConfiguration) in | |
AuthenticationService().saveAuthenticationData(config) | |
_ = dispatch(AuthenticationAction.updateLoggedInState(loggedInState: .loggedIn(config))) | |
_ = dispatch(NavigationActions.setRouteAction(route: [mainViewRoute], animated: true)) | |
} | |
} | |
} | |
} | |
enum AuthenticationAction: Action { | |
case setOAuthURL(oAuthUrl: URL) | |
case updateLoggedInState(loggedInState: LoggedInState) | |
case initializeGitHubConfigurations | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment