Skip to main content

Social Login

The @archibald/cdc package provides a CDCSocializeService for implementing social login with SAP Customer Data Cloud (CDC).

Social Login Flow

The social login flow with CDC typically involves the following steps:

  1. Frontend: The user clicks on a "Login with Google" (or other provider) button in the frontend.
  2. Frontend: The frontend opens a popup window to the social provider's login page.
  3. Social Provider: The user authenticates with the social provider.
  4. Social Provider: The social provider redirects the user back to the frontend with an authorization code.
  5. Frontend: The frontend sends the authorization code to the Archibald backend.
  6. Backend: The backend uses the CDCSocializeService to exchange the authorization code for a CDC session.
  7. Backend: The backend creates an Archibald session for the user and returns a JWE token to the frontend.

CDCSocializeService

The CDCSocializeService provides a set of methods for interacting with the CDC socialize API. The most important method for social login is getToken, which exchanges an authorization code for a CDC session.

Usage

Here is a high-level example of how you might use the CDCSocializeService in a custom controller to handle the social login callback:

import { CDCSocializeService } from '@archibald/cdc';
import { Inject, BaseController } from '@archibald/server';
import type { Request, ResponseToolkit } from '@hapi/hapi';

export class SocialLoginController extends BaseController {
@Inject()
private readonly cdcSocializeService: CDCSocializeService;

constructor() {
super('SocialLoginController');
}

public async handleSocialLogin(request: Request, h: ResponseToolkit) {
const { authorizationCode } = request.payload as { authorizationCode: string };

try {
const cdcSession = await this.cdcSocializeService.getToken({
grant_type: 'authorization_code',
code: authorizationCode
});
// ... create an Archibald session and return a JWE token
return h.response({ token: '...' }).code(200);
} catch (error) {
// ... handle error
return h.response({ error: '...' }).code(500);
}
}
}

And how you would configure the route for this controller:

// src/server/routes/social-login.ts
import { type DefaultRouteConfig, RouteMethod } from '@archibald/core';

const SocialLoginServerRouteConfig: DefaultRouteConfig[] = [
{
method: RouteMethod.POST,
path: 'social-login',
handler: 'SocialLoginController.handleSocialLogin',
options: {
description: 'Handles social login callback',
tags: ['api', 'Auth']
}
}
];

export default SocialLoginServerRouteConfig;

This is a simplified example, but it illustrates the basic flow of how to use the CDCSocializeService to implement social login with CDC.