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:
- Frontend: The user clicks on a "Login with Google" (or other provider) button in the frontend.
- Frontend: The frontend opens a popup window to the social provider's login page.
- Social Provider: The user authenticates with the social provider.
- Social Provider: The social provider redirects the user back to the frontend with an authorization code.
- Frontend: The frontend sends the authorization code to the Archibald backend.
- Backend: The backend uses the
CDCSocializeServiceto exchange the authorization code for a CDC session. - 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.