anzar 1.2.8 → 1.2.10

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 CHANGED
@@ -2,61 +2,61 @@
2
2
 
3
3
  ## Install The Typescript SDK
4
4
  In a ts project run the following command to install the anzar package.
5
- === "npm"
6
- ```bash
7
- $ npm install anzar
8
- ```
9
- === "pnpm"
10
- ```bash
11
- $ pnpm install anzar
12
- ```
13
- === "yarn"
14
- ```bash
15
- $ yarn add anzar
16
- ```
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
+ ```
17
20
 
18
21
  ## Create Anzar Auth Instance
19
22
  if you are using `vite` as a bundler, add this to your `vite.config.js`
20
- ```javascript title="vite.config.js" linenums="1"
23
+ ```javascript
21
24
  import yaml from "@rollup/plugin-yaml";
22
25
 
23
26
  export default {
24
27
  plugins: [yaml()],
25
28
  };
26
29
  ```
27
- ```bash title="Shell Command"
30
+ ```bash
28
31
  $ npm install -D @rollup/plugin-yaml
29
32
  ```
30
33
 
31
34
  in your main entry file (`main.ts` for example), import Anzar and create your auth instance
32
35
 
33
- ```typescript title="main.ts" hl_lines="6" linenums="1"
36
+ ```typescript
34
37
  // Initialize once at application startup
35
38
  // The SDK will communicate with your Anzar container at the configured api_url
36
39
  import { Anzar } from "anzar";
37
40
  import anzarConfig from "anzar.yml";
38
41
 
39
- const anzar = Anzar(anzarConfig);
42
+ const anzar = new Anzar(anzarConfig);
40
43
  ```
41
44
 
42
- !!! danger
43
- Only extend these functionality if you are using **JWT** authentication
45
+ ⚠️ **Note**
46
+ Choose your token storage adapter
47
+
48
+ ```typescript
49
+ import { LocalStorage } from "anzar/adapters";
44
50
 
45
- :warning: Implement a secure Storage solution
46
- ```typescript linenums="1"
47
- import { SessionTokens } from "anzar.types";
48
- const anzar = Anzar(anzarConfig, {
49
- getToken: () => "AccessToken",
50
- getRefreshToken: () => "RefreshToken",
51
- onTokenRefresh(tokens) => //store tokens.access, tokens.refresh,
52
- ...
53
- });
54
- ```
51
+ const anzar = new Anzar(anzarConfig, {
52
+ storage: new LocalStorage(),
53
+ });
54
+ ```
55
55
 
56
56
  ## Basic Usage
57
57
  Anzar provides authentication support for email and password.
58
- !!! Notes
59
- Other methods of authentication will be implemented later
58
+
59
+ 📝 **Note:** Other methods of authentication will be implemented later
60
60
 
61
61
  ### Sign Up
62
62
  To sign up a user you need to call the method register with the user's information.
@@ -64,20 +64,14 @@ To sign up a user you need to call the method register with the user's informati
64
64
  try {
65
65
  const response = await anzar.Auth.register({ username, email, password });
66
66
  const data: AuthResponse = response.data;
67
- if (data.tokens) {
68
- // Store securely the `access`,`refresh` tokens
69
- }
70
67
  } catch (e) {
71
68
  const message = e.response?.data?.message ?? "An unknown error occurred";
72
69
  console.log(message);
73
70
  }
74
71
  ```
75
- See [`AuthResponse`](../reference#authresponse) for the full type definition.
76
-
77
- !!! notes "Notes"
78
- By default, the users are automatically signed in after they successfully sign up.
79
72
 
80
- Disabling this behavior will be implemented later
73
+ 📝 **Note:** By default, the users are automatically signed in after they successfully sign up.
74
+ Disabling this behavior will be implemented later
81
75
 
82
76
  ### Sign In
83
77
  To sign in a user you need to call the method login.
@@ -85,9 +79,6 @@ To sign in a user you need to call the method login.
85
79
  try {
86
80
  const response = await anzar.Auth.login({ email, password });
87
81
  const data: AuthResponse = response.data;
88
- if (data.tokens) {
89
- // Store securely the `access`,`refresh` tokens
90
- }
91
82
  } catch (e) {
92
83
  const message = e.response?.data?.message ?? "An unknown error occurred";
93
84
  console.log(message);
@@ -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-CQKHFbrb.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-CQKHFbrb.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,25 @@
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
+ interface SessionTokens {
13
+ 'access': string;
14
+ 'refresh': string;
15
+ }
16
+
17
+ interface BaseStorage {
18
+ getToken(): string | null;
19
+ getRefreshToken(): string | null;
20
+ onTokenRefresh(tokens: SessionTokens): void;
21
+ onSessionExpired?(): void;
22
+ onLogout?(): void;
23
+ }
24
+
25
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,25 @@
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
+ interface SessionTokens {
13
+ 'access': string;
14
+ 'refresh': string;
15
+ }
16
+
17
+ interface BaseStorage {
18
+ getToken(): string | null;
19
+ getRefreshToken(): string | null;
20
+ onTokenRefresh(tokens: SessionTokens): void;
21
+ onSessionExpired?(): void;
22
+ onLogout?(): void;
23
+ }
24
+
25
+ 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
+ };