JSPM

  • Created
  • Published
  • Downloads 696
  • Score
    100M100P100Q24893F
  • License MIT

Nhost JS SDK

Package Exports

  • nhost-js-sdk

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nhost-js-sdk) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Nhost JS SDK

Nhost JS SDK to handle Auth and Storage.

Installation

npm install --save nhost-js-sdk

Setup

In ex /src/nhost/index.js:

import nhost from "nhost-js-sdk";

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

nhost.initializeApp(config);

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

export { auth, storage };

To use cookies (not recomended), use this config:

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

Usage auth and storage across in your app

import { auth, storage } from 'src/nhost/index.js';

Configuration

base_url

URL to your backend. Usually https://backend-xxx.nhost.app.

Type Required Default value
string Yes

use_cookies

Use a HTTP Only Cookie for the refresh_token. Recommended value is false (default) because of cross platform issues and third party cookies issues in browsers.

Type Required Default value
boolean NO false

Auth

Register

auth.register(email, password);

Register with user_data

auth.register(email, password, { display_name: "Joe Doe" });

Login

auth.login(email, password);

Logout

auth.logout();

onAuthStateChanged

auth.onAuthStateChanged((logged_in) => {
  console.log("auth state changed!");
  console.log({ logged_in });
});

Check if user is authenticated

auth.isAuthenticated();

Get JWT token

auth.getJWTToken();

Get JWT claim

auth.getClaim("x-hasura-user-id");

Activate account

auth.activate(<ticket>);

Change email address

Note: The user must be logged in.

auth.changeEmail(new_email);

Request new email change

auth.changeEmailRequest(new_email);

Change to requested email

auth.changeEmailChange(ticket);

Change password

auth.changePassword(old_password, new_password);

Request new password

auth.changePasswordRequest(email);

Change password using ticket

auth.changePasswordChange(new_password, ticket);

Generate MFA QR-code

Note: User must be logged in.

auth.MFAGenerate();

Enable MFA

auth.enable(code);

Disable MFA

auth.enable(code);

Login using TOTP

Note: ticket comes from the auth.login() response if the user has MFA enabled.

auth.MFATotp(code, ticket);

Storage

Upload

storage.put(path, file, metadata?, onUploadProgress?);

UploadString

storage.putString(path, file, type?, metadata?, onUploadProgress?);

Type can be one of [raw (default), data_url].

Exmaple:

const data =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
storage.putString("/public/red-dot.png", data, "data_url");

Delete


storage.delete(path);

```

### Get metadata

```

storage.getMetadata(path);

Setup in different environments

React Native

Install:

yarn add @react-native-community/async-storage

More info: https://react-native-community.github.io/async-storage/docs/install.

import nhost from "nhost-js-sdk";
import AsyncStorage from "@react-native-community/async-storage";

const config = {
  base_url: "https://backend-xxxx.nhost.app",
  client_storage: AsyncStorage,
  client_storage_type: "react-native",
};

nhost.initializeApp(config);

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

export { auth, storage };

Ionic

import nhost from "nhost-js-sdk";
import { Plugins } from "@capacitor/core";
const { Storage } = Plugins;

const config = {
  base_url: "https://backend-xxxx.nhost.app",
  client_storage: Storage,
  client_storage_type: "capacitor",
};

nhost.initializeApp(config);

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

export { auth, storage };

Expo

Install:

expo install expo-secure-store

More info: https://docs.expo.io/versions/latest/sdk/securestore/.

import nhost from "nhost-js-sdk";
import * as SecureStore from "expo-secure-store";

const config = {
  base_url: "https://backend-xxxx.nhost.app",
  client_storage: SecureStore,
  client_storage_type: "expo-secure-storage",
};

nhost.initializeApp(config);

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

export { auth, storage };