You're viewing the legacy docs. They are deprecated as of May 18, 2016.
These docs are for version 2.4.2 and below of the Javascript SDK. Go to our current docs, or see our Web migration guide.

JavaScript Web Guide

GitHub Authentication

Configuring Your Application

To get started with GitHub authentication, you need to first create a new GitHub application. Click the Register new application button at the top right of that page and fill in a name, description, and website for your application. Set the Authorization callback URL to https://auth.firebase.com/v2/<YOUR-FIREBASE-APP>/auth/github/callback so that GitHub's OAuth service can properly communicate with Firebase's authentication server.

After configuring your GitHub application, head on over to the Login & Auth section in your App Dashboard. Enable GitHub authentication and then copy your GitHub application credentials (Client ID and Client Secret) into the appropriate inputs. You can find your GitHub application's client ID and secret at the top of the application's GitHub dashboard.

Logging Users In

If your user does not have an existing session, you can prompt the user to login and then invoke the GitHub login popup (e.g. after they have clicked a "Login" button) with the following snippet:

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithOAuthPopup("github", function(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Authenticated successfully with payload:", authData);
  }
});

Alternatively, you may prompt the user to login with a full browser redirect, and Firebase will automatically restore the session when you return to the originating page:

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithOAuthRedirect("github", function(error) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    // We'll never get here, as the page will redirect on success.
  }
});

Optional Settings

authWithOAuthPopup() and authWithOAuthRedirect() take an optional third parameter which is an object containing any of the following settings:

Name Description Type
remember If not specified - or set to default - sessions are persisted for as long as you have configured in the Login & Auth tab of your App Dashboard. To limit persistence to the lifetime of the current window, set this to sessionOnly. A value of none will not persist authentication data at all and will end authentication as soon as the page is closed. String
scope A comma-delimited list of requested extended permissions. See http://developer.github.com/v3/oauth/#scopes for more information. String

Here is an example of GitHub login where the session will expire upon browser shutdown and we also request some extended permissions:

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithOAuthPopup("github", function(error, authData) { /* Your Code */ }, {
  remember: "sessionOnly",
  scope: "user,gist"
});

The authData object returned to your callback contains the following fields:

authData Object
Field Description Type
uid A unique user ID, intended as the user's unique key across all providers. String
provider The authentication method used, in this case: github. String
token The Firebase authentication token for this session. String
auth The contents of the authentication token, which will be available as the auth variable within your Security and Firebase Rules. Object
expires A timestamp, in seconds since the UNIX epoch, indicating when the authentication token expires. Number
github An object containing provider-specific data. Object
github.id The GitHub user's ID. String
github.accessToken The GitHub OAuth 2.0 access token granted by GitHub during user authentication. String
github.displayName The GitHub user's full name. String
github.email The GitHub user's primary, verified email address as listed on their profile. Returned only if a primary, verified email address is available, and the GitHub user:email scope was granted by the user. String
github.username The GitHub user's username. String
github.profileImageURL The URL of the Github user's profile picture. String
github.cachedUserProfile The GitHub user's raw profile, as specified by GitHub's user documentation. Note that the data included in this payload is generated by GitHub and may be changed by them at any time. Object

Security and Firebase Rules

Now that the client is logged in, your Security and Firebase Rules have access to their verified GitHub User id. The auth variable contains the following values:

auth Variable
Field Description Type
uid A unique user ID, intended as the user's unique key across all providers. String
provider The authentication method used, in this case: github. String

Here is an example of how to use the auth variable in your Security and Firebase Rules:

{
  "rules": {
    "users": {
      "$uid": {
        // grants write access to the owner of this user account whose uid must exactly match the key ($uid)
        ".write": "auth !== null && auth.uid === $uid",

        // grants read access to any user who is logged in with GitHub
        ".read": "auth !== null && auth.provider === 'github'"
      }
    }
  }
}
See the User Authentication and User Based Security articles for more details.
  1. 1

    Next

    Installation & Setup

  2. 2

    Next

    Understanding Data

  3. 3

    Next

    Saving Data

  4. 4

    Next

    Retrieving Data

  5. 5

    Next

    Structuring Data

  6. 6

    Next

    Understanding Security

  7. 7

    Next

    User Authentication

  8. 8

    Next

    Offline Capabilities

  9. 9

    Next

    Deploying Your App