@authefy/node-client

Authefy SDK for Node.js server.

Getting Started

For more information, check the official github repository.

To Install @authefy/node-client via npm:

$ npm install @authefy/node-client

Instantiate a new Authefy node client:

import Client from '@authefy/node-client';

const authefyNodeClient =  new Client({
    appAccessKey: process.env.AUTHEFY_APP_ACCESS_KEY
});

To create/register a user with a password:

/*
 username - authefy unique username
 password - authefy password 
 email(optional) - user email. If appplication requires email verification,
    it will automatically send email verification link which the user will then
    click and get verified.
 externalId(optional): If the application has different user storage and they want
    to use their own custom id, set this externalId.
*/

await authefyNodeClient.user.create({
  username: 'soaman',
  password: 'pisobr',
  email: 'cu@wuc.hn',
  externalId: '61426833-966e-5d7b-a4d9-ac269a7ac579',
  details: {
     name: 'Kathryn Manning'
  }
});

// returns: User Object

To update users with their id:

/*
    When updating user details, the old user details will be merge to the new user details.
    If a key exists in both details, the value from the new user details will be used.
*/

await authefyNodeClient.user.update('ca10416e530f5f7bb7dfb02940143618', {
    isVerified: true,
    email: 'uphibtu@su.li',
    details: {
        age: 24
    }
})

To remove/delete users with their id:

await authefyNodeClient.user.delete('ca10416e530f5f7bb7dfb02940143618')

To get user information with their id:

await authefyNodeClient.user.read('ca10416e530f5f7bb7dfb02940143618')
// returns: User Object

To get the list of users:

/*
    filter(optional) - filters the query with selected information
    sort(optional) - ASC | DESC sorts the order of the edges
    size(optional) - limits the page size
    after(optional) - query users after the given cursor
    before(optional) - query users before the given cursor
*/

await authefyNodeClient.user.readAll({
    filter: {
        username: 'kuudahi',
        isEmailVerified: true,
        isVerified: true,
    },
    sort: 'ASC',
    size: 5,
    after: 'gejtiwzosusivozi',
    before: 'mulfikpahfafiwas'
})
/* 
    returns: {
        edges: { node: User; cursor: string };
        endCursor?: string;
        totalCount: number;
    }
*/

To get all user events:

/*
    filter(optional) - filters the query with selected information
    sort(optional) - ASC | DESC sorts the order of the edges
    size(optional) - limits the page size
    after(optional) - query user events after the given cursor
    before(optional) - query user events before the given cursor
*/

await authefyNodeClient.event.fetch({
    filter: {
        type: UserCreated | UserUpdated | UserDeleted,
    },
    sort: 'ASC',
    size: 5,
    after: 'gejtiwzosusivozi',
    before: 'mulfikpahfafiwas'
})
/* 
    returns: {
        edges: { node: UserEvent; cursor: string };
        endCursor?: string;
        totalCount: number;
    }
*/

To listen to the user event socket:

/*
    startFromLastEventCursor(optional) - by default null
        if null: it will query user events from the start
        if string: it will query user events starting from the given cursor
        if false: it won't query any user events
    type: filters what user event type to listen 
*/

const userEventEmitter = await authefyNodeClient.event.listen({
    startFromLastEventCursor: null | string | false,
    type: UserCreated | UserUpdated | UserDeleted
});

userEventEmitter.on('data', (event) => {
    console.log(event); /*
        {
          id: string;
          user: string;
          externalId?: string;
          application: string;
          type: UserCreated | UserUpdated | UserDeleted;
          body?: Record<string, any>;
          dateTimeCreated: Date;
          cursor: string;
        }
    */
});

To authenticate/login user with username and password:

await authefyNodeClient.token.authenticate({
    grantType: 'password',
    username: 'hopafk',
    password: 'zobluf'
});

// returns: { accessToken: string; refreshToken: string; expiresIn: number; }

To authenticate/login user with refresh token:

await authefyNodeClient.token.authenticate({
    grantType: 'refreshToken',
    refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjBmMmRjOTk3ZmJlYjVjZWY5ZGMxZjk3MmUyNTkyZGY2IiwiaWF0IjoxNjIxOTk2MTgxOTg3LCJleHAiOjE2MjE5OTYxODE5ODcsInN1YiI6ImE0MWVhMTJmOThhOTUwZDNhNTY4OTFkNjAxYTM3Y2NkIn0.nL1e1D53TOow8gUNtqkJTGEU6oGECwi2aCtMySHAhzk',
});

// returns: { accessToken: string; expiresIn: number; }

To get the user claims of the authorization bearer token:

await authefyNodeClient.token.authorizeBearer(
  'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjBmMmRjOTk3ZmJlYjVjZWY5ZGMxZjk3MmUyNTkyZGY2IiwiaWF0IjoxNjIxOTk2MTgxOTg3LCJleHAiOjE2MjE5OTYxODE5ODcsInN1YiI6ImE0MWVhMTJmOThhOTUwZDNhNTY4OTFkNjAxYTM3Y2NkIn0.nL1e1D53TOow8gUNtqkJTGEU6oGECwi2aCtMySHAhzk'
);

/*
  returns: {
    id: string;
    externalId?: string;
    iat: number;
    exp: number;
    sub: string;
  }
*/

To revoke/logout user session with refresh token:

await authefyNodeClient.token.revoke({
    refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjBmMmRjOTk3ZmJlYjVjZWY5ZGMxZjk3MmUyNTkyZGY2IiwiaWF0IjoxNjIxOTk2MTgxOTg3LCJleHAiOjE2MjE5OTYxODE5ODcsInN1YiI6ImE0MWVhMTJmOThhOTUwZDNhNTY4OTFkNjAxYTM3Y2NkIn0.nL1e1D53TOow8gUNtqkJTGEU6oGECwi2aCtMySHAhzk'
});

Last updated

Was this helpful?