Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import UIKit
  2.  
  3. struct ErrorModel {
  4. let code: Int
  5. let title: String
  6. let message: String
  7. }
  8.  
  9. struct User {
  10. let firstName: String
  11. let lastName: String
  12. let phoneNumber: String
  13. let emailAddress: String
  14. }
  15.  
  16. protocol AuthService {
  17. func authenticateUser(emailAddress: String, password: String, completionHandler: (User?) -> Void)
  18. }
  19.  
  20. protocol AuthServiceComponent {
  21. static var authService: AuthService { get }
  22. static func createAuthService() -> AuthService
  23. }
  24.  
  25. struct TestAuthService: AuthService {
  26. func authenticateUser(emailAddress: String, password: String, completionHandler: (User?) -> Void) {
  27. if emailAddress == "nishant.shrestha@hotmail.com" && password == "password" {
  28. let user = User(firstName: "Nishant", lastName: "Shrestha", phoneNumber: "0478578673", emailAddress: emailAddress)
  29. completionHandler(user)
  30. } else {
  31. completionHandler(nil)
  32. }
  33. }
  34. }
  35.  
  36. protocol TestAuthServiceComponent: AuthServiceComponent {}
  37.  
  38. extension TestAuthServiceComponent {
  39. static func createAuthService() -> AuthService {
  40. return TestAuthService()
  41. }
  42. }
  43.  
  44. enum TestContext: TestAuthServiceComponent {
  45. public static let authService = TestContext.createAuthService()
  46. }
  47.  
  48.  
  49. let testEmailAddress = "nishant.shrestha@hotmail.com"
  50. let testPassword = "password"
  51.  
  52. TestContext.authService.authenticateUser(emailAddress: testEmailAddress, password: testPassword, completionHandler: { (user) in
  53. guard let user = user else { return }
  54. print("test authentication successful.")
  55. print(user)
  56. print("----")
  57. })
  58.  
  59.  
  60. struct AlamofireAuthService: AuthService {
  61. func authenticateUser(emailAddress: String, password: String, completionHandler: (User?) -> Void) {
  62. let params = [
  63. "email_address": emailAddress,
  64. "password": password
  65. ]
  66.  
  67. // TODO: Implement network request
  68. let user = User(firstName: "Nishant", lastName: "Shrestha", phoneNumber: "0478578673", emailAddress: emailAddress)
  69. completionHandler(user)
  70. }
  71. }
  72.  
  73. protocol AlamofireAuthServiceComponent: AuthServiceComponent {}
  74.  
  75. extension AlamofireAuthServiceComponent {
  76. static func createAuthService() -> AuthService {
  77. return AlamofireAuthService()
  78. }
  79. }
  80.  
  81. enum ProductionContext: AlamofireAuthServiceComponent {
  82. public static let authService = ProductionContext.createAuthService()
  83. }
  84.  
  85. ProductionContext.authService.authenticateUser(emailAddress: "nishant.shrestha@hotmail.com", password: "password") { (user) in
  86. if let user = user {
  87. print("production authentication succesful")
  88. print(user)
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement