Skip to content

Latest commit

 

History

History
114 lines (81 loc) · 3.12 KB

File metadata and controls

114 lines (81 loc) · 3.12 KB

CredentialAuth - Swift Package

CredentialAuth is a Swift package that provides a simple way to handle authentication and token management.

Features

Installation

Requirement

  • iOS 13 and above
  • Swift 5.7 and above

Swift Package Manager

To install CredentialAuth using Swift Package Manager, add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/sun-asterisk/tech-standard-ios-auth", from: "1.0.0")
]

Select CredentialAuth package, or CredentialAuthRx package if you use RxSwift.

Usage

Prerequisites

Set a delegate for CredentialAuth:

import CredentialAuth

final class AppDelegate: NSObject, UIApplicationDelegate {
    
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        // Credential
        CredentialAuth.shared.delegate = self
        
        return true
    }
}

extension AppDelegate: CredentialAuthDelegate {
    func login(credential: [String : Any],
               success: @escaping (Token, User?) -> Void,
               failure: @escaping (Error) -> Void) {

    }
    
    func logout(credential: [String : Any]?, success: (() -> Void)?, failure: ((Error) -> Void)?) {

    }
    
    func refreshToken(refreshToken: String, success: @escaping (Token) -> Void, failure: @escaping (Error) -> Void) {

    }
}

Example

Here's an example of how to use the CredentialAuth package:

import CredentialAuth

// initialize CredentialAuth
let auth = CredentialAuth.shared

// Login the user
auth.login(credential: ["username": "user", "password": "pass"]) { (result) in
    switch result {
    case .success:
        print("Login successful")
    case .failure(let error):
        print("Login failed with error: \(error)")
    }
}

// Get the token
if let token = auth.getToken() {
    print("Token: \(token)")
}

// Get the authenticated user
if let user = auth.getUser() {
    print("User: \(user)")
}

// Logout the user
auth.logout() { (result) in
    switch result {
    case .success:
        print("Logout successful")
    case .failure(let error):
        print("Logout failed with error: \(error)")
    }
}

Documentation

You can see the full documentation here.