Skip to content

A simple implementation of OAuth2 in Swift using Combine and AuthenticationServices (macOS and iOS).

License

Notifications You must be signed in to change notification settings

hadiidbouk/SimpleOAuth2

Repository files navigation

SimpleOAuth2

workflow badge version badge platform compatibility

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

@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).

About

A simple implementation of OAuth2 in Swift using Combine and AuthenticationServices (macOS and iOS).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages