Example of simple OAuth2 authentication using Alamofire 5 and RxSwift library

Overview

iOS - Swift - Alamofire 5 - REST Client example

Example of simple OAuth2 authentication using Alamofire 5 and RxSwift library.

Supported grant types:

  • password
  • refresh_token
  • client_credentials

Supported interceptors:

  • Client credentials grant type authentication interceptor
  • Password grant type authentication and refresh token interceptor with request synchronization

Example call

extension Api {
    
    enum OAuth2Service {
                
        static func getUserAccessToken(username: String, password: String) -> AuthorizedEndpoint
    {
            
   return 
   AuthorizedEndpoint(
                
   raw: 
   Endpoint(
                    
   authType: .
   basic,
                    
   method: .
   post,
                    
   path: 
   "oauth/token",
                    
   parameters: [
                        
   "username"
   : username,
                        
   "password"
   : password,
                        
   "grant_type"
   : GrantType.
   password
                    ]
                )
            )
        }
       
   ...
    }
}
  
 
 let client = AuthorizedClient<Scope.Guest>()
 
 client
    .request(Api.OAuth2Service.getUserAccessToken(username: username, password: password))
    .asObservable()
    .subscribe(
        onNext: { accessToken in
            DispatchQueue.main.async {
                // Store access token
            }
        },
        onError: { error in
            DispatchQueue.main.async {
                // Handle api error
                guard let errorResponse = error.asApiError?.response else {  
                    ...
                    return
                }
                ...
            }
        }
    )
    .disposed(by: disposeBag)
You might also like...
A simple way to implement Facebook and Google login in your iOS apps.

Simplicity Simplicity is a simple way to implement Facebook and Google login in your iOS apps. Simplicity can be easily extended to support other exte

A quick and simple way to authenticate an Instagram user in your iPhone or iPad app.
A quick and simple way to authenticate an Instagram user in your iPhone or iPad app.

InstagramSimpleOAuth A quick and simple way to authenticate an Instagram user in your iPhone or iPad app. Adding InstagramSimpleOAuth to your project

A quick and simple way to authenticate a Dropbox user in your iPhone or iPad app.
A quick and simple way to authenticate a Dropbox user in your iPhone or iPad app.

DropboxSimpleOAuth A quick and simple way to authenticate a Dropbox user in your iPhone or iPad app. Adding DropboxSimpleOAuth to your project CocoaPo

A quick and simple way to authenticate a Box user in your iPhone or iPad app.
A quick and simple way to authenticate a Box user in your iPhone or iPad app.

BoxSimpleOAuth A quick and simple way to authenticate a Box user in your iPhone or iPad app. Adding BoxSimpleOAuth to your project CocoaPods CocoaPods

Swift based OAuth library for iOS and macOS
Swift based OAuth library for iOS and macOS

OAuthSwift Swift based OAuth library for iOS and macOS. Support OAuth1.0, OAuth2.0 Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, L

An poc to make a login using Auth0 in Swift

Swift-Auth0-poc This app is an poc to make a login using Auth0. If you want to try it yourself here is a small tutorial on how to do it. 1. Configure

A simple to use, standard interface for authenticating to oauth 2.0 protected endpoints via SFSafariViewController.
A simple to use, standard interface for authenticating to oauth 2.0 protected endpoints via SFSafariViewController.

AuthenticationViewController A simple to use, standard interface for authenticating to OAuth 2.0 protected endpoints via SFSafariViewController. Instr

Easy to use OAuth 2 library for iOS, written in Swift.
Easy to use OAuth 2 library for iOS, written in Swift.

Heimdallr Heimdallr is an OAuth 2.0 client specifically designed for easy usage. It currently supports the resource owner password credentials grant f

Swift based OAuth library for iOS
Swift based OAuth library for iOS

OAuthSwift Swift based OAuth library for iOS and macOS. Support OAuth1.0, OAuth2.0 Twitter, Flickr, Github, Instagram, Foursquare, Fitbit, Withings, L

Owner
Tomáš Smolík
Tomáš Smolík
OAuth2 framework for macOS and iOS, written in Swift.

OAuth2 OAuth2 frameworks for macOS, iOS and tvOS written in Swift 5.0. ⤵️ Installation ?? Usage ?? Sample macOS app (with data loader examples) ?? Tec

Pascal Pfiffner 1.1k Jan 2, 2023
A swift implementation of OAuth2

#AlamofireOauth2 A Swift implementation of OAuth2 for iOS using Alamofire. #Intro This library is heavilly inspired by the SwiftOAuth2 repository from

Edwin Vermeer 83 Dec 5, 2022
A simple project for Face ID Authentication for iOS device using LocalAuthentication Framework

BiometricAuthentication This respository is a simple project for Face ID Authentication for iOS device using LocalAuthentication Framework and infoPli

Lee McCormick 0 Oct 23, 2021
FCLAuthSwift is a Swift library for the Flow Client Library (FCL) that enables Flow wallet authentication on iOS devices.

FCLAuthSwift is a Swift library for the Flow Client Library (FCL) that enables Flow wallet authentication on iOS devices. Demo The demo a

Zed 3 May 2, 2022
A UIViewController subclass for Instagram authentication.

InstagramAuthViewController A ViewController for Instagram authentication. A UIViewController subclass that handles showing the Instagram login page,

Isuru Nanayakkara 38 Jun 29, 2021
Implementing User Authentication with Sign in with Apple

Implementing User Authentication with Sign in with Apple Provide a way for users of your app to set up an account and start using your services. Overv

null 0 Oct 18, 2021
Implementing User Authentication with Sign in with Apple

Implementing User Authentication with Sign in with Apple Provide a way for users of your app to set up an account and start using your services. Overv

Liselot 0 Dec 2, 2021
A simple OAuth library for iOS with a built-in set of providers

SwiftyOAuth is a small OAuth library with a built-in set of providers and a nice API to add your owns. let instagram: Provider = .instagram(clientID:

Damien 477 Oct 15, 2022
A simple library to make authenticating tvOS apps easy via their iOS counterparts.

Voucher The new Apple TV is amazing but the keyboard input leaves a lot to be desired. Instead of making your users type credentials into their TV, yo

Riz 516 Nov 24, 2022
LinkedInSignIn - Simple view controller to log in and retrieve an access token from LinkedIn.

LinkedInSignIn Example To run the example project, clone the repo, and run pod install from the Example directory first. Also you need to setup app on

Serhii Londar 34 Sep 1, 2022