Getting started with social connections
Single sign-on (SSO) enhances B2B SaaS application security and user experience by enabling secure authentication through an organization’s identity provider (IdP). Some organizations use social identity providers like Google, Microsoft, GitHub, Salesforce etc.
This guide shows you how to add social login functionality to your B2B SaaS application using Scalekit. You’ll learn how to enable users to authenticate with popular identity providers like Google, Microsoft, GitHub, and more through OAuth 2.0.
-
Set up your environment
Section titled “Set up your environment”Before implementing social login, ensure you have the necessary tools and access:
- Access to your Scalekit account and API credentials
- Your application’s codebase for implementing and testing changes
npm install @scalekit-sdk/nodepip install scalekit-sdk-pythongo get -u github.com/scalekit-inc/scalekit-sdk-go/* Gradle users - add the following to your dependencies in build file */implementation "com.scalekit:scalekit-sdk-java:1.1.3"<!-- Maven users - add the following to your `pom.xml` --><dependency><groupId>com.scalekit</groupId><artifactId>scalekit-sdk-java</artifactId><version>1.1.3</version></dependency>You’re now ready to integrate social connections into your application! The next step covers how to use the SDK to authenticate users.
-
Create authorization URL
Section titled “Create authorization URL”To start the social login process, your application must redirect users to Scalekit’s authorization endpoint with the appropriate social provider details.
Construct the authorization URL using these required parameters:
Parameter Description redirect_uri
The URL where Scalekit sends users after successful authentication. After users complete authentication with the social provider, Scalekit redirects them to this URI with a one-time authorization code
. You’ll use this code in the next step to fetch user details.client_id
Your application’s unique Scalekit identifier. This parameter is mandatory for all requests and uniquely identifies both your application and environment (staging, production). provider
Specifies which social provider to use. Set this to the unique code of your chosen provider (e.g., google
,microsoft
).Example authorization URL https://auth.scalekit.com/authorize?client_id=skc_122056050118122349527&redirect_uri=https://yourapp.com/auth/callback&provider=googleNext, construct your authorization URL with these parameters and redirect users to this URL when they attempt to log in.
import { Scalekit } from '@scalekit-sdk/node';// Initialize the SDK clientconst scalekit = new Scalekit('<SCALEKIT_ENVIRONMENT_URL>', '<SCALEKIT_CLIENT_ID>', '<SCALEKIT_CLIENT_SECRET>');const authorizationURL = scalekit.getAuthorizationUrl(redirectUri, {provider: 'google',state: state, // optional});// Redirect the user to this authorizationURLfrom scalekit import ScalekitClient, AuthorizationUrlOptions, CodeAuthenticationOptions# Initialize the SDK clientscalekit_client = ScalekitClient('<SCALEKIT_ENVIRONMENT_URL>','<SCALEKIT_CLIENT_ID>','<SCALEKIT_CLIENT_SECRET>')options = AuthorizationUrlOptions()options.provider = 'google'authorization_url = scalekit_client.get_authorization_url(redirect_uri=<redirect_uri>,options=options)# Redirect the user to this authorization URLimport ("github.com/scalekit/scalekit-sdk-go")func main() {scalekitClient := scalekit.NewScalekitClient("<SCALEKIT_ENVIRONMENT_URL>","<SCALEKIT_CLIENT_ID>","<SCALEKIT_CLIENT_SECRET>")options := scalekitClient.AuthorizationUrlOptions{}// Pass the social login provider details while constructing the authorization URL.options.Provider = "google"authorizationURL := scalekitClient.GetAuthorizationUrl(redirectUrl,options,)// Next step is to redirect the user to this authorization URL}package com.scalekit;import com.scalekit.internal.http.AuthorizationUrlOptions;public class Main {public static void main(String[] args) {ScalekitClient scalekitClient = new ScalekitClient("<SCALEKIT_ENVIRONMENT_URL>","<SCALEKIT_CLIENT_ID>","<SCALEKIT_CLIENT_SECRET>");AuthorizationUrlOptions options = new AuthorizationUrlOptions();options.setProvider("google");try {// Pass the social login provider details while constructing the authorization URL.String url = scalekitClient.authentication().getAuthorizationUrl(redirectUrl, options).toString();} catch (Exception e) {System.out.println(e.getMessage());}}}After users complete authentication, Scalekit redirects them to your
redirect_uri
with an authorization code. Handle this callback to complete the authentication process. -
Retrieve user information
Section titled “Retrieve user information”After successful social authentication, Scalekit redirects users to your
redirect_uri
with a unique authorizationcode
. Exchange this code for user profile details on your server side.Here’s how to convert the authorization code into user data:
const { code, state } = req.query;if (error) {// Handle errors}const res = await scalekit.authenticateWithCode(code, redirectUri);// res.user has the authenticated user's detailsconst userEmail = res.user.email;// Next step: create a session for this user and allow accesscode = request.args.get('code')error = request.args.get('error')error_description = request.args.get('error_description')if error:raise Exception(error_description)result = scalekit_client.authenticate_with_code(code,<redirect_uri>)# result.user has the authenticated user's detailsuser_email = result.user.email# Next step: create a session for this user and allow accesscode := r.URL.Query().Get("code")error := r.URL.Query().Get("error")errorDescription := r.URL.Query().Get("error_description")if error != "" {// Handle errors and exit}res, err := sc.AuthenticateWithCode(code, redirectUrl)if err != nil {// Handle errors and exit}// res.User has the authenticated user's detailsuserEmail := res.User.Email// Next step: create a session for this user and allow accessString code = request.getParameter("code");String error = request.getParameter("error");String errorDescription = request.getParameter("error_description");if (error != null && !error.isEmpty()) {// Handle errorsreturn;}try {AuthenticationResponse res = scalekitClient.authentication().authenticateWithCode(code, redirectUrl);// res.getIdTokenClaims() has the authenticated user's detailsString userEmail = res.getIdTokenClaims().getEmail();} catch (Exception e) {// Handle errors}// Next step: create a session for this user and allow access
Next steps
Section titled “Next steps”You’ve successfully implemented social login functionality in your application. Your users can now authenticate using their preferred social identity providers.