Skip to content

@drupal-js-sdk/auth

Overview

Helpers for Drupal core session endpoints: session token, login, logout, password reset and registration. It relies on the configured HTTP client/session in your Drupal instance.

Usage

JavaScript
import { Drupal } from "@drupal-js-sdk/core";
import { DrupalAuth } from "@drupal-js-sdk/auth";

const drupal = new Drupal({ baseURL: "https://example.com" });
const auth = new DrupalAuth(drupal);

await auth.getSessionToken();
const isLoggedIn = await auth.loginStatus();
if (!isLoggedIn) {
  await auth.login("username", "password");
}

Public API

class DrupalAuth

Constructor

constructor(drupal: Drupal)
Example
JavaScript
import { Drupal } from "@drupal-js-sdk/core";
import { DrupalAuth } from "@drupal-js-sdk/auth";

const drupal = new Drupal({ baseURL: "https://example.com" });
const auth = new DrupalAuth(drupal);

getSessionToken

getSessionToken(): Promise<XhrResponse>
Example
JavaScript
await auth.getSessionToken();

login

login(name: string, pass: string): Promise<XhrResponse>
Example
JavaScript
await auth.login("username", "password");

loginStatus

loginStatus(): Promise<boolean>
Example
JavaScript
const isLoggedIn = await auth.loginStatus();

logout

logout(): Promise<XhrResponse>
Example
JavaScript
await auth.logout();

passwordResetByUserName

passwordResetByUserName(name: string): Promise<XhrResponse>
Example
JavaScript
await auth.passwordResetByUserName("username");

passwordResetByMail

passwordResetByMail(mail: string): Promise<XhrResponse>
Example
JavaScript
await auth.passwordResetByMail("user@example.com");

register

register(name: string, mail: string): Promise<XhrResponse>
Example
JavaScript
await auth.register("newuser", "newuser@example.com");

Notes

  • Uses HTTP client and session from the provided Drupal instance.
  • Errors are DrupalError with getErrorCode().