Authenticate Using GitHub on Apple Platforms Stay organized with collections Save and categorize content based on your preferences.
You can let your users authenticate with Firebase using OAuth providers such asGitHub by integrating generic OAuth Login into your app using the Firebase SDK tocarry out the end to end sign-in flow.
Before you begin
To sign in users using GitHub accounts, you must first enable GitHub as a sign-inprovider for your Firebase project:
Use Swift Package Manager to install and manage Firebase dependencies.
Visitour installation guide to learn about the different ways you can add Firebase SDKs to your Apple project.- In Xcode, with your app project open, navigate toFile > Add Packages.
- When prompted, add the Firebase Apple platforms SDK repository:
- Choose theFirebase Authentication library.
- Add the
-ObjCflag to theOther Linker Flags section of your target's build settings. - When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
https://github.com/firebase/firebase-ios-sdk.git
Now, perform some configuration steps:
- In theFirebase console, open theAuth section.
- On theSign in method tab, enable theGitHub provider.
- Add theClient ID andClient Secret from that provider's developer console to the provider configuration:
- Register your app as a developer application on GitHub and get your app's OAuth 2.0Client ID andClient Secret.
- Make sure your FirebaseOAuth redirect URI (e.g.
my-app-12345.firebaseapp.com/__/auth/handler) is set as yourAuthorization callback URL in your app's settings page on yourGitHub app's config.
- ClickSave.
Handle the sign-in flow with the Firebase SDK
To handle the sign-in flow with the Firebase Apple platforms SDK, follow these steps:
Add custom URL schemes to your Xcode project:
- Open your project configuration: double-click the project name in the left tree view. Select your app from theTARGETS section, then select theInfo tab, and expand theURL Types section.
- Click the+ button, and add your Encoded App ID as a URL scheme. You can find your Encoded App ID on theGeneral Settings page of the Firebase console, in the section for your iOS app. Leave the other fields blank.
When completed, your config should look something similar to the following (but with your application-specific values):

Create an instance of anOAuthProvider using the provider IDgithub.com.
Swift
varprovider=OAuthProvider(providerID:"github.com")
Objective-C
FIROAuthProvider*provider=[FIROAuthProviderproviderWithProviderID:@"github.com"];
Optional: Specify additional custom OAuth parameters that you want tosend with the OAuth request.
Swift
provider.customParameters=["allow_signup":"false"]
Objective-C
[providersetCustomParameters:@{@"allow_signup":@"false"}];
For the parameters GitHub supports, see theGitHub OAuth documentation.Note that you can't pass Firebase-required parameters with
setCustomParameters. These parameters areclient_id,redirect_uri,response_type,scope andstate.Optional: Specify additional OAuth 2.0 scopes beyond basic profile thatyou want to request from the authentication provider. If your applicationrequires access to private user data from GitHub APIs, you'll need torequest permissions to access GitHub APIs underAPI Permissions in theGitHub developer console. Requested OAuth scopes must be exact matches tothe preconfigured ones in the app's API permissions.
Swift
// Request read access to a user's email addresses.// This must be preconfigured in the app's API permissions.provider.scopes=["user:email"]
Objective-C
// Request read access to a user's email addresses.// This must be preconfigured in the app's API permissions.[providersetScopes:@[@"user:email"]];
To learn more, refer to theGitHub scopes documentation.
Optional: If you want to customize the way your app presents the
SFSafariViewControllerorUIWebViewwhendisplaying the reCAPTCHA to the user, create a custom class that conformsto theAuthUIDelegateprotocol, and pass it tocredentialWithUIDelegate.Authenticate with Firebase using the OAuth provider object.
Swift
provider.getCredentialWith(nil){credential,erroriniferror!=nil{// Handle error.}ifcredential!=nil{Auth().signIn(with:credential){authResult,erroriniferror!=nil{// Handle error.}// User is signed in.// IdP data available in authResult.additionalUserInfo.profile.guardletoauthCredential=authResult.credentialas?OAuthCredentialelse{return}// GitHub OAuth access token can also be retrieved by:// oauthCredential.accessToken// GitHub OAuth ID token can be retrieved by calling:// oauthCredential.idToken}}}
Objective-C
[providergetCredentialWithUIDelegate:nilcompletion:^(FIRAuthCredential*_Nullablecredential,NSError*_Nullableerror){if(error){// Handle error.}if(credential){[[FIRAuthauth]signInWithCredential:credentialcompletion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){if(error){// Handle error.}// User is signed in.// IdP data available in authResult.additionalUserInfo.profile.FIROAuthCredential*oauthCredential=(FIROAuthCredential*)authResult.credential;// GitHub OAuth access token can also be retrieved by:// oauthCredential.accessToken// GitHub OAuth ID token can be retrieved by calling:// oauthCredential.idToken}];}}];
Using the OAuth access token, you can call theGitHub API.
For example, to get basic profile information, you can call the REST API,passing the access token in the
Authorizationheader:https://api.github.com/user
While the above examples focus on sign-in flows, you also have theability to link a GitHub provider to an existing user. For example, you canlink multiple providers to the same user allowing them to sign in with either.
Swift
Auth().currentUser.link(withCredential:credential){authResult,erroriniferror!=nil{// Handle error.}// GitHub credential is linked to the current user.// IdP data available in authResult.additionalUserInfo.profile.// GitHub OAuth access token can also be retrieved by:// (authResult.credential as? OAuthCredential)?.accessToken// GitHub OAuth ID token can be retrieved by calling:// (authResult.credential as? OAuthCredential)?.idToken}
Objective-C
[[FIRAuthauth].currentUserlinkWithCredential:credentialcompletion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){if(error){// Handle error.}// GitHub credential is linked to the current user.// IdP data available in authResult.additionalUserInfo.profile.// GitHub OAuth access token is can also be retrieved by:// ((FIROAuthCredential *)authResult.credential).accessToken// GitHub OAuth ID token can be retrieved by calling:// ((FIROAuthCredential *)authResult.credential).idToken}];
The same pattern can be used with
reauthenticateWithCredentialwhich canbe used to retrieve fresh credentials for sensitive operations that requirerecent login.Swift
Auth().currentUser.reauthenticateWithCredential(withCredential:credential){authResult,erroriniferror!=nil{// Handle error.}// User is re-authenticated with fresh tokens minted and// should be able to perform sensitive operations like account// deletion and email or password update.// IdP data available in result.additionalUserInfo.profile.// Additional OAuth access token is can also be retrieved by:// (authResult.credential as? OAuthCredential)?.accessToken// GitHub OAuth ID token can be retrieved by calling:// (authResult.credential as? OAuthCredential)?.idToken}
Objective-C
[[FIRAuthauth].currentUserreauthenticateWithCredential:credentialcompletion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){if(error){// Handle error.}// User is re-authenticated with fresh tokens minted and// should be able to perform sensitive operations like account// deletion and email or password update.// IdP data available in result.additionalUserInfo.profile.// Additional OAuth access token is can also be retrieved by:// ((FIROAuthCredential *)authResult.credential).accessToken// GitHub OAuth ID token can be retrieved by calling:// ((FIROAuthCredential *)authResult.credential).idToken}];
Handling account-exists-with-different-credential Errors
If you enabled theOne account per email address setting in theFirebase console,when a user tries to sign in a to a provider (such as GitHub) with an email that alreadyexists for another Firebase user's provider (such as Google), the errorFIRAuthErrorCodeAccountExistsWithDifferentCredential is thrown along with a temporaryFIRAuthCredential object (GitHub credential). To complete the sign in to theintended provider, the user has to sign first to the existing provider (Google) and then link to theformerFIRAuthCredential (GitHub credential). This would look as illustrated below:
Swift
// Sign-in with an OAuth credential.provider.getCredentialWith(nil){credential,errorin// An account with the same email already exists.if(errorasNSError?)?.code==AuthErrorCode.accountExistsWithDifferentCredential.rawValue{// Get pending credential and email of existing account.letexistingAcctEmail=(error!asNSError).userInfo[AuthErrorUserInfoEmailKey]as!StringletpendingCred=(error!asNSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey]as!AuthCredential// Lookup existing account identifier by the email.Auth.auth().fetchProviders(forEmail:existingAcctEmail){providers,errorin// Existing email/password account.if(providers?.contains(EmailAuthProviderID))!{// Existing password account for email. Ask user to provide the password of the// existing account.// Sign in with existing account.Auth.auth().signIn(withEmail:existingAcctEmail,password:password){user,errorin// Successfully signed in.ifuser!=nil{// Link pending credential to account.Auth.auth().currentUser?.linkAndRetrieveData(with:pendingCred){result,errorin// ...}}}}}return}// Other errors.iferror!=nil{// handle the error.return}// Sign in with the credential.ifcredential!=nil{Auth.auth().signInAndRetrieveData(with:credential!){result,erroriniferror!=nil{// handle the error.return}}}}
Objective-C
// Sign-in with an OAuth credential.[providergetCredentialWithUIDelegate:nilcompletion:^(FIRAuthCredential*_Nullablecredential,NSError*_Nullableerror){// An account with the same email already exists.if(error.code==FIRAuthErrorCodeAccountExistsWithDifferentCredential){// Get pending credential and email of existing account.NSString*existingAcctEmail=error.userInfo[FIRAuthErrorUserInfoEmailKey];FIRAuthCredential*pendingCred=error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey];// Lookup existing account identifier by the email.[[FIRAuthauth]fetchProvidersForEmail:existingAcctEmailcompletion:^(NSArray<NSString*>*_Nullableproviders,NSError*_Nullableerror){// Existing email/password account.if([providerscontainsObject:FIREmailAuthProviderID]){// Existing password account for email. Ask user to provide the password of the// existing account.// Sign in with existing account.[[FIRAuthauth]signInWithEmail:existingAcctEmailpassword:passwordcompletion:^(FIRUser*user,NSError*error){// Successfully signed in.if(user){// Link pending credential to account.[[FIRAuthauth].currentUserlinkWithCredential:pendingCredcompletion:^(FIRUser*_Nullableuser,NSError*_Nullableerror){// ...}];}}];}}];return;}// Other errors.if(error){// handle the error.return;}// Sign in with the credential.if(credential){[[FIRAuthauth]signInAndRetrieveDataWithCredential:credentialcompletion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){if(error){// handle the error.return;}}];}}];
Next steps
After a user signs in for the first time, a new user account is created andlinked to the credentials—that is, the user name and password, phonenumber, or auth provider information—the user signed in with. This newaccount is stored as part of your Firebase project, and can be used to identifya user across every app in your project, regardless of how the user signs in.
In your apps, you can get the user's basic profile information from the
Userobject. SeeManage Users.In yourFirebase Realtime Database andCloud StorageSecurity Rules, you can get the signed-in user's unique user ID from the
authvariable, and use it to control what data a user can access.
You can allow users to sign in to your app using multiple authenticationproviders bylinking auth provider credentials to anexisting user account.
To sign out a user, callsignOut:.
Swift
letfirebaseAuth=Auth.auth()do{tryfirebaseAuth.signOut()}catchletsignOutErrorasNSError{print("Error signing out: %@",signOutError)}
Objective-C
NSError*signOutError;BOOLstatus=[[FIRAuthauth]signOut:&signOutError];if(!status){NSLog(@"Error signing out: %@",signOutError);return;}
You may also want to add error handling code for the full range of authentication errors. SeeHandle Errors.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.