https://github.com/hadiidbouk/simpleoauth2
A simple implementation of OAuth2 in Swift using Combine and AuthenticationServices (macOS and iOS).
https://github.com/hadiidbouk/simpleoauth2
authenticationservices combine oauth2 swift
Last synced: 3 months ago
JSON representation
A simple implementation of OAuth2 in Swift using Combine and AuthenticationServices (macOS and iOS).
- Host: GitHub
- URL: https://github.com/hadiidbouk/simpleoauth2
- Owner: hadiidbouk
- License: mit
- Created: 2020-12-04T09:13:25.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-08T19:44:53.000Z (over 4 years ago)
- Last Synced: 2025-04-23T08:06:20.322Z (3 months ago)
- Topics: authenticationservices, combine, oauth2, swift
- Language: Swift
- Homepage:
- Size: 52.7 KB
- Stars: 30
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleOAuth2


A simple implementation of OAuth2 in Swift using Combine and AuthenticationServices.
The goal of this package is to make it as simple as possible :)
## Installation
You can add SimpleOAuth2 to an Xcode project by adding it as a package dependency.
1. From the **File** menu, select **Swift Packages › Add Package Dependency…**
2. Enter "https://github.com/hadiidbouk/SimpleOAuth2" into the package repository URL text field## Usage
You need to pass your client credentials to the signIn method using `OAuth2Request`
```swift
@State var cancellable: AnyCancellable?
let request: OAuth2Request = .init(authUrl: "",
tokenUrl: "",
clientId: "",
redirectUri: "",
clientSecret: "",
scopes: [])
Button(action: {
cancellable = auth.signIn(with: request)
.sink( receiveCompletion: { result in
switch result {
case .failure(let error):
print(error.localizedDescription)
default: break
}
}, receiveValue: { credentials in
credentials.save()
print(credentials)
})
}) {
Text("Login with GitLab")
}
```You can also use `save()` function to save the `OAuth2Credentials` instance to your `UserDefaults` and load the credentials using this static function `OAuth2Credentials.load()`.
## About this package
- It will never be published on Cocoapods neither on Carthage.
- Only Authorization Code flow supported since it's the recommended flow for native mobile apps.## Coming soon
- Adding refresh function for refreshing your token (if needed).