@anzar-auth/client 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Anzar SDK Documentation
2
+
3
+ ## Install The Typescript SDK
4
+ In a ts project run the following command to install the anzar package.
5
+
6
+ **npm**
7
+ ```bash
8
+ $ npm install anzar
9
+ ```
10
+
11
+ **pnpm**
12
+ ```bash
13
+ $ pnpm install anzar
14
+ ```
15
+
16
+ **yarn**
17
+ ```bash
18
+ $ yarn add anzar
19
+ ```
20
+
21
+ ## Create Anzar Auth Instance
22
+ if you are using `vite` as a bundler, add this to your `vite.config.js`
23
+ ```javascript
24
+ import yaml from "@rollup/plugin-yaml";
25
+
26
+ export default {
27
+ plugins: [yaml()],
28
+ };
29
+ ```
30
+ ```bash
31
+ $ npm install -D @rollup/plugin-yaml
32
+ ```
33
+
34
+ in your main entry file (`main.ts` for example), import Anzar and create your auth instance
35
+
36
+ ```typescript
37
+ // Initialize once at application startup
38
+ // The SDK will communicate with your Anzar container at the configured api_url
39
+ import { Anzar } from "anzar";
40
+ import anzarConfig from "anzar.yml";
41
+
42
+ const anzar = new Anzar(anzarConfig);
43
+ ```
44
+
45
+ ⚠️ **Note**
46
+ Choose your token storage adapter
47
+
48
+ ```typescript
49
+ import { LocalStorage } from "anzar/adapters";
50
+
51
+ const anzar = new Anzar(anzarConfig, {
52
+ storage: new LocalStorage(),
53
+ });
54
+ ```
55
+
56
+ ## Basic Usage
57
+ Anzar provides authentication support for email and password.
58
+
59
+ 📝 **Note:** Other methods of authentication will be implemented later
60
+
61
+ ### Sign Up
62
+ To sign up a user you need to call the method register with the user's information.
63
+ ```typescript
64
+ import type { AuthResult } from "anzar/types";
65
+
66
+ try {
67
+ const data: AuthResult = await anzar.Auth.register({ username, email, password });
68
+ console.log(data);
69
+ } catch (e) {
70
+ const message = e.response?.data?.message ?? "An unknown error occurred";
71
+ console.log(message);
72
+ }
73
+ ```
74
+
75
+ 📝 **Note:** By default, the users are automatically signed in after they successfully sign up.
76
+ Disabling this behavior will be implemented later
77
+
78
+ ### Sign In
79
+ To sign in a user you need to call the method login.
80
+ ```typescript
81
+ import type { AuthResult } from "anzar/types";
82
+
83
+ try {
84
+ const data: AuthResult = await anzar.Auth.login({ email, password });
85
+ console.log(data);
86
+ } catch (e) {
87
+ const message = e.response?.data?.message ?? "An unknown error occurred";
88
+ console.log(message);
89
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/index.ts
21
+ var adapters_exports = {};
22
+ __export(adapters_exports, {
23
+ LocalStorage: () => LocalStorage,
24
+ MemoryStorage: () => MemoryStorage
25
+ });
26
+ module.exports = __toCommonJS(adapters_exports);
27
+
28
+ // src/adapters/local_storage.ts
29
+ var LocalStorage = class {
30
+ getToken() {
31
+ return localStorage.getItem("AccessToken");
32
+ }
33
+ getRefreshToken() {
34
+ return localStorage.getItem("RefreshToken");
35
+ }
36
+ onTokenRefresh(tokens) {
37
+ localStorage.setItem("AccessToken", tokens.access);
38
+ localStorage.setItem("RefreshToken", tokens.refresh);
39
+ }
40
+ onSessionExpired() {
41
+ localStorage.clear();
42
+ }
43
+ onLogout() {
44
+ localStorage.clear();
45
+ }
46
+ };
47
+
48
+ // src/adapters/memory_storage.ts
49
+ var MemoryStorage = class {
50
+ acessToken = null;
51
+ refreshToken = null;
52
+ getToken() {
53
+ return this.acessToken;
54
+ }
55
+ getRefreshToken() {
56
+ return this.refreshToken;
57
+ }
58
+ onTokenRefresh(tokens) {
59
+ this.acessToken = tokens.access;
60
+ this.refreshToken = tokens.refresh;
61
+ }
62
+ onSessionExpired() {
63
+ this.acessToken = null;
64
+ this.refreshToken = null;
65
+ }
66
+ onLogout() {
67
+ this.acessToken = null;
68
+ this.refreshToken = null;
69
+ }
70
+ };
@@ -0,0 +1,21 @@
1
+ import { B as BaseStorage, S as SessionTokens } from '../base_storage-BnHsA4fU.cjs';
2
+
3
+ declare class LocalStorage implements BaseStorage {
4
+ getToken(): string | null;
5
+ getRefreshToken(): string | null;
6
+ onTokenRefresh(tokens: SessionTokens): void;
7
+ onSessionExpired?(): void;
8
+ onLogout?(): void;
9
+ }
10
+
11
+ declare class MemoryStorage implements BaseStorage {
12
+ private acessToken;
13
+ private refreshToken;
14
+ getToken(): string | null;
15
+ getRefreshToken(): string | null;
16
+ onTokenRefresh(tokens: SessionTokens): void;
17
+ onSessionExpired?(): void;
18
+ onLogout?(): void;
19
+ }
20
+
21
+ export { LocalStorage, MemoryStorage };
@@ -0,0 +1,21 @@
1
+ import { B as BaseStorage, S as SessionTokens } from '../base_storage-BnHsA4fU.js';
2
+
3
+ declare class LocalStorage implements BaseStorage {
4
+ getToken(): string | null;
5
+ getRefreshToken(): string | null;
6
+ onTokenRefresh(tokens: SessionTokens): void;
7
+ onSessionExpired?(): void;
8
+ onLogout?(): void;
9
+ }
10
+
11
+ declare class MemoryStorage implements BaseStorage {
12
+ private acessToken;
13
+ private refreshToken;
14
+ getToken(): string | null;
15
+ getRefreshToken(): string | null;
16
+ onTokenRefresh(tokens: SessionTokens): void;
17
+ onSessionExpired?(): void;
18
+ onLogout?(): void;
19
+ }
20
+
21
+ export { LocalStorage, MemoryStorage };
@@ -0,0 +1,27 @@
1
+ import {
2
+ MemoryStorage
3
+ } from "../chunk-5SSEPZKA.js";
4
+
5
+ // src/adapters/local_storage.ts
6
+ var LocalStorage = class {
7
+ getToken() {
8
+ return localStorage.getItem("AccessToken");
9
+ }
10
+ getRefreshToken() {
11
+ return localStorage.getItem("RefreshToken");
12
+ }
13
+ onTokenRefresh(tokens) {
14
+ localStorage.setItem("AccessToken", tokens.access);
15
+ localStorage.setItem("RefreshToken", tokens.refresh);
16
+ }
17
+ onSessionExpired() {
18
+ localStorage.clear();
19
+ }
20
+ onLogout() {
21
+ localStorage.clear();
22
+ }
23
+ };
24
+ export {
25
+ LocalStorage,
26
+ MemoryStorage
27
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ /**
13
+ * SessionTokens model
14
+ */
15
+ interface SessionTokens {
16
+ 'access': string;
17
+ 'expires_in': number;
18
+ 'refresh': string;
19
+ 'token_type': string;
20
+ }
21
+
22
+ interface BaseStorage {
23
+ getToken(): string | null;
24
+ getRefreshToken(): string | null;
25
+ onTokenRefresh(tokens: SessionTokens): void;
26
+ onSessionExpired?(): void;
27
+ onLogout?(): void;
28
+ }
29
+
30
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ /**
13
+ * SessionTokens model
14
+ */
15
+ interface SessionTokens {
16
+ 'access': string;
17
+ 'expires_in': number;
18
+ 'refresh': string;
19
+ 'token_type': string;
20
+ }
21
+
22
+ interface BaseStorage {
23
+ getToken(): string | null;
24
+ getRefreshToken(): string | null;
25
+ onTokenRefresh(tokens: SessionTokens): void;
26
+ onSessionExpired?(): void;
27
+ onLogout?(): void;
28
+ }
29
+
30
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,27 @@
1
+ // src/adapters/memory_storage.ts
2
+ var MemoryStorage = class {
3
+ acessToken = null;
4
+ refreshToken = null;
5
+ getToken() {
6
+ return this.acessToken;
7
+ }
8
+ getRefreshToken() {
9
+ return this.refreshToken;
10
+ }
11
+ onTokenRefresh(tokens) {
12
+ this.acessToken = tokens.access;
13
+ this.refreshToken = tokens.refresh;
14
+ }
15
+ onSessionExpired() {
16
+ this.acessToken = null;
17
+ this.refreshToken = null;
18
+ }
19
+ onLogout() {
20
+ this.acessToken = null;
21
+ this.refreshToken = null;
22
+ }
23
+ };
24
+
25
+ export {
26
+ MemoryStorage
27
+ };
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ declare const Role: {
13
+ readonly User: "User";
14
+ readonly Admin: "Admin";
15
+ };
16
+ type Role = typeof Role[keyof typeof Role];
17
+
18
+ /**
19
+ * Anzar Software API
20
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
21
+ *
22
+ * The version of the OpenAPI document: 0.6.2
23
+ * Contact: dev@anzar.io
24
+ *
25
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
26
+ * https://openapi-generator.tech
27
+ * Do not edit the class manually.
28
+ */
29
+
30
+ interface User {
31
+ '_id'?: string | null;
32
+ 'createdAt': string;
33
+ 'email': string;
34
+ 'role': Role;
35
+ 'username': string;
36
+ 'verified': boolean;
37
+ }
38
+
39
+ /**
40
+ * Anzar Software API
41
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
42
+ *
43
+ * The version of the OpenAPI document: 0.6.2
44
+ * Contact: dev@anzar.io
45
+ *
46
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
47
+ * https://openapi-generator.tech
48
+ * Do not edit the class manually.
49
+ */
50
+ /**
51
+ * Verification model
52
+ */
53
+ interface Verification {
54
+ 'link': string;
55
+ 'token': string;
56
+ }
57
+
58
+ interface AuthResult {
59
+ user: User;
60
+ verification?: Verification | null;
61
+ }
62
+
63
+ export type { AuthResult as A, User as U, Verification as V };
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ declare const Role: {
13
+ readonly User: "User";
14
+ readonly Admin: "Admin";
15
+ };
16
+ type Role = typeof Role[keyof typeof Role];
17
+
18
+ /**
19
+ * Anzar Software API
20
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
21
+ *
22
+ * The version of the OpenAPI document: 0.6.2
23
+ * Contact: dev@anzar.io
24
+ *
25
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
26
+ * https://openapi-generator.tech
27
+ * Do not edit the class manually.
28
+ */
29
+
30
+ interface User {
31
+ '_id'?: string | null;
32
+ 'createdAt': string;
33
+ 'email': string;
34
+ 'role': Role;
35
+ 'username': string;
36
+ 'verified': boolean;
37
+ }
38
+
39
+ /**
40
+ * Anzar Software API
41
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
42
+ *
43
+ * The version of the OpenAPI document: 0.6.2
44
+ * Contact: dev@anzar.io
45
+ *
46
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
47
+ * https://openapi-generator.tech
48
+ * Do not edit the class manually.
49
+ */
50
+ /**
51
+ * Verification model
52
+ */
53
+ interface Verification {
54
+ 'link': string;
55
+ 'token': string;
56
+ }
57
+
58
+ interface AuthResult {
59
+ user: User;
60
+ verification?: Verification | null;
61
+ }
62
+
63
+ export type { AuthResult as A, User as U, Verification as V };