If your projects require user authentication and token management—handling token validation, authentication state, and secure storage—izziSession simplifies the process. It automates token storage, expiration checks, and refreshes tokens when needed, not just on app launch but also during authorized requests, ensuring seamless authentication with less effort.
- Save Tokens in Keychain.
- Retrieve Tokens from Keychain.
- Delete Tokens from Keychain.
- Easily Verify Token Validity: Check if the token is still valid with a simple method.
- Default Token Models or Custom Codable Models.
| Parameter | Key |
|---|---|
accessTokenKey |
izzi.session.accessToken |
refreshTokenKey |
izzi.session.refreshToken |
| Parameter | Key | Description | Default Value |
|---|---|---|---|
apiEndpoint |
string |
Required. API endpoint. | N/A |
customRequestBuilder |
((String) -> RequestModel) |
Optional. A closure that builds a custom request model using the refreshToken. |
DefaultRefreshRequestModel |
tokenExtractor |
((ResponseModel) -> String) |
Required. A closure that extracts the token (e.g., accessToken) from the API response. |
DefaultTokenResponseModel |
First, inject izziSessionManager into your project
final class MyProject {
private let izziSession: IzziSessionManager
init(izziSession: IzziSessionManager = IzziSessionManager()) {
self.izziSession = izziSession
}
}Then, save the returned tokens from the response in Keychain for future use
do {
let response: MyResponseModel = // API call to log in the user
try izziSession.saveTokensToKeychain(accessToken: response.access, refreshToken: response.refresh)
} catch {
print(error)
}After successfully logging in and securely saving the tokens, on the app’s next launch, we can check token validity in RouterManager (or anywhere else) to decide where to navigate the user.
You can do this with a single line of code:
let api = "https://test.com/check_token"
do {
try await izziSession.verifyTokenValidity(apiEndpoint: api)
// Navigate user to the main screen
} catch {
print(error)
// Navigate user to the login screen
}With the code above, we check token validity using the default request and response models, which are structured as follows:
struct DefaultRequestModel: Codable {
let refresh: String
}
struct DefaultRsponseModel: Codable {
let access: String
}If your API only sends and receives tokens, you can freely use these default models and simply call: izziSession.verifyTokenValidity(apiEndpoint: api)
If your API requires additional information—such as a device ID or other parameters—along with the token, you need to send a custom request model and handle a custom response model.
struct CustomRequestModel: Codable {
let refreshToken: String
let clientId: String
}
struct CustomResponseModel: Codable {
let accessToken: String
let deviceId: String
let appVersion: String
}
-------
let api = "https://test.com/check_token"
do {
try await izziSession.verifyTokenValidity(
apiEndpoint: api,
customRequestBuilder: { refreshToken in
CustomRequestModel(refreshToken: refreshToken, clientId: "client123") // Custom request model
},
tokenExtractor: { (response:CustomResponseModel) in // Custom response model
response.accessToken
}
)
// your code to forward user in main screen
} catch {
print(error)
// forward user in login screen
}To log out a user, you must also delete the tokens stored in the keychain. Use the following izziSession function:
func logOut() {
do {
try izziSession.deleteTokensFromKeychain()
// Your logout logic
} catch {
print("Error during logout: \(error)")
}
}🟢 With the verifyTokenValidity function, it’s also possible to check token validity or obtain a new token during authorized API calls. 🔄 🟢
Additionally, if needed, you can use the following functions:
try izziSession.getAccessToken() // Retrieve only the access token
try izziSession.getRefreshToken() // Retrieve only the refresh token
try izziSession.saveOnlyAccessToken(token: "testToken") // Save only the access token
try izziSession.saveOnlyRefreshToken(token: "testToken") // Save only the refresh token - Open your project.
- Go to File → Add Package Dependencies.
- Enter URL: https://github.com/Desp0o/izziSession.git
- Click Add Package.