Nhost JS SDK is used to handle Auth and Storage.
$ npm install nhost-js-sdk
or
$ yarn add nhost-js-sdk
import nhost from "nhost-js-sdk";
const config = {
base_url: "https://backend-REPLACE.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-REPLACE.nhost.app",
use_cookies: true,
};
import { auth, storage } from "src/utils/nhost";
auth.register(email, password);
auth.register(email, password, { display_name: "Joe Doe" });
auth.login(email, password);
auth.logout();
auth.onAuthStateChanged((logged_in) => {
console.log("auth state changed!");
console.log({ logged_in });
});
Can return one of [true, false, null]
.
true
means users is logged infalse
means users is not logged innull
means that nhost-js-sdk
is trying to login the user, but don't know yet if the user is logged in or not.auth.isAuthenticated();
Get the full JWT token
auth.getJWTToken();
Get specific claim from the JWT token.
auth.getClaim("x-hasura-user-id");
auth.activate(<ticket>);
Note: The user must be logged in.
auth.changeEmail(new_email);
auth.changeEmailRequest(new_email);
auth.changeEmailChange(ticket);
auth.changePassword(old_password, new_password);
auth.changePasswordRequest(email);
auth.changePasswordChange(new_password, ticket);
Note: User must be logged in.
auth.MFAGenerate();
auth.enable(code);
auth.enable(code);
Note: ticket
comes from the auth.login()
response if the user has MFA enabled.
auth.MFATotp(code, ticket);
storage.put(path, file, metadata?, onUploadProgress?);
storage.delete(path);
storage.getMetadata(path);
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 };
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 };
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 };