Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

v3.0.0

Choose a tag to compare

@elitan elitan released this 01 Mar 12:48
· 43 commits to master since this release

This major release introduces a few small (mostly renaming) breaking changes and some minor improvements to the API:

Breaking change - It refers to the way the client is created and initialized:

Before:

import nhost from "nhost-js-sdk";

const config = {
  base_url: "https://backend-url.nhost.app",
};

nhost.initializeApp(config);

const auth = nhost.auth();
const storage = nhost.storage();

export { auth, storage };

Now:

import { createClient } from 'nhost-js-sdk'

// createClient returns an instance of NhostClient 
const nhostClient = createClient({
  baseURL: 'https://backend-url.nhost.app',
})

// auth and storage are now properties of NhostClient
const auth = nhostClient.auth;
const storage = nhostClient.storage;

export { auth, storage };

Breaking change - functions renamed:

  • auth.changeEmailRequest() is now auth.requestEmailChange()
  • auth.changeEmailChange() is now auth.confirmEmailChange()
  • auth.changePasswordRequest() is now auth.requestPasswordChange()
  • auth.changePasswordChange() is now auth.confirmPasswordChange()

Improvements and additions to the API

register()

  • Unless you have set AUTO_ACTIVATE_NEW_USERS to false, we will log in the user and return a promise with the session and user data. Because the user gets logged in on registration, you don't have to redirect your users to /login anymore.
{
  "session": {
    "jwt_token": "e3MmYZSJdLCJ4LWhhjE0MDg3Nzc3fQ.d4kdsiBjD3MpPpbNNbaMt-gyHHVXwrSvrAtVcEQ_jB8",
    "jwt_expires_in": 900000,
    "user": {
      "id": "kcf72f45-5a2d-4615-810d-96e10548bb35",
      "display_name": "nuno@nhost.io", 
      "email": "nuno@nhost.io"
    },
    "refresh_token": "dd69aafd-71f6-4f97-be93-9bdbfb192fe6"
  },
  "user": {
    "id": "kcf72f45-5a2d-4615-810d-96e10548bb35",
    "display_name": "nuno@nhost.io",
    "email": "nuno@nhost.io"
  }
}
  • If you have indeed AUTO_ACTIVATE_NEW_USERS set to false, session will be null because we can't log in the user.
{
  "session":  null,
  "user": {
    "id": "kcf72f45-5a2d-4615-810d-96e10548bb35",
    "display_name": "nuno@nhost.io",
    "email": "nuno@nhost.io"
  }
}

Usage

const { session, user } = await auth.register({ email, password }) // notice the object being passed

login()

  • A promise with the session and user data is returned:
{
  "session": {
    "jwt_token": "e3MmYZSJdLCJ4LWhhjE0MDg3Nzc3fQ.d4kdsiBjD3MpPpbNNbaMt-gyHHVXwrSvrAtVcEQ_jB8",
    "jwt_expires_in": 900000,
    "user": {
      "id": "kcf72f45-5a2d-4615-810d-96e10548bb35",
      "display_name": "nuno@nhost.io", 
      "email": "nuno@nhost.io"
    },
    "refresh_token": "dd69aafd-71f6-4f97-be93-9bdbfb192fe6"
  },
  "user": {
    "id": "kcf72f45-5a2d-4615-810d-96e10548bb35",
    "display_name": "nuno@nhost.io",
    "email": "nuno@nhost.io"
  }
}
  • If the user has MFA enabled, an mfa object is added and the response looks like this instead:
{
  "session": null,
  "user": null,
  "mfa": {
    "ticket": "762ea295-4a12-436f-b8fc-36b91aefb28e"
  },
}

Usage

const { session, user } = await auth.login({ email, password }) // notice the object being passed

// in case MFA is enabled
const { mfa } = await auth.login({ email, password }) 

MFATotp()

  • Returns an object with the user and corresponding session as a promise (same as login() and register()).

Usage

const { session, user } = await auth.MFATotp(code, ticket)

logout()

  • For the sake of API consistency, logout() also returns session and user set to null.

Additions to the API

user()

  • Returns an object for the current logged in user or null otherwise
{
	"id": "kcf72f45-5a2d-4615-810d-96e10548bb35",
	"display_name": "Nuno Pato",
	"email": "nuno@nhost.io"
}

Usage

auth.user()

refreshSession()

  • Explicitly calls refresh token

Usage

auth.refreshSession()

Full documentation here