-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAuth0Manager.swift
51 lines (44 loc) · 1.28 KB
/
Auth0Manager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Foundation
import Auth0
enum Auth0ManagerError: LocalizedError {
case noCredential
}
struct Auth0Manager {
private let credentialsManager = CredentialsManager(authentication: Auth0.authentication())
}
extension Auth0Manager {
public var hasCredentials: Bool {
return credentialsManager.canRenew() || credentialsManager.hasValid()
}
}
extension Auth0Manager {
func login() async throws -> Credentials {
do {
let credentials = try await Auth0.webAuth()
.audience("your-auth0-audience")
.start()
_ = credentialsManager.store(credentials: credentials)
return credentials
} catch {
print("Failed with: \(error)")
throw error
}
}
func clear() async {
do {
try await Auth0.webAuth().clearSession()
try await credentialsManager.revoke()
print("Logged out")
} catch {
print("Failed with: \(error)")
}
}
func getCredentials() async throws -> Credentials {
if hasCredentials {
let credentials = try await credentialsManager.credentials()
return credentials
} else {
throw Auth0ManagerError.noCredential
}
}
}