A simple library for web-based authentication (OAuth, SSO) on iOS. It presents a browser session, waits for the callback and extracts the token automatically.
- iOS 15+
- Swift 5.5+
.package(url: "https://github.com/InQBarna/WebAuthentication.git", from: "0.2.0")pod 'WebAuthentication'let config = AuthConfiguration(
authCallbackURLScheme: "myapp",
authCallbackTokenQueryParamName: "token",
authStatusChangedNotificationName: Notifications.authStatusChanged,
authStatusChangedNotificationInfo: "tokenKey"
)let auth = WebAuthentication(configuration: config)UIKit
auth.display(loginURL, from: viewController) { result in
switch result {
case .success(.token(let token)):
// use token
case .success(.otherCallback(let url)):
// callback received without token
case .failure(let error):
// handle error
}
}SwiftUI — callback
auth.display(loginURL) { result in
switch result {
case .success(.token(let token)): // use token
case .failure(let error): // handle error
}
}SwiftUI — async/await
let result = await auth.display(loginURL)
if case .success(.token(let token)) = result {
// use token
}SwiftUI — View Modifier
struct ContentView: View {
@State private var authURL: URL? = nil
var body: some View {
Button("Login") {
authURL = URL(string: "https://your-service.com/auth")!
}
.webAuthentication(auth, url: $authURL) { result in
switch result {
case .success(.token(let token)): // use token
case .failure(let error): // handle error
}
}
}
}Pass ephemeralWebSession: true to run the login in private mode — no cookies or credentials will be shared with the browser.
let config = AuthConfiguration(
authCallbackURLScheme: "myapp",
authCallbackTokenQueryParamName: "token",
authStatusChangedNotificationName: Notifications.authStatusChanged,
authStatusChangedNotificationInfo: "tokenKey",
ephemeralWebSession: true
)InQBarna, @catchakos, @alexmaxu
WebAuthentication is available under the MIT license. See the LICENSE file for more info.