This repository was archived by the owner on Jan 28, 2022. It is now read-only.
v3.0.0
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 nowauth.requestEmailChange()auth.changeEmailChange()is nowauth.confirmEmailChange()auth.changePasswordRequest()is nowauth.requestPasswordChange()auth.changePasswordChange()is nowauth.confirmPasswordChange()
Improvements and additions to the API
register()
- Unless you have set
AUTO_ACTIVATE_NEW_USERStofalse, we will log in the user and return a promise with thesessionanduserdata. Because the user gets logged in on registration, you don't have to redirect your users to/loginanymore.
{
"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_USERSset tofalse,sessionwill benullbecause 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 passedlogin()
- A promise with the
sessionanduserdata 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
mfaobject 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()andregister()).
Usage
const { session, user } = await auth.MFATotp(code, ticket)logout()
- For the sake of API consistency,
logout()also returnssessionanduserset tonull.
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