Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.58 KB | None | 0 0
  1. import Foundation
  2. import SwiftKeychainWrapper
  3. import SwiftyJSON
  4.  
  5. class Session {
  6.     var curUser: User?
  7.    
  8.     init() {
  9.         if let accessToken = KeychainWrapper.standard.string(forKey: ACCESS_TOKEN) {
  10.             EndpointManager.authenticatUser(accessToken: accessToken)
  11.         }
  12.     }
  13.    
  14.     func login(email: String, password: String, responseDelegate: ResponseDelegate) {
  15.         let parameters: Dictionary<String, Any> = [
  16.             "user[email]": email,
  17.             "user[password]": password
  18.         ]
  19.        
  20.         EndpointManager.sendRequest(with: parameters, request: .login) {
  21.             [weak self] data in
  22.            
  23.             if let errors = data.dictionary?["errors"] {
  24.                 responseDelegate.onFail(error: Error(data: errors))
  25.             } else if let user = data.dictionary?["user"] {
  26.                 self?.curUser = User(data: user)
  27.                 self?.persistUser(accessToken: data["user"]["access_token"].stringValue)
  28.                 responseDelegate.onSuccess()
  29.             } else {
  30.                 responseDelegate.onFail(error: Error(data:
  31.                     ["Unkown data received from api server"]))
  32.             }
  33.         }
  34.     }
  35.    
  36.     func signup(name: String, email: String, password: String, responseDelegate: ResponseDelegate) {
  37.         let parameters: Dictionary<String, Any> = [
  38.             "user[name]": name,
  39.             "user[email]": email,
  40.             "user[password]": password
  41.         ]
  42.        
  43.         EndpointManager.sendRequest(with: parameters, request: .signup) {
  44.             [weak self] data in
  45.            
  46.             if let errors = data.dictionary?["errors"] {
  47.                 responseDelegate.onFail(error: Error(data: errors))
  48.             } else if let user = data.dictionary?["user"] {
  49.                 self?.curUser = User(data: user)
  50.                 self?.persistUser(accessToken: data["user"]["access_token"].stringValue)
  51.                 responseDelegate.onSuccess()
  52.             } else {
  53.                 responseDelegate.onFail(error: Error(data:
  54.                     ["Unkown data received from api server"]))
  55.             }
  56.         }
  57.     }
  58.    
  59.     func logOut() {
  60.         removeUser()
  61.     }
  62.    
  63.     func isLoggedIn() -> Bool {
  64.         return KeychainWrapper.standard.hasValue(forKey: ACCESS_TOKEN)
  65.     }
  66.    
  67.     func getUserData(getCached: Bool, responseDelegate: ResponseDelegate) {
  68.         if !getCached || curUser == nil {
  69.             EndpointManager.sendRequest(with: nil, request: .get_user) {
  70.                 [weak self] data in
  71.                
  72.                 if let errors = data.dictionary?["errors"] {
  73.                     responseDelegate.onFail(error: Error(data: errors))
  74.                 } else if let user = data.dictionary?["user"] {
  75.                     self?.curUser = User(data: user)
  76.                     responseDelegate.onSuccess()
  77.                 } else {
  78.                     responseDelegate.onFail(error: Error(data: "Unkown data received from api server"))
  79.                 }
  80.             }
  81.         } else {
  82.             responseDelegate.onSuccess()
  83.         }
  84.     }
  85.    
  86.     // Mark: - save user access_token to keychain
  87.     private func persistUser(accessToken: String) {
  88.         KeychainWrapper.standard.set(accessToken, forKey: ACCESS_TOKEN)
  89.         EndpointManager.authenticatUser(accessToken: accessToken)
  90.     }
  91.    
  92.     // Mark: - remove user access_token from keychain
  93.     private func removeUser() {
  94.         KeychainWrapper.standard.removeObject(forKey: ACCESS_TOKEN)
  95.         EndpointManager.deauthenticatUser()
  96.         curUser = nil
  97.     }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement