# @authefy/node-client

## Getting Started

For more information, check the official [github repository](https://github.com/HighOutputVentures/authefy/tree/master/node-client#readme).

To Install `@authefy/node-client` via `npm`:&#x20;

```
$ npm install @authefy/node-client
```

Instantiate a new Authefy node client:

```typescript
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:

```typescript
/*
 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:

```typescript
/*
    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:

```typescript
await authefyNodeClient.user.delete('ca10416e530f5f7bb7dfb02940143618')
```

To get user information with their id:

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

To get the list of users:

```typescript
/*
    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:

```typescript
/*
    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:&#x20;

```typescript
/*
    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:

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

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

To authenticate/login user with refresh token:

```typescript
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:

```typescript
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:

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://iammeosjin.gitbook.io/authefy/authefy-node-client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
