Skip to content

Instantly share code, notes, and snippets.

@hungdv136
Last active December 13, 2016 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hungdv136/d313d14a345923b00e6a7e5402e2a072 to your computer and use it in GitHub Desktop.
Save hungdv136/d313d14a345923b00e6a7e5402e2a072 to your computer and use it in GitHub Desktop.
//
// 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