You can let your users authenticate with Firebase using their GitHub accounts by integrating GitHub authentication into your app.
Authenticate users can access user-restricted data in Firebase Realtime Database and Firebase Storage.
Before you begin
- Add Firebase to your iOS project. Include the
following pods in your
Podfile
:pod 'Firebase/Auth'
- Register your app as a developer application on GitHub and get your app's OAuth 2.0 Client ID and Client Secret.
- Enable GitHub authentication:
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the GitHub sign-in method and specify the OAuth 2.0 Client ID and Client Secret you got from GitHub.
- Then, make sure your Firebase OAuth redirect URI (e.g.
my-app-12345.firebaseapp.com/__/auth/handler
) is set as your Authorization callback URL in your app's settings page on your GitHub app's config.
Authenticate with Firebase
- Integrate GitHub authentication into your app by following the developer's documentation. Use the web application flow, and set up a custom URL scheme in your app to handle the OAuth 2.0 callback from GitHub. At the end of the GitHub sign-in flow, you will receive an OAuth 2.0 access token.
- Import the Firebase module:
Objective-C
@import Firebase;
Swift
import Firebase
- Configure a
FIRApp
shared instance, typically in your application'sapplication:didFinishLaunchingWithOptions:
method:Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
Swift
// Use Firebase library to configure APIs FIRApp.configure()
- After a user successfully signs in with GitHub, in your implementation of
didCompleteWithResult:error:
, exchange the OAuth 2.0 access token from GitHub for a Firebase credential:Objective-C
FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:accessToken];
Swift
let credential = FIRTwitterAuthProvider.credentialWithToken(accessToken)
- Finally, authenticate with Firebase using the Firebase credential:
Objective-C
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) { // ... }];
Swift
FIRAuth.auth()?.signIn(with: credential) { (user, error) in // ... }
Next steps
After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a 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
FIRUser
object. See Manage Users. In your Firebase Realtime Database and Firebase Storage Security Rules, you can get the signed-in user's unique user ID from the
auth
variable, and use it to control what data a user can access.
You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account.
To sign out a user, call
signOut:
.
Objective-C
NSError *error; [[FIRAuth auth] signOut:&error;]; if (!error) { // Sign-out succeeded }
Swift
try! FIRAuth.auth()!.signOut()
You may also want to add error handling code for the full range of authentication errors. See Handle Errors.