Bypassing Repeated Auth in Scheduler

I am trying to use Nylas for Email and scheduling. I have succssfully integrated Email using grant_id. While integrating scheduling using ScheduleEditor I faced some issues.
The component asks for authenticating with email again, I dont want repeated authentication. What is the way for authenticating using only grant_id. I have tried custom authentication blog from nylas doc.

In custom authentication, currentUser function asks for user_id: id.sub… Which id i should fill this field with?

hey @mohaimin try this one i am able to do it using some changes in my code

1 Like

Thanks, I found this one and got the correct sub.

But still it is not working. Now the problem is, only the constructor is called of CustomIdentityRequestWrapper class, no other function is being called of this class.

Here is my implementation of this class:

import { getCurrentUser } from "@/actions/user.actions";

export class CustomIdentityRequestWrapper {
  constructor(accessToken, domain) {
    console.log("constructor");
    // Initialize the class
    this.accessToken = accessToken;
    this.domain = domain;
  }
  async request(args) {
    console.log(args, "hello");
    try {
      const response = await fetch(`${this.domain}/grants/me/${args.path}`, {
        method: args.method,
        body: JSON.stringify(args.body),
        headers: {
          ...args.headers,
          Authorization: `Bearer ${this.accessToken}`,
          "Content-Type": "application/json",
        },
      });

      // Check if the response is not okay (e.g., 404, 500)
      if (!response.ok) {
        console.error(`Error: ${response.status} ${response.statusText}`);
        return { error: `Error: ${response.status} ${response.statusText}` };
      }

      // Parse the response
      const data = await response.json();
      console.log(data);
      return [data, null];
    } catch (error) {
      console.error("Fetch error:", error);
      return { error: "Error" };
    }
  }

  /**
   * This method returns the current user's information.
   */

  async currentUser() {
    // IMPLEMENT: Get the logged in user's ID token and return the user information
    console.log("current user");

    const user = await getCurrentUser();

    return {
      id: "c97xxxxxxxxxxxxx",
      email: user.grantEmail,
      name: "test",
      provider: user.provider,
    };
  }

  /**
   * This method sets the default authentication arguments to use when authenticating the user.
   */
  async setDefaultAuthArgs(authArgs) {
    console.log(authArgs, "default");
    // Set the default authentication arguments
    return authArgs;
  }

  /**
   * This method returns the URL to redirect the user to for authentication.
   */
  async authenticationUrl() {
    // IMPLEMENT: Return the URL to redirect the user to for authentication
    return process.env.NEXT_PUBLIC_URL + "/sign0in";
  }
}

And here is my function call:

const accessToken = user.nylasAccessToken;
const domain = "https://api.eu.nylas.com/v3";

const nylasApiRequest = new CustomIdentityRequestWrapper(accessToken, domain);

return (
    <NylasSchedulerEditor
      schedulerPreviewLink={`${process.env.NEXT_PUBLIC_URL}/meet?config_id=1`}
      nylasApiRequest={nylasApiRequest}
      defaultSchedulerConfigState={{
        selectedConfiguration: {
          requires_session_auth: false,
        },
      }}
    />
  )

this might be because you haven’t imported component how does you react component gonna know ?

import { NylasSchedulerEditor } from "@nylas/react";