Skip to content

Commit

Permalink
Update UserContext for normalized user authentication flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mioke committed Sep 13, 2022
1 parent fea23c9 commit 562c267
Show file tree
Hide file tree
Showing 31 changed files with 1,095 additions and 345 deletions.
28 changes: 14 additions & 14 deletions ComponentizeDemo/Application/Source/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ class ApplicationModule: ModuleProtocol, ApplicationProtocol {
}

func startApplication() {
AppContext.standard
.setup()
.subscribe { finished in
print(AppContext.standard.previousLaunchedUserId as Any)
guard let authModule = try? self.moduleManager?.bridge.resolve(.auth) as? AuthServiceProtocol else {
fatalError()
}
if let _ = AppContext.standard.previousLaunchedUserId {
authModule.refreshAuthenticationIfNeeded(completion: { user in
print(user)
})
}
}
.disposed(by: disposeBag)
// AppContext.standard
// .setup()
// .subscribe { finished in
// print(AppContext.standard.previousLaunchedUserId as Any)
// guard let authModule = try? self.moduleManager?.bridge.resolve(.auth) as? AuthServiceProtocol else {
// fatalError()
// }
// if let _ = AppContext.standard.previousLaunchedUserId {
// authModule.refreshAuthenticationIfNeeded(completion: { user in
// print(user)
// })
// }
// }
// .disposed(by: disposeBag)
}

}
18 changes: 7 additions & 11 deletions ComponentizeDemo/Auth/Protocol/AuthProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ import Foundation
import MIOSwiftyArchitecture
import RxSwift

public class User: UserProtocol {
final public class User: UserProtocol, Codable {
public var id: String
public var authState: BehaviorSubject<AuthState> = .init(value: .unauthenticated)
public var authConfiguration: AuthController.Configuration = .init(archiveLocation: .database)
public var archivableInfo: UserArchivableInfoProtocol = UserArchiveInfo()
public var contextConfiguration: StandardAppContext.Configuration = .init(archiveLocation: .database)
public var name: String?

public init(id: String) {
self.id = id
}
}

class UserArchiveInfo: UserArchivableInfoProtocol {
static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)

enum CodingKeys: CodingKey {
case id
case contextConfiguration
case name
}
}

Expand Down
16 changes: 8 additions & 8 deletions ComponentizeDemo/Auth/Source/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class AuthModule: ModuleProtocol, AuthServiceProtocol {
}

func refreshAuthenticationIfNeeded(completion: @escaping (User) -> ()) {
if let previousUID = AppContext.standard.previousLaunchedUserId {
let previousUser = User(id: previousUID)
let previousContext = AppContext(user: previousUser)
// get token from previous context data center, like:
// let credential = previousContext.dataCenter.object(with: previousUID, type: Credential.Type)
print(previousContext)
// and then do some refresh token logic here.
}
// if let previousUID = AppContext.standard.previousLaunchedUserId {
// let previousUser = User(id: previousUID)
// let previousContext = AppContext(user: previousUser)
// // get token from previous context data center, like:
// // let credential = previousContext.store.object(with: previousUID, type: Credential.Type)
// print(previousContext)
// // and then do some refresh token logic here.
// }
}

func deauthenticate(completion: @escaping (Error?) -> Void) {
Expand Down
11 changes: 11 additions & 0 deletions README-AppDocker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SwiftyArchitecture - App Docker

## App Context

## User Context

There is a default user or an anonymous user for user context before the user login journey, the default user account has a set of functionalities like store etc.

## Store


16 changes: 16 additions & 0 deletions SwiftArchitecture.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
AB6781392861C8CD007740FA /* ThemeTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB6781372861C8CD007740FA /* ThemeTestViewController.swift */; };
AB67813A2861C8CD007740FA /* ThemeTestViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AB6781382861C8CD007740FA /* ThemeTestViewController.xib */; };
AB67813C2862BB74007740FA /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB67813B2862BB74007740FA /* SceneDelegate.swift */; };
ABAFE34F28C1EAAF00D023A2 /* AuthTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABAFE34D28C1EAAE00D023A2 /* AuthTestViewController.swift */; };
ABAFE35028C1EAAF00D023A2 /* AuthTestViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = ABAFE34E28C1EAAE00D023A2 /* AuthTestViewController.xib */; };
ABC9EA872449AFD2009D044E /* TestObjModifyVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABC9EA852449AFD2009D044E /* TestObjModifyVC.swift */; };
ABC9EA882449AFD2009D044E /* TestObjModifyVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = ABC9EA862449AFD2009D044E /* TestObjModifyVC.xib */; };
ABD632442727FE0E00AA775A /* Video.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABD632432727FE0D00AA775A /* Video.swift */; };
Expand Down Expand Up @@ -95,6 +97,8 @@
AB6781372861C8CD007740FA /* ThemeTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeTestViewController.swift; sourceTree = "<group>"; };
AB6781382861C8CD007740FA /* ThemeTestViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ThemeTestViewController.xib; sourceTree = "<group>"; };
AB67813B2862BB74007740FA /* SceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
ABAFE34D28C1EAAE00D023A2 /* AuthTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthTestViewController.swift; sourceTree = "<group>"; };
ABAFE34E28C1EAAE00D023A2 /* AuthTestViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AuthTestViewController.xib; sourceTree = "<group>"; };
ABC9EA852449AFD2009D044E /* TestObjModifyVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestObjModifyVC.swift; sourceTree = "<group>"; };
ABC9EA862449AFD2009D044E /* TestObjModifyVC.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TestObjModifyVC.xib; sourceTree = "<group>"; };
ABD632432727FE0D00AA775A /* Video.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Video.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -170,6 +174,7 @@
A752DE411C031066008E3118 /* ViewController */ = {
isa = PBXGroup;
children = (
ABAFE34C28C1EA8800D023A2 /* Auth */,
A753A0A21C06FE5A00C9FBFE /* ViewController.swift */,
A77BAD191C196DFD00AF7A86 /* InternalTestVC.swift */,
AB35E652244476B400EC361A /* DataCenterTestViewController.swift */,
Expand Down Expand Up @@ -283,6 +288,15 @@
path = Appearance;
sourceTree = "<group>";
};
ABAFE34C28C1EA8800D023A2 /* Auth */ = {
isa = PBXGroup;
children = (
ABAFE34D28C1EAAE00D023A2 /* AuthTestViewController.swift */,
ABAFE34E28C1EAAE00D023A2 /* AuthTestViewController.xib */,
);
path = Auth;
sourceTree = "<group>";
};
B41F4498FD05D386DBCFBEED /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -414,6 +428,7 @@
A791C6BB1C02E90000670017 /* Assets.xcassets in Resources */,
AB35E655244476B400EC361A /* DataCenterTestViewController.xib in Resources */,
AF437DD127898EB40052FEAE /* ModulesRegistery.plist in Resources */,
ABAFE35028C1EAAF00D023A2 /* AuthTestViewController.xib in Resources */,
AB67813A2861C8CD007740FA /* ThemeTestViewController.xib in Resources */,
A791C6B61C02E90000670017 /* Main.storyboard in Resources */,
ABC9EA882449AFD2009D044E /* TestObjModifyVC.xib in Resources */,
Expand Down Expand Up @@ -574,6 +589,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
ABAFE34F28C1EAAF00D023A2 /* AuthTestViewController.swift in Sources */,
A791C6B11C02E90000670017 /* AppDelegate.swift in Sources */,
AB6781392861C8CD007740FA /* ThemeTestViewController.swift in Sources */,
A77BAD1A1C196DFD00AF7A86 /* InternalTestVC.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion SwiftArchitectureUITests/MockComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension MockAuth: AuthServiceProtocol {
let previousUser = User(id: previousUID)
let previousContext = AppContext(user: previousUser)
// get token from previous context data center, like:
// let credential = previousContext.dataCenter.object(with: previousUID, type: Credential.Type)
// let credential = previousContext.store.object(with: previousUID, type: Credential.Type)
print(previousContext)
// and then do some refresh token logic here.
}
Expand Down
14 changes: 10 additions & 4 deletions SwiftyArchitecture/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ import UIKit
import CoreData
import Alamofire
import ApplicationProtocol
import RxSwift

@_exported import MIOSwiftyArchitecture

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

let bag: DisposeBag = .init()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

// AFNetworkActivityIndicatorManager.sharedManager().enabled = true

#if DEBUG
try? MioDemoServer.switch(to: "DEV")
try? MioDemoServer.switch(to: "DEV")
#endif

print(#function)
Expand All @@ -36,6 +35,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()

// App Context
if let standardContext = AppContext.current as? StandardAppContext {
standardContext.setup(authDelegate: UserService.shared)
}
// UserService.shared.login()

// Modulization setup
if let url = Bundle.main.url(forResource: "ModulesRegistery", withExtension: ".plist") {
try! ModuleManager.default.registerModules(withConfigFilePath: url)
}
Expand Down
Loading

0 comments on commit 562c267

Please sign in to comment.