Skip to content

Commit

Permalink
Fix authentication issue
Browse files Browse the repository at this point in the history
  • Loading branch information
reloni committed Jul 2, 2017
1 parent 62baac1 commit 696d81a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
3 changes: 1 addition & 2 deletions Aika/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

var hasAuthenticationData: Bool {
guard Keychain.userEmail.characters.count > 0,
Keychain.userPassword.characters.count > 0,
guard Keychain.authenticationType != nil,
Keychain.token.characters.count > 0,
Keychain.refreshToken.characters.count > 0,
Keychain.userUuid.characters.count > 0 else {
Expand Down
34 changes: 32 additions & 2 deletions Aika/Common/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,42 @@ extension IconBadgeStyle {
extension Keychain {
private static let keychain = Keychain()

static var userEmail: String {
static var authenticationType: AuthenticationType? {
get {
guard let type = keychain.stringForAccount(account: "authenticationType") else { return nil }

switch type {
case "db": return AuthenticationType.db(email: userEmail, password: userPassword)
case "facebook": return AuthenticationType.facebook
case "google": return AuthenticationType.google
default:return nil
}
}
set {
guard let type = newValue else {
userEmail = ""
userPassword = ""
keychain.setString(string: "", forAccount: "authenticationType")
return
}

switch type {
case .facebook: keychain.setString(string: "facebook", forAccount: "authenticationType")
case .google: keychain.setString(string: "google", forAccount: "authenticationType")
case let .db(email, password):
keychain.setString(string: "db", forAccount: "authenticationType")
userEmail = email
userPassword = password
}
}
}

private static var userEmail: String {
get { return keychain.stringForAccount(account: "userEmail") ?? "" }
set { keychain.setString(string: newValue, forAccount: "userEmail") }
}

static var userPassword: String {
private static var userPassword: String {
get { return keychain.stringForAccount(account: "userPassword") ?? "" }
set { keychain.setString(string: newValue, forAccount: "userPassword") }
}
Expand Down
11 changes: 6 additions & 5 deletions Aika/DataFlow/Reducers/AuthenticationReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ extension AuthenticationReducer {
}

func signOut() -> Observable<RxStateMutator<AppState>> {
Keychain.userPassword = ""
if case let AuthenticationType.db(email, _)? = Keychain.authenticationType {
Keychain.authenticationType = AuthenticationType.db(email: email, password: "")
} else {
Keychain.authenticationType = nil
}
Keychain.token = ""
Keychain.refreshToken = ""
Keychain.userUuid = ""
Expand All @@ -43,10 +47,7 @@ extension AuthenticationReducer {
func logIn(currentState state: AppState, authType: AuthenticationType) -> Observable<RxStateMutator<AppState>> {
return state.authenticationService.logIn(authType: authType)
.flatMapLatest { result -> Observable<RxStateMutator<AppState>> in
if case .db(let data) = authType {
Keychain.userEmail = data.email
Keychain.userPassword = data.password
}
Keychain.authenticationType = authType
Keychain.token = result.token
Keychain.refreshToken = result.refreshToken
Keychain.userUuid = result.uid
Expand Down
3 changes: 1 addition & 2 deletions Aika/DataFlow/Reducers/SystemReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ struct SystemReducer : RxReducerType {
}

func clearKeychain() {
Keychain.userEmail = ""
Keychain.userPassword = ""
Keychain.authenticationType = nil
Keychain.token = ""
Keychain.refreshToken = ""
Keychain.userUuid = ""
Expand Down
9 changes: 7 additions & 2 deletions Aika/ViewModels/AuthenticationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ final class AuthenticationViewModel: ViewModelType {
}
}

var dbAuthentication: (email: String, password: String)? {
guard case let AuthenticationType.db(email, password)? = Keychain.authenticationType else { return nil }
return (email: email, password: password)
}

var email: String {
guard mode == .logIn else { return "" }
return Keychain.userEmail
return dbAuthentication?.email ?? ""
}

var password: String {
guard mode == .logIn else { return "" }
return Keychain.userPassword
return dbAuthentication?.password ?? ""
}

func toggleShowPasswordOrRegistrationEnter() {
Expand Down

0 comments on commit 696d81a

Please sign in to comment.