Authentication

Default authentication service is provided via DrupalAuth. It provides helper method to do cookie based authentication with Drupal backend.

TIP

Authentication calls requires some persistent data to be maintained. Hence we need to set a session service in our sdk.

import {Drupal} from 'drupal-js-sdk';
import { DrupalAuth } from '@drupal-js-sdk/auth';
import { StorageInMemory } from '@drupal-js-sdk/storage';

const sdk = new Drupal({baseURL: 'http://example.com'});
// Awailable in Node and Browser environments.
const sessionStorage = new StorageInMemory();
sdk.setSessionService(sessionStorage);
const auth = new DrupalAuth(sdk);





 
 
 

1
2
3
4
5
6
7
8
9
import {Drupal} from 'drupal-js-sdk';
import { DrupalAuth } from '@drupal-js-sdk/auth';
import { StorageInWeb } from '@drupal-js-sdk/storage';

const sdk = new Drupal({baseURL: 'http://example.com'});
// Awailable only in Browser environments.
const sessionStorage = new StorageInWeb(() => window.localStorage);
sdk.setSessionService(sessionStorage);
const auth = new DrupalAuth(sdk);





 
 
 

1
2
3
4
5
6
7
8
9
import {Drupal} from 'drupal-js-sdk';
import { DrupalAuth } from '@drupal-js-sdk/auth';
import { StorageInWeb } from '@drupal-js-sdk/storage';

const sdk = new Drupal({baseURL: 'http://example.com'});
// Awailable only in Browser environments.
const sessionStorage = new StorageInWeb(() => window.sessionStorage);
sdk.setSessionService(sessionStorage);
const auth = new DrupalAuth(sdk);





 
 
 

1
2
3
4
5
6
7
8
9

Login status

let logged_in = false;
auth.loginStatus()
    .then(status => logged_in)
    .catch((error) => {
        // Display message that login status check failed. 
    }) 

 




1
2
3
4
5
6

Login

let user_info = {};
auth.login('admin', 'Z1ON0101')
    .then(data => user_info)
    .catch((error) => {
        // Display message that login failed. 
    }) 

 




1
2
3
4
5
6

Logout experimental

let logged_in = true;
auth.logout()
    .then(() => {logged_in = false})
    .catch((error) => {
        // Display message that logout failed. 
    }) 

 




1
2
3
4
5
6

Password Reset experimental

Password reset request using username

auth.passwordResetByUserName('admin')
    .then(() => { 
        // Show some message about password reset was success.
    })
    .catch((error) => {
        // Display message that password reset failed. 
    }) 
 






1
2
3
4
5
6
7

Password reset request using user mail

auth.passwordResetByMail('admin@example.com')
    .then(() => { 
        // Show some message about password reset was success.
    })
    .catch((error) => {
        // Display message that password reset failed. 
    }) 
 






1
2
3
4
5
6
7

Register experimental

WARNING

Some extra configuration has to be performed on Drupal side for registration to work

Drupal Side configuration

  • Install restuiopen in new window module and enable User registration resource.
    • Download and enable restuiopen in new window module
    • Enable User registration (/user/register: POST) resource
    • Configure User registration resource as follows
      • Method POST : enabled
      • Accepted Request format json : checked
    • Authentication provider cookie : checked
  • Allow Permission Access POST on User registration resource for Anonymous users.
let user_info = {};
// Returns a promise that is fulfilled with the user when the registration completes.
auth.register('admin', 'admin@example.com')
    .then(data => {
        // Succesfully registered.
    })
    .catch((error) => {
        // Display message that login failed. 
    }) 


 






1
2
3
4
5
6
7
8
9
References

On Drupal.org

Others