# SDK for Swift

This is a pure Swift library for Authing (opens new window).

Gihutb Source Code: https://github.com/Authing/SwiftyAuthing (opens new window)

# Requirements

  • Swift 5.0 +
  • Xcode 11 +
  • iOS 10+

# Installation

# Swift Package Manager

  1. Xcode - Project - Swift Packages
  2. Add + https://github.com/Authing/SwiftyAuthing.git
  3. Select the latest version

image

image

image

  1. Update to latest version (optional)

# Example

  1. Clone repository

  2. Change UserPoolId, Secret, and Host in SwiftyAuthingExample - ViewController.swift

  3. Run SwiftyAuthingExample with iOS simulator or device.

  4. Test and check data with web console https://console.authing.cn (opens new window)

# How to use

import SwiftyAuthing

/// Config Information, change your UserPoolId
/// Find in https://console.authing.cn Setting-Basic Information & Application.
let userPoolId = "userPoolId"
let appId = "appId"

var client: AuthenticationClient?
var userid = ""

//Use userPoolId and appId to initialize at the same time
self.client = AuthenticationClient(userPoolId: userPoolId, appId: appId)

// If you call user related methods in this class, you need to set a valid user AccessToken saved by this user here.
// If you need to change the user AccessToken, just assign it again.
//self.client?.accessToken = "accessToken"


/// Register by Email and Password.
/// Register with email
///
func registerByEmail() {
    //Normal
// self.client?.registerByEmail(email: textEmail.text!, password: textPassword.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.registerByEmail ?? "")
// self.userid = status.data?.registerByEmail?.id ?? ""
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.registerByEmailWithResult(email: textEmail.text!, password: textPassword.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            print(graphQLResult.data?.registerByEmail ?? "")
        case .failure(let error):
            print(error)
        }
    })
}

/// Register by Username and Password.
/// Register with username
///
func registerByUsername() {
    //Normal
// self.client?.registerByUsername(username: textUsername.text!, password: textPassword.text!, completion:{status in
// if(status.errors == nil) {
// //Success
// print(status.data?.registerByUsername ?? "")
// self.userid = status.data?.registerByUsername?.id ?? ""
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.registerByUsernameWithResult(username: textUsername.text!, password: textPassword.text!, completion:{result in
        switch result {
        case .success(let graphQLResult):
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}

/// Register by Phone Number and SMS Code.
/// Use mobile phone number to register
///
func registerByPhoneCode() {
    
    //Normal
// self.client?.registerByPhoneCode(phone: textPhone.text!, code: textPhonecode.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.registerByPhoneCode ?? "")
// self.userid = status.data?.registerByPhoneCode?.id ?? ""
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.registerByPhoneCodeWithResult(phone: textPhone.text!, code: textPhonecode.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.registerByPhoneCode ?? "")
                self.userid = status.data?.registerByPhoneCode?.id ?? ""
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
            
        case .failure(let error):
            print(error)
        }
    })
    
}

/// Check Password Strength.
/// Check password strength
///
func checkPasswordStrength() {
    //Normal
// self.client?.checkPasswordStrength(password: textPassword.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.checkPasswordStrength ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.checkPasswordStrengthWithResult(password: textPassword.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.checkPasswordStrength ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }

    })
}

/// Send SMS Code to Phone Number.
/// Send SMS verification code
///
func sendSmsCode() {
    //Normal
// self.client?.sendSmsCode(phone: textPhone.text!, completion: {status in
// print(status)
// })
    
    //With result
    self.client?.sendSmsCodeWithResult(phone: textPhone.text!, completion: {result in
        print(result)
    })
}


/// Login by Email and Password.
/// Login with email
///
func loginByEmail() {
    //Normal
// self.client?.loginByEmail(email: textEmail.text!, password: textPassword.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.loginByEmail ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.loginByEmailWithResult(email: textEmail.text!, password: textPassword.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.loginByEmail ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Login with Username and Password.
/// Login with username
func loginByUsername() {
    //Normal
// self.client?.loginByUsername(username: textUsername.text!, password: textPassword.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.loginByUsername ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.loginByUsernameWithResult(username: textUsername.text!, password: textPassword.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.loginByUsername ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}

/// Login by Phone Number and SMS Code.
/// Use mobile phone number verification code to log in
///
func loginByPhoneCode() {
    //Normal
// self.client?.loginByPhoneCode(phone: textPhone.text!, code: textPhonecode.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.loginByPhoneCode ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.loginByPhoneCodeWithResult(phone: textPhone.text!, code: textPhonecode.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.loginByPhoneCode ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Login by Phone Number and Password.
/// Login with mobile phone number and password
///
func loginByPhonePassword() {
    //Normal
// self.client?.loginByPhonePassword(phone: textPhone.text!, password: textPassword.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.loginByPhonePassword ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.loginByPhonePasswordWithResult(phone: textPhone.text!, password: textPassword.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.loginByPhonePassword ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Check Login Status.
/// Check Token login status
///
func checkLoginStatus() {
    // Change your accessToken here
    let accessToken = self.client?.accessToken ?? ""
    print("accessToken: "+ accessToken)
    
    //Normal
// self.client?.checkLoginStatus(token: accessToken, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.checkLoginStatus ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.checkLoginStatusWithResult(token: accessToken, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.checkLoginStatus ?? "")
            }else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Send Email.
/// send email
///
func sendEmail() {
    //Normal
// self.client?.sendEmail(email: textEmail.text!, scene: EmailScene.resetPassword, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.sendEmail ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.sendEmailWithResult(email: textEmail.text!, scene: EmailScene.resetPassword, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.sendEmail ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}

/// Reset Password by Phone Code.
/// Reset password via SMS verification code
///
func resetPasswordByPhoneCode() {
    //Normal
// self.client?.resetPasswordByPhoneCode(phone: textPhone.text!, code: textPhonecode.text!, newPassword: textPassword.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.resetPassword ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.resetPasswordByPhoneCodeWithResult(phone: textPhone.text!, code: textPhonecode.text!, newPassword: textPassword.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.resetPassword ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}

/// Reset Password by Email Code.
/// Reset password via email verification code
///
func resetPasswordByEmailCode() {
    //Normal
// self.client?.resetPasswordByEmailCode(email: textEmail.text!, code: textPhonecode.text!, newPassword: textPassword.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.resetPassword ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.resetPasswordByEmailCodeWithResult(email: textEmail.text!, code: textPhonecode.text!, newPassword: textPassword.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.resetPassword ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Update Profile.
/// Modify user information
///
func updateProfile() {
    //Normal
// self.client?.updateProfile(nickname: textUsername.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.updateUser ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.updateProfileWithResult(nickname: textUsername.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.updateUser ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Update Password.
/// Update user password
///
func updatePassword() {
    //Normal
// self.client?.updatePassword(newPassword: textPassword.text!,oldPassword: textPassword.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.updatePassword ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.updatePasswordWithResult(newPassword: textPassword.text!,oldPassword: textPassword.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.updatePassword ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Update Phone.
/// Update user phone number
///
func updatePhone() {
    //Normal
// self.client?.updatePhone(phone: textPhone.text!, phoneCode: textPhonecode.text!, oldPhone: textPhone.text!, oldPhoneCode: textPhonecode.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.updatePhone ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.updatePhoneWithResult(phone: textPhone.text!, phoneCode: textPhonecode.text!, oldPhone: textPhone.text!, oldPhoneCode: textPhonecode.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.updatePhone ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Update Email.
/// Update user mailbox
///
func updateEmail() {
    //Normal
// self.client?.updateEmail(email: textEmail.text!, emailCode: textPhonecode.text!, oldEmail: textEmail.text!, oldEmailCode: textPhonecode.text!, completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.updateEmail ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.updateEmailWithResult(email: textEmail.text!, emailCode: textPhonecode.text!, oldEmail: textEmail.text!, oldEmailCode: textPhonecode.text!, completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.updateEmail ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


///Refresh Token.
/// Refresh the current user's token
///
func refreshToken() {
    //Normal
// self.client?.refreshToken(completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.refreshToken ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.refreshTokenWithResult(completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.refreshToken ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Get Current User.
/// Get the information of the currently logged in user
///
func getCurrentUser() {
    //Normal
// self.client?.getCurrentUser(completion: {status in
// if(status.errors == nil) {
// //Success
// print(status.data?.user ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.getCurrentUserWithResult(completion: {result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.user ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Logout Current User.
/// sign out
///
func logout() {
    //Normal
// self.client?.logout(completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.updateUser ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.logoutWithResult(completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.updateUser ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}

/// bindPhone.
/// Bind mobile phone number
///
func bindPhone() {
    //Normal
// self.client?.bindPhone(phone: textPhone.text!, phoneCode: textPhonecode.text!, completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.bindPhone ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.bindPhoneWithResult(phone: textPhone.text!, phoneCode: textPhonecode.text!, completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.bindPhone ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// unbindEmail.
/// Unbind mailbox
///
func unbindEmail() {
    //Normal
// self.client?.unbindEmail(completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.unbindEmail ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.unbindEmailWithResult(completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.unbindEmail ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// bindPhone.
/// Bind mobile phone number
///
func unbindPhone() {
    //Normal
// self.client?.unbindPhone(completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.unbindPhone ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.unbindPhoneWithResult(completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.unbindPhone ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}

/// List Udv.
/// Get the current user's custom data list
///
func listUdv() {
    //Normal
// self.client?.listUdv(completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.udv ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.listUdvWithResult(completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.udv ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// Set Udv.
/// Add custom data
///
func setUdv() {
    //Normal
// self.client?.setUdv(key: "HelloDate", value: Date(), completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.setUdv ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.setUdvWithResult(key: "HelloDate", value: Date(), completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.setUdv ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)case .failure(let error):
            print(error)
        }
    })
}

/// Remove Udv.
/// Delete custom data
///
func removeUdv() {
    //Normal
// self.client?.removeUdv(key: "HelloDate", completion:{ status in
// if(status.errors == nil) {
// //Success
// print(status.data?.removeUdv ?? "")
//} else {
// //Failure
// print(status.errors ?? "")
//}
// })
    
    //With result
    self.client?.removeUdvWithResult(key: "HelloDate", completion:{ result in
        switch result {
        case .success(let graphQLResult):
            let status = graphQLResult
            if(status.errors == nil) {
                //Success
                print(status.data?.removeUdv ?? "")
            } else {
                //Failure
                print(status.errors ?? "")
            }
            print(graphQLResult)
        case .failure(let error):
            print(error)
        }
    })
}


/// listOrg.
/// Get the user's organization
func listOrg() {
    //Normal
// self.client?.listOrg(completion: {status in
// print(status)
// })
    
    //With result
    self.client?.listOrgWithResult(completion: {result in
        print(result)
    })
}

/// loginByLdap.
/// Login with LDAP username
func loginByLdap() {
    //Normal
// self.client?.loginByLdap(username: textUsername.text!, password: textPassword.text!, completion: {status in
// print(status)
// })
    
    //With result
    self.client?.loginByLdapWithResult(username: textUsername.text!, password: textPassword.text!, completion: {result in
        print(result)
    })
}


/// Login by WeChat Code.
/// Log in with WeChat authentication code
///
func loginByWeChatCode() {
    //Login through the authentication code returned by WeChat SDK https://docs.authing.cn/social-login/mobile/wechat.html
    let code = "code"
    //Normal
// self.client?.loginByWeChatCode(code: code, completion: {status in
// print(status)
// })
    
    //With result
    self.client?.loginByWeChatCodeWithResult(code: code, completion: {result in
        print(result)
    })
}


/// User Id Verify.
/// Real-name authentication-use name, ID number, face image, call after login
///
func userIdVerify() {
    //*Call after login
    
    //via faceImageURL
    self.client?.userIdVerify(name: "张三", idCard: "123456", faceImageURL: URL(string: "http://xxx.jpg")!, completion: {result in
        print(result)
    })
    
    //via faceImageBase64, if you use UIImage, you can use getBase64FromImage to convert
// self.client?.userIdVerify(name: "Zhang San", idCard: "123456", faceImageBase64: "data:image/jpeg;base64,/9j/4QFmRXhpZgA", completion: {result in
// print(result)
// })
    
// success msg
// {
// "code": 200,
// "message": "Real-name authentication succeeded"
//}
}

/// Get Base64 from UIImage
/// UIImage to Base64
///
func getBase64FromImage(_ name: String) -> String{
    let imageOrigin = UIImage(named: name)
    if let image = imageOrigin {
        let dataTmp = image.pngData()
        if let data = dataTmp {
            let imageStrTT = data.base64EncodedString()
            return imageStrTT
        }
    }
    return ""
}

/// User Id Verify Status.
/// Query the real-name authentication status, which needs to be called after login
///
func userIdVerifyStatus() {
    //*Call after login
    self.client?.userIdVerifyStatus(completion: {result in
        print(result)
    })
    
// success msg
// {
// "code": 200,
// "data": true,
// "message": "Query the real-name authentication status successfully"
//}
}