anzar 1.2.6 → 1.2.8
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 +94 -0
- package/dist/index.cjs +11 -2
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +11 -2
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
=== "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
|
+
```
|
|
17
|
+
|
|
18
|
+
## Create Anzar Auth Instance
|
|
19
|
+
if you are using `vite` as a bundler, add this to your `vite.config.js`
|
|
20
|
+
```javascript title="vite.config.js" linenums="1"
|
|
21
|
+
import yaml from "@rollup/plugin-yaml";
|
|
22
|
+
|
|
23
|
+
export default {
|
|
24
|
+
plugins: [yaml()],
|
|
25
|
+
};
|
|
26
|
+
```
|
|
27
|
+
```bash title="Shell Command"
|
|
28
|
+
$ npm install -D @rollup/plugin-yaml
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
in your main entry file (`main.ts` for example), import Anzar and create your auth instance
|
|
32
|
+
|
|
33
|
+
```typescript title="main.ts" hl_lines="6" linenums="1"
|
|
34
|
+
// Initialize once at application startup
|
|
35
|
+
// The SDK will communicate with your Anzar container at the configured api_url
|
|
36
|
+
import { Anzar } from "anzar";
|
|
37
|
+
import anzarConfig from "anzar.yml";
|
|
38
|
+
|
|
39
|
+
const anzar = Anzar(anzarConfig);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
!!! danger
|
|
43
|
+
Only extend these functionality if you are using **JWT** authentication
|
|
44
|
+
|
|
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
|
+
```
|
|
55
|
+
|
|
56
|
+
## Basic Usage
|
|
57
|
+
Anzar provides authentication support for email and password.
|
|
58
|
+
!!! Notes
|
|
59
|
+
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
|
+
try {
|
|
65
|
+
const response = await anzar.Auth.register({ username, email, password });
|
|
66
|
+
const data: AuthResponse = response.data;
|
|
67
|
+
if (data.tokens) {
|
|
68
|
+
// Store securely the `access`,`refresh` tokens
|
|
69
|
+
}
|
|
70
|
+
} catch (e) {
|
|
71
|
+
const message = e.response?.data?.message ?? "An unknown error occurred";
|
|
72
|
+
console.log(message);
|
|
73
|
+
}
|
|
74
|
+
```
|
|
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
|
+
|
|
80
|
+
Disabling this behavior will be implemented later
|
|
81
|
+
|
|
82
|
+
### Sign In
|
|
83
|
+
To sign in a user you need to call the method login.
|
|
84
|
+
```typescript
|
|
85
|
+
try {
|
|
86
|
+
const response = await anzar.Auth.login({ email, password });
|
|
87
|
+
const data: AuthResponse = response.data;
|
|
88
|
+
if (data.tokens) {
|
|
89
|
+
// Store securely the `access`,`refresh` tokens
|
|
90
|
+
}
|
|
91
|
+
} catch (e) {
|
|
92
|
+
const message = e.response?.data?.message ?? "An unknown error occurred";
|
|
93
|
+
console.log(message);
|
|
94
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -753,7 +753,7 @@ var Configuration = class {
|
|
|
753
753
|
// src/http/jwt_interceptor.ts
|
|
754
754
|
var JwtInterceptor = class {
|
|
755
755
|
apply(axiosInstance, options) {
|
|
756
|
-
const REFRESH_URI = "/auth/
|
|
756
|
+
const REFRESH_URI = "/auth/refresh-token";
|
|
757
757
|
axiosInstance.interceptors.request.use(
|
|
758
758
|
(config) => {
|
|
759
759
|
const accessToken = options?.getToken();
|
|
@@ -813,6 +813,7 @@ function Anzar(anzarConfig, options) {
|
|
|
813
813
|
new JwtInterceptor().apply(axiosInstance, options);
|
|
814
814
|
}
|
|
815
815
|
const generatedAuthApi = new AuthApi(configuration, basePath, axiosInstance);
|
|
816
|
+
const generatedUserApi = new UsersApi(configuration, basePath, axiosInstance);
|
|
816
817
|
return {
|
|
817
818
|
Auth: {
|
|
818
819
|
login: (body) => generatedAuthApi.login(body),
|
|
@@ -826,9 +827,17 @@ function Anzar(anzarConfig, options) {
|
|
|
826
827
|
},
|
|
827
828
|
...anzarConfig.auth?.strategy === "Session" /* Session */ && {
|
|
828
829
|
session: () => generatedAuthApi.getSession()
|
|
830
|
+
},
|
|
831
|
+
isAuthenticated: async () => {
|
|
832
|
+
try {
|
|
833
|
+
const response = await generatedUserApi.findUser();
|
|
834
|
+
return !!response.data;
|
|
835
|
+
} catch {
|
|
836
|
+
return false;
|
|
837
|
+
}
|
|
829
838
|
}
|
|
830
839
|
},
|
|
831
|
-
User:
|
|
840
|
+
User: generatedUserApi,
|
|
832
841
|
Email: new EmailApi(configuration, basePath, axiosInstance)
|
|
833
842
|
};
|
|
834
843
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -399,6 +399,7 @@ interface SdkOptions {
|
|
|
399
399
|
|
|
400
400
|
declare function Anzar(anzarConfig: AnzarConfig, options?: SdkOptions): {
|
|
401
401
|
Auth: {
|
|
402
|
+
isAuthenticated: () => Promise<boolean>;
|
|
402
403
|
session?: (() => Promise<axios.AxiosResponse<Session, any, {}>>) | undefined;
|
|
403
404
|
login: (body: LoginRequest) => Promise<axios.AxiosResponse<AuthResponse, any, {}>>;
|
|
404
405
|
register: (body: RegisterRequest) => Promise<axios.AxiosResponse<AuthResponse, any, {}>>;
|
package/dist/index.d.ts
CHANGED
|
@@ -399,6 +399,7 @@ interface SdkOptions {
|
|
|
399
399
|
|
|
400
400
|
declare function Anzar(anzarConfig: AnzarConfig, options?: SdkOptions): {
|
|
401
401
|
Auth: {
|
|
402
|
+
isAuthenticated: () => Promise<boolean>;
|
|
402
403
|
session?: (() => Promise<axios.AxiosResponse<Session, any, {}>>) | undefined;
|
|
403
404
|
login: (body: LoginRequest) => Promise<axios.AxiosResponse<AuthResponse, any, {}>>;
|
|
404
405
|
register: (body: RegisterRequest) => Promise<axios.AxiosResponse<AuthResponse, any, {}>>;
|
package/dist/index.js
CHANGED
|
@@ -719,7 +719,7 @@ var Configuration = class {
|
|
|
719
719
|
// src/http/jwt_interceptor.ts
|
|
720
720
|
var JwtInterceptor = class {
|
|
721
721
|
apply(axiosInstance, options) {
|
|
722
|
-
const REFRESH_URI = "/auth/
|
|
722
|
+
const REFRESH_URI = "/auth/refresh-token";
|
|
723
723
|
axiosInstance.interceptors.request.use(
|
|
724
724
|
(config) => {
|
|
725
725
|
const accessToken = options?.getToken();
|
|
@@ -779,6 +779,7 @@ function Anzar(anzarConfig, options) {
|
|
|
779
779
|
new JwtInterceptor().apply(axiosInstance, options);
|
|
780
780
|
}
|
|
781
781
|
const generatedAuthApi = new AuthApi(configuration, basePath, axiosInstance);
|
|
782
|
+
const generatedUserApi = new UsersApi(configuration, basePath, axiosInstance);
|
|
782
783
|
return {
|
|
783
784
|
Auth: {
|
|
784
785
|
login: (body) => generatedAuthApi.login(body),
|
|
@@ -792,9 +793,17 @@ function Anzar(anzarConfig, options) {
|
|
|
792
793
|
},
|
|
793
794
|
...anzarConfig.auth?.strategy === "Session" /* Session */ && {
|
|
794
795
|
session: () => generatedAuthApi.getSession()
|
|
796
|
+
},
|
|
797
|
+
isAuthenticated: async () => {
|
|
798
|
+
try {
|
|
799
|
+
const response = await generatedUserApi.findUser();
|
|
800
|
+
return !!response.data;
|
|
801
|
+
} catch {
|
|
802
|
+
return false;
|
|
803
|
+
}
|
|
795
804
|
}
|
|
796
805
|
},
|
|
797
|
-
User:
|
|
806
|
+
User: generatedUserApi,
|
|
798
807
|
Email: new EmailApi(configuration, basePath, axiosInstance)
|
|
799
808
|
};
|
|
800
809
|
}
|