@anzar-auth/client
v1.5.11
Published
Anzar is a lightweight authentication and authorization framework that runs as a separate microservice
Downloads
597
Maintainers
Readme
Anzar SDK Documentation
Install The Typescript SDK
In a ts project run the following command to install the anzar package.
npm
$ npm install @anzar-auth/clientpnpm
$ pnpm install @anzar-auth/clientyarn
$ yarn add @anzar-auth/clientCreate Anzar Auth Instance
if you are using vite as a bundler, add this to your vite.config.js
import yaml from "@rollup/plugin-yaml";
export default {
plugins: [yaml()],
};$ npm install -D @rollup/plugin-yamlin your main entry file (main.ts for example), import Anzar and create your auth instance
// Initialize once at application startup
// The SDK will communicate with your Anzar container at the configured api_url
import { Anzar } from "@anzar-auth/client";
import anzarConfig from "anzar.yml";
const anzar = new Anzar(anzarConfig);⚠️ Note Choose your token storage adapter
import { LocalStorage } from "@anzar-auth/client/adapters";
const anzar = new Anzar(anzarConfig, {
storage: new LocalStorage(),
});Basic Usage
Anzar provides authentication support for email and password.
📝 Note: Other methods of authentication will be implemented later
Sign Up
To sign up a user you need to call the method register with the user's information.
import { AnzarError, type AuthResult, ErrorCode } from "@anzar-auth/client/types";
try {
const data: AuthResult = await anzar.Auth.register({ username, email, password });
console.log(data);
} catch (e) {
if (e instanceof AnzarError) {
if (e.code === ErrorCode.InvalidCredentials) {
console.log(e.message);
}
else if (e.code === ErrorCode.AccountNotVerified) {
console.log(e.message);
}
}
}📝 Note: By default, the users are automatically signed in after they successfully sign up. Disabling this behavior will be implemented later
Sign In
To sign in a user you need to call the method login.
import { AnzarError, type AuthResult, ErrorCode } from "@anzar-auth/client/types";
try {
const data: AuthResult = await anzar.Auth.login({ email, password });
console.log(data);
} catch (e) {
if (e instanceof AnzarError) {
if (e.code === ErrorCode.InvalidCredentials) {
console.log(e.message);
}
else if (e.code === ErrorCode.AccountNotVerified) {
console.log(e.message);
}
}
}