1.0.21 • Published 4 years ago

@trixcms/nauth v1.0.21

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

NAuth

NAuth is a library to authenticate users of your TrixCMS site in NodeJS.

Prerequisites

Before using NAuth you must install the Auth plugin on your CMS

Installation

You can install the library via npm:

npm install @trixcms/nauth

How to use ?

First you need to define an instance of NAuth. The manufacturer will take the URL of your TrixCMS site as the first parameter and an optional second parameter, a timeout in milliseconds:

const NAuth = require('@trixcms/nauth')

const auth = new NAuth('http://website.domain')

Verify that a user exist

You can verify that a user exists with the exists (username: string): Promise <boolean> method which will take the username as a parameter. The method will return a promise to you.

const username = 'username'

auth.exists(username)
    .then((exists) => {

        if(exists) {
            console.log('The account exists.')
        } else {
            console.log('The account does not exists.')
        }

    })
    .catch(() => {
        console.error('An error has occurred.')
    })

Retrieve user information

You can verify that a user exists with the login (username: string, password: string): Promise <Profile> method which will take the username and password as parameters. The method will return you a promise with the profile:

const username = 'username'
const password = 'password'

auth.login(username, password)
    .then((profile) => {
        console.log(profile);
    })
    .catch(err => {
        if(err.code === 'INVALID_CREDENTIALS') {
            console.error('Bad credentials')
        } else {
            console.error('An error has occurred.')
        }
    })

List of properties available in the profile:

PropertyTypeDescription
idstringThe user ID.
uuidstringUser UUID.
usernamestringUsername of the user.
emailstringUser email address.
moneynumberNumber of user store points.
hasAvatarbooleanDoes the user have an avatar.
isBannedbooleanIs the user banned.
banReasonstring | nullThe reason for the ban if the user is banned.
isConfirmedbooleanHas the user confirmed their email address.
hasTwoAuthbooleanHas the user enabled dual authentication.
ranksstring[]List of user ranks.
tokenstringUser token.
createdAtDateDate of creation of the account.
updatedAtDateDate of last modification of the account.

Code example

const NAuth = require('@trixcms/nauth')

// We create an instance of NAuth
const auth = new NAuth('https://example.domain/', 10000)

// We verify that the 'admin' account exists.
auth.exists('admin')
    .then((exists) => {
        if (exists) {
            // The 'admin' account exists!

            // We retrieve the account profile using the login method.
            auth.login('admin', '123456789')
                .then((profile) => {

                    // optional
                    if (profile.isBanned) {
                        // This account is banned
                        return console.log('This account is banned')
                    }

                    // optional
                    if (!profile.isConfirmed) {
                        // This account has not validated its email address.
                        return console.log('This account has not validated its email address.')
                    }

                    // You can retrieve account information:
                    const uuid = profile.uuid
                    const username = profile.username
                    const email = profile.email
                    const token = profile.token

                    console.log('Here is the information about the admin account:', uuid, username, email, token)
                })
                .catch((err) => {
                    if (err.code === 'INVALID_CREDENTIALS') {
                        // Account credentials are invalid.
                        console.log('Account credentials are invalid.')
                    } else {
                        // An error has occurred.
                        console.log('An error has occurred.')
                    }
                })
        } else {
            // The 'admin' account does not exist!
            console.log('This account does not exist')
        }
    })
    .catch(() => {
        console.log('An error has occurred.')
    })

GitHub code size in bytes GitHub issues GitHub pull requests NPM GitHub package.json version

1.0.19

4 years ago

1.0.18

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago