Skip to content

Setting up Khalis SSO in sttm‐desktop

Gauravjeet Singh edited this page Feb 2, 2024 · 5 revisions

Overview

After logging in with Khalis SSO, users gain access to shared user data, which is currently utilized by both sttm-desktop and sttm-web. We have the 'Favourite shabad' functionality, which is synchronized between sttm-web and sttm-desktop. For instance, if a user logs in and marks a shabad as a favourite on sttm-web, this preference will also be reflected in the sttm-desktop app upon logging in. As of now, the 'Favourite shabad' functionality is the only feature implemented in this manner. Moving forward, we plan to implement additional features that can be shared across applications

Process of Khalis SSO

  • Khalis App (Sttm-desktop, in this case) sends a request to the Khalis SSO Service Provider.
  • Service Provider accepts the request, and redirects to the khalis login page.
  • After successful login, user is redirected back to the Service Provider with the User token
  • Service Provider sends the User token back to the app (Sttm-desktop).

Deployment link

Khalis SSO service provider: https://serviceprovider.khalis.net

Get the user token

Step 1: Send a login request

To send a login request, redirect the user to
https://serviceprovider.khalis.net/login/sso?redirect_url={your app's redirect url}

The redirect url will be redirected to upon succesful login.

For example, for sttm-desktop, the redirect url is: sttm-desktop://login

As it is a desktop app, we are using custom protocol for redirect url in electronjs.

Step 2: Save the user token

After the sucesful login, the service provider will go to the redirect url given in step 1.
It will send the user token in a parameter named token

For example, in sttm-desktop the service provider will redirect to sttm-desktop://login?token={user token here}

Get the token from above url, and save it securely within the app.

Logged in user info

To get the details of the logged in user, send request to the /user endpoint. Send a GET request with the Authorization header as shown below. Don't forget to add Bearer with the user token in header.

  • JS implementation:
const response = await fetch(`${SP_API}/user`, {
   headers: {
     Authorization: `Bearer ${userToken}`,
   },
});
const userData = response.json().then((data) => data);
  • Data format
{
   email: "abc@xyz.com",
   exp: ,
   firstname: "John",
   iat: 
   lastname: "Doe"
   nameID: ""
   nameIDFormat: ""
}

Using the favourite shabad feature

For any request related to fav shabad, we would need to send the Authorization header.

Add a fav shabad

  • To add a new fav shabad, send a POST request to /favourite-shabads
  • It needs two parameters
    • shabadId: id of the shabad to be marked as fav
    • verseId: id of the verse to be used as title of fav shabad
await fetch(`${SP_API}/favourite-shabads`, {
    method: 'POST',
    body: JSON.stringify({
      shabadId: 223,
      verseId: 12,
    }),
    headers: {
      'Content-Type': 'application/json',
       Authorization: `Bearer ${userToken}`,
    },
  });



Delete a fav shabad

  • To delete a fav shabad, send a DELETE request to /favourite-shabads/[shabadId]
  • Send the shabad id in the endpoint to delete it from favourites.
await fetch(`${SP_API}/favourite-shabads/${shabadId}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${userToken}`,
    },
  });



List all fav shabads

  • To list all the fav shabads send a GET request to endpoint /favourite-shabads
  const response = await fetch(`${SP_API}/favourite-shabads`, {
    headers: {
      Authorization: `Bearer ${userToken}`,
    },
  });
  const favShabadList = response.json().then((data) => data.favouriteShabads);
  • The resulting fav shabad response would be in the following format:
{
    "favouriteShabads": [
        {
            "id": 551,
            "user_id": 15,
            "shabad_id": 957,
            "verse_id": 12303,
            "comment": null,
            "created_at": "2023-05-11T06:10:39.000Z",
            "updated_at": "2023-05-11T06:10:39.000Z"
        },
        {
            "id": 552,
            "user_id": 15,
            "shabad_id": 10365,
            "verse_id": 114771,
            "comment": null,
            "created_at": "2023-05-11T06:22:05.000Z",
            "updated_at": "2023-05-11T06:22:05.000Z"
        }
    ]
}