Integrate with Firebase
Introduction
This guide streamlines the integration of Scalekit with Firebase applications, enabling seamless single sign-on (SSO) authentication for your users. We'll walk you through configuring your Firebase project with Scalekit and provide sample code to manage Scalekit SSO within your app's Firebase environment.
Scalekit as your OpenID Connect provider for seamless Firebase integration
Before you begin
- Ensure access to Firebase authentication with identity platform. OpenID Connect is not available in legacy Firebase authentication. See the product comparison for details.
- Access to your Scalekit account
Set up Firebase
Add Scalekit as an OpenID Connect provider
- Log in to the Firebase management console and select your project.
- Go to Authentication > Sign-in method.
- Click Add new provider and select OpenID Connect.
- Choose Code flow for the Grant type and set the Name to "Scalekit".
- Retrieve the client ID, issuer URL, and client secret from your Scalekit API config.
Retrieve configuration details from Scalekit
- Log in to your Scalekit dashboard and navigate to Settings > API config.
- Locate the following fields in your Scalekit API config:
- Client ID
- Environment URL
- Client secret (generate a new one if needed)
- Copy the client ID from Scalekit and paste it into the Client ID field in your Firebase project's OIDC provider settings.
- Copy the environment URL from Scalekit and paste it into the Issuer URL field in your Firebase project's OIDC provider settings.
- Copy the newly generated client secret from Scalekit and paste it into the Client secret field in your Firebase project's OIDC provider settings. After filling in these details, your Firebase project's OIDC provider settings should look similar to this:
Add redirect URI in Scalekit API config
After defining new OIDC provider, you would need to configure OIDC integration in your Firebase project. Copy the callback URL from your Firebase project and add it as a new redirect URI in your Scalekit API config
In your Firebase project, copy the callback URL under "Configure OIDC integration"
Add this URL as a new redirect URI in your Scalekit API config.
Handle identity provider-initiated single sign-on
In the current setup, Firebase acts as an OpenID Connect (OIDC) provider for Scalekit. When a user signs in with Firebase, their information is sent to Scalekit. Scalekit then redirects the user to authenticate with their identity provider and sends the user information back to your app.
Users can access your app directly from their identity provider via single sign-on (SSO). To handle these SSO requests, create a dedicated endpoint in your app. This endpoint will complete the authentication flow when users initiate SSO from their identity provider. For detailed instructions on implementing the IdP-initiated SSO flow, refer to Implement IdP-initiated SSO guide. This guide walks you through the necessary steps to set up and handle SSO requests originating from the identity provider.
Enable single sign-on in your login page
Modify your app's login page to invoke SSO and send the request to Scalekit with user details.
// Initialize Scalekit as an OpenID Connect (OIDC) provider
const scalekitProvider = new OAuthProvider('oidc.scalekit');
// Set custom parameters for SSO authentication
// You can use 'domain', 'organization_id', or 'connection_id' based on your setup
scalekitProvider.setCustomParameters({
domain: 'customer@megasoft.org',
});
// Handle the SSO login button click event
loginbtn.onclick = () => {
// Initiate the sign-in process with a popup
signInWithPopup(auth, scalekitProvider)
.then(result => {
// Extract the OAuth credential from the authentication result
const credential = OAuthProvider.credentialFromResult(result);
// Get the authenticated user's information
const user = result.user.providerData[0];
// You can now access user details such as:
// user.email, user.displayName, user.uid, etc.
// Perform any additional actions with the user data here
})
.catch(error => {
// Handle any errors that occur during the sign-in process
console.error('Authentication error:', error);
// Implement appropriate error handling and user feedback
});
};
By following these steps, you can seamlessly integrate Scalekit's social login into your Firebase application, employ single sign-on authentication seamlessly.