By default, Kleeen products are built with Cognito integrated. However, you can replace this authentication system with one of your choosing using the instructions below.
The information below is also in the readme file located in the code repository.
How to Add Custom Integrations
Kleeen Software provides the option to extend the default authentication or implements new ones. To support custom workflows, `@kleeen/auth library` exposes a set of types and interfaces.
```
import { Integrations, KSAuth } from '@kleeen/auth';
KSAuth.configure({
authenticationHandler: new Integrations.CognitoAuthenticationHandler(),
});
```
AuthenticationHandler Base Definition
The AuthenticationHandler interface is the blueprint to implement different workflows. Here is an example of a custom implementation using Firebase:
```
import 'firebase/auth';
import firebase from 'firebase/app';
import { Integrations } from '@kleeen/auth';
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: '',
authDomain: '',
databaseURL: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: '',
};
export class FirebaseAuthenticationHandler extends Integrations.AuthenticationHandler {
constructor(config: typeof firebaseConfig = firebaseConfig) {
super();
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
}
/**
* Sign in a register user using username and password
*/
async signIn(options: SignInOptions): Promise<IUser> {
const { password, username } = options;
const response = await firebase.auth().signInWithEmailAndPassword(username, password);
return {
...response,
email: response?.user?.email,
getUsername: () => response?.user?.displayName,
role: null, // Set here the default for the current user
roles: [], // set here the list of roles assigned for the current user
};
}
/**
* A function that takes a new context object and update it if needed
*
* @param {Record<string, any>} context is an existing context
* @return {Record<string, any>} with an updated context
*/
setContext(context: Record<string, any>): Record<string, any> {
return {
...context,
headers: {
...context.headers,
MY_CUSTOM_HEADER: 'GOING HERE',
},
};
}
}
```
How to Update the Authentication Handler
The following is the example of configuring the **KSAuth** class to use the custom implementation.
```
import { FirebaseAuthenticationHandler } from './google-firebase';
import firebaseConfiguration from './custom-implementations/firebase.json'
KSAuth.configure({
authenticationHandler: new FirebaseAuthenticationHandler(firebaseConfiguration),
});
```
If you have any questions about authentication systems or need assistance with them, you can reach out to support@kleeen.software or contact us using the chat in the Kleeen.cloud authoring environment.
Comments
0 comments
Please sign in to leave a comment.