kodzero-front-sdk-alfa 0.0.5 → 0.0.7
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 +6 -6
- package/dist/auth/base.d.ts +3 -3
- package/dist/auth/base.js +3 -3
- package/dist/auth/email.d.ts +38 -8
- package/dist/auth/email.js +13 -13
- package/dist/auth/index.js +3 -3
- package/package.json +1 -1
- package/src/auth/base.ts +3 -3
- package/src/auth/email.ts +61 -25
- package/src/auth/index.ts +3 -3
package/README.md
CHANGED
|
@@ -18,14 +18,14 @@ const kodzero = new Kodzero({
|
|
|
18
18
|
host: 'https://api.your-backend.com'
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
//
|
|
22
|
-
const
|
|
21
|
+
// New user registration
|
|
22
|
+
const registerResult = await kodzero.auth.register({
|
|
23
23
|
email: 'user@example.com',
|
|
24
24
|
password: 'secure_password'
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
const
|
|
27
|
+
// Login
|
|
28
|
+
const loginResult = await kodzero.auth.login({
|
|
29
29
|
email: 'user@example.com',
|
|
30
30
|
password: 'secure_password'
|
|
31
31
|
});
|
|
@@ -36,8 +36,8 @@ const verified = await kodzero.auth.verify();
|
|
|
36
36
|
// Refresh the token
|
|
37
37
|
const refreshed = await kodzero.auth.refresh();
|
|
38
38
|
|
|
39
|
-
//
|
|
40
|
-
await kodzero.auth.
|
|
39
|
+
// Logout
|
|
40
|
+
await kodzero.auth.logout();
|
|
41
41
|
|
|
42
42
|
// Manually set tokens (useful for persisting sessions)
|
|
43
43
|
kodzero.auth.setTokens('access_token', 'refresh_token');
|
package/dist/auth/base.d.ts
CHANGED
|
@@ -7,10 +7,10 @@ declare class KodzeroAuthBase {
|
|
|
7
7
|
tokensManager: TokensManagerClass;
|
|
8
8
|
constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass);
|
|
9
9
|
_handleApiError(response: Response): Promise<void>;
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
login: (...args: any[]) => Promise<any> | void;
|
|
11
|
+
register: (...args: any[]) => Promise<any> | void;
|
|
12
12
|
refresh: (...args: any[]) => Promise<any> | void;
|
|
13
|
-
|
|
13
|
+
logout: (...args: any[]) => Promise<any> | void;
|
|
14
14
|
verify: (...args: any[]) => Promise<any> | void;
|
|
15
15
|
}
|
|
16
16
|
export default KodzeroAuthBase;
|
package/dist/auth/base.js
CHANGED
|
@@ -13,10 +13,10 @@ class KodzeroAuthBase {
|
|
|
13
13
|
_handleApiError(response) {
|
|
14
14
|
return validateApiResponse(response);
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
login = (...args) => { };
|
|
17
|
+
register = (...args) => { };
|
|
18
18
|
refresh = (...args) => { };
|
|
19
|
-
|
|
19
|
+
logout = (...args) => { };
|
|
20
20
|
verify = (...args) => { };
|
|
21
21
|
}
|
|
22
22
|
export default KodzeroAuthBase;
|
package/dist/auth/email.d.ts
CHANGED
|
@@ -2,22 +2,52 @@ import FluidFetch from "fluid-fetch";
|
|
|
2
2
|
import { AuthOptions } from "./index.js";
|
|
3
3
|
import KodzeroAuthBase from "./base.js";
|
|
4
4
|
import TokensManagerClass from "./tokens.js";
|
|
5
|
-
interface
|
|
5
|
+
interface KodzeroAuthEmailLogin {
|
|
6
6
|
email: string;
|
|
7
7
|
password: string;
|
|
8
8
|
}
|
|
9
|
+
interface SuccessResponse<Result> {
|
|
10
|
+
ok: true;
|
|
11
|
+
result: Result;
|
|
12
|
+
}
|
|
13
|
+
interface Tokens {
|
|
14
|
+
access: string;
|
|
15
|
+
refresh: string;
|
|
16
|
+
}
|
|
17
|
+
interface AuthUser {
|
|
18
|
+
_id: string;
|
|
19
|
+
email: string;
|
|
20
|
+
name: string;
|
|
21
|
+
workspace: string;
|
|
22
|
+
createdAt: string;
|
|
23
|
+
updatedAt: string;
|
|
24
|
+
[key: string]: any;
|
|
25
|
+
}
|
|
26
|
+
type RegisterResponse = SuccessResponse<{
|
|
27
|
+
user: AuthUser;
|
|
28
|
+
tokens: Tokens;
|
|
29
|
+
session: number;
|
|
30
|
+
}>;
|
|
31
|
+
type LoginResponse = SuccessResponse<{
|
|
32
|
+
user: AuthUser;
|
|
33
|
+
tokens: Tokens;
|
|
34
|
+
session: number;
|
|
35
|
+
}>;
|
|
36
|
+
type VerifyResponse = SuccessResponse<{}>;
|
|
37
|
+
type RefreshResponse = SuccessResponse<{
|
|
38
|
+
tokens: Tokens;
|
|
39
|
+
}>;
|
|
9
40
|
declare class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
10
41
|
tokensManager: TokensManagerClass;
|
|
11
42
|
collection: "auth/password";
|
|
12
43
|
constructor(options: AuthOptions, api: typeof FluidFetch, tokensManager: TokensManagerClass);
|
|
13
44
|
_setTokens: (access: string, refresh?: string) => void;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
45
|
+
login: (input: KodzeroAuthEmailLogin) => Promise<LoginResponse["result"]>;
|
|
46
|
+
register: (userData: Record<string, string>) => Promise<RegisterResponse["result"]>;
|
|
47
|
+
verify: () => Promise<VerifyResponse["result"]>;
|
|
48
|
+
refresh: () => Promise<RefreshResponse["result"] | {
|
|
49
|
+
tokens: null;
|
|
17
50
|
}>;
|
|
18
|
-
|
|
19
|
-
verify: () => Promise<any>;
|
|
20
|
-
refresh: () => Promise<any>;
|
|
21
|
-
signout: () => Promise<any>;
|
|
51
|
+
logout: () => Promise<boolean>;
|
|
22
52
|
}
|
|
23
53
|
export default KodzeroAuthEmail;
|
package/dist/auth/email.js
CHANGED
|
@@ -13,19 +13,19 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
|
13
13
|
if (refresh)
|
|
14
14
|
this.tokensManager.setRefresh(refresh);
|
|
15
15
|
};
|
|
16
|
-
|
|
17
|
-
const url = buildURL(this.host, this.collection + '/
|
|
16
|
+
login = async (input) => {
|
|
17
|
+
const url = buildURL(this.host, this.collection + '/login');
|
|
18
18
|
const response = await this.api.post(url, input)
|
|
19
19
|
.headers({ 'Content-Type': 'application/json' });
|
|
20
20
|
await this._handleApiError(response);
|
|
21
21
|
const json = await response.json();
|
|
22
|
-
if (json.ok && json.
|
|
23
|
-
this._setTokens(json.tokens.access, json.tokens.refresh);
|
|
22
|
+
if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
|
|
23
|
+
this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
|
|
24
24
|
}
|
|
25
|
-
return json.
|
|
25
|
+
return json.result;
|
|
26
26
|
};
|
|
27
|
-
|
|
28
|
-
const url = buildURL(this.host, this.collection + '/
|
|
27
|
+
register = async (userData) => {
|
|
28
|
+
const url = buildURL(this.host, this.collection + '/register');
|
|
29
29
|
const response = await this.api.post(url, userData)
|
|
30
30
|
.headers({ 'Content-Type': 'application/json' });
|
|
31
31
|
await this._handleApiError(response);
|
|
@@ -33,7 +33,7 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
|
33
33
|
if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
|
|
34
34
|
this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
|
|
35
35
|
}
|
|
36
|
-
return json.result
|
|
36
|
+
return json.result;
|
|
37
37
|
};
|
|
38
38
|
verify = async () => {
|
|
39
39
|
try {
|
|
@@ -53,13 +53,13 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
|
53
53
|
.headers({ 'Content-Type': 'application/json' });
|
|
54
54
|
await this._handleApiError(response);
|
|
55
55
|
const json = await response.json();
|
|
56
|
-
if (json.ok && json.tokens && json.tokens.access) {
|
|
57
|
-
this.tokensManager.setAccess(json.tokens.access);
|
|
56
|
+
if (json.ok && json.result?.tokens && json.result.tokens.access) {
|
|
57
|
+
this.tokensManager.setAccess(json.result.tokens.access);
|
|
58
58
|
}
|
|
59
|
-
return json.ok ?
|
|
59
|
+
return json.ok ? { tokens: json.result.tokens } : { tokens: null };
|
|
60
60
|
};
|
|
61
|
-
|
|
62
|
-
const url = buildURL(this.host, this.collection + '/
|
|
61
|
+
logout = async () => {
|
|
62
|
+
const url = buildURL(this.host, this.collection + '/logout');
|
|
63
63
|
const response = await this.api.post(url, {})
|
|
64
64
|
.headers({ 'Content-Type': 'application/json' });
|
|
65
65
|
await this._handleApiError(response);
|
package/dist/auth/index.js
CHANGED
|
@@ -7,9 +7,9 @@ class KodzeroAuth extends KodzeroAuthBase {
|
|
|
7
7
|
constructor(options, api, tokensManager) {
|
|
8
8
|
super(options, api, tokensManager);
|
|
9
9
|
this.email = new KodzeroAuthEmail(options, api, tokensManager);
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
10
|
+
this.login = this.email.login;
|
|
11
|
+
this.register = this.email.register;
|
|
12
|
+
this.logout = this.email.logout;
|
|
13
13
|
this.verify = this.email.verify;
|
|
14
14
|
this.refresh = this.email.refresh;
|
|
15
15
|
this.setTokens = (access, refresh) => {
|
package/package.json
CHANGED
package/src/auth/base.ts
CHANGED
|
@@ -29,10 +29,10 @@ class KodzeroAuthBase {
|
|
|
29
29
|
* Base auth methods.
|
|
30
30
|
* These will be overridden by specific strategies (e.g. email, social, etc.)
|
|
31
31
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
login = (...args: any[]): Promise<any> | void => {}
|
|
33
|
+
register = (...args: any[]): Promise<any> | void => {}
|
|
34
34
|
refresh = (...args: any[]): Promise<any> | void => {}
|
|
35
|
-
|
|
35
|
+
logout = (...args: any[]): Promise<any> | void => {}
|
|
36
36
|
verify = (...args: any[]): Promise<any> | void => {}
|
|
37
37
|
}
|
|
38
38
|
|
package/src/auth/email.ts
CHANGED
|
@@ -4,11 +4,47 @@ import buildURL from "../utils/buildURL.js"
|
|
|
4
4
|
import KodzeroAuthBase from "./base.js"
|
|
5
5
|
import TokensManagerClass from "./tokens.js"
|
|
6
6
|
|
|
7
|
-
interface
|
|
7
|
+
interface KodzeroAuthEmailLogin {
|
|
8
8
|
email: string
|
|
9
9
|
password: string
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
interface SuccessResponse<Result> {
|
|
13
|
+
ok: true
|
|
14
|
+
result: Result
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Tokens {
|
|
18
|
+
access: string
|
|
19
|
+
refresh: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface AuthUser {
|
|
23
|
+
_id: string
|
|
24
|
+
email: string
|
|
25
|
+
name: string
|
|
26
|
+
workspace: string
|
|
27
|
+
createdAt: string
|
|
28
|
+
updatedAt: string
|
|
29
|
+
[key: string]: any
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type RegisterResponse = SuccessResponse<{
|
|
33
|
+
user: AuthUser
|
|
34
|
+
tokens: Tokens
|
|
35
|
+
session: number
|
|
36
|
+
}>
|
|
37
|
+
|
|
38
|
+
type LoginResponse = SuccessResponse<{
|
|
39
|
+
user: AuthUser
|
|
40
|
+
tokens: Tokens
|
|
41
|
+
session: number
|
|
42
|
+
}>
|
|
43
|
+
|
|
44
|
+
type VerifyResponse = SuccessResponse<{}>
|
|
45
|
+
type RefreshResponse = SuccessResponse<{tokens: Tokens}>
|
|
46
|
+
type LogoutResponse = SuccessResponse<boolean>
|
|
47
|
+
|
|
12
48
|
class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
13
49
|
tokensManager: TokensManagerClass
|
|
14
50
|
collection: "auth/password"
|
|
@@ -30,50 +66,50 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
|
30
66
|
}
|
|
31
67
|
|
|
32
68
|
/**
|
|
33
|
-
*
|
|
69
|
+
* Login with email and password. On success, sets tokens in TokensManager automatically
|
|
34
70
|
*/
|
|
35
|
-
|
|
36
|
-
const url = buildURL(this.host, this.collection + '/
|
|
37
|
-
|
|
71
|
+
login = async (input: KodzeroAuthEmailLogin): Promise<LoginResponse['result']> => {
|
|
72
|
+
const url = buildURL(this.host, this.collection + '/login')
|
|
73
|
+
|
|
38
74
|
const response = await this.api.post(url, input)
|
|
39
75
|
.headers({ 'Content-Type': 'application/json' });
|
|
40
76
|
|
|
41
77
|
await this._handleApiError(response);
|
|
42
|
-
const json = await response.json();
|
|
78
|
+
const json: LoginResponse = await response.json();
|
|
43
79
|
|
|
44
|
-
if (json.ok && json.
|
|
45
|
-
this._setTokens(json.tokens.access, json.tokens.refresh);
|
|
80
|
+
if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
|
|
81
|
+
this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
|
|
46
82
|
}
|
|
47
83
|
|
|
48
|
-
return json.
|
|
84
|
+
return json.result
|
|
49
85
|
}
|
|
50
86
|
|
|
51
87
|
/**
|
|
52
|
-
*
|
|
88
|
+
* Register with email and password. On success, sets tokens in TokensManager automatically
|
|
53
89
|
*/
|
|
54
|
-
|
|
55
|
-
const url = buildURL(this.host, this.collection + '/
|
|
90
|
+
register = async (userData: Record<string, string>): Promise<RegisterResponse['result']> => {
|
|
91
|
+
const url = buildURL(this.host, this.collection + '/register')
|
|
56
92
|
const response = await this.api.post(url, userData)
|
|
57
93
|
.headers({ 'Content-Type': 'application/json' });
|
|
58
94
|
|
|
59
95
|
await this._handleApiError(response);
|
|
60
|
-
const json = await response.json();
|
|
96
|
+
const json: RegisterResponse = await response.json();
|
|
61
97
|
|
|
62
98
|
if (json.ok && json.result && json.result.tokens.access && json.result.tokens.refresh) {
|
|
63
99
|
this._setTokens(json.result.tokens.access, json.result.tokens.refresh);
|
|
64
100
|
}
|
|
65
101
|
|
|
66
|
-
return json.result
|
|
102
|
+
return json.result
|
|
67
103
|
}
|
|
68
104
|
|
|
69
105
|
/**
|
|
70
106
|
* Verify current access token
|
|
71
107
|
*/
|
|
72
|
-
verify = async (): Promise<
|
|
108
|
+
verify = async (): Promise<VerifyResponse['result']> => {
|
|
73
109
|
try {
|
|
74
110
|
const url = buildURL(this.host, this.collection + '/verify')
|
|
75
111
|
const response = await this.api.get(url)
|
|
76
|
-
const json = await response.json();
|
|
112
|
+
const json: VerifyResponse = await response.json();
|
|
77
113
|
|
|
78
114
|
return json.ok ? true : false
|
|
79
115
|
} catch (error) {
|
|
@@ -86,32 +122,32 @@ class KodzeroAuthEmail extends KodzeroAuthBase {
|
|
|
86
122
|
* Refresh access token using refresh token
|
|
87
123
|
* If success, updates access token in TokensManager automatically
|
|
88
124
|
*/
|
|
89
|
-
refresh = async (): Promise<
|
|
125
|
+
refresh = async (): Promise<RefreshResponse['result'] | {tokens: null}> => {
|
|
90
126
|
const url = buildURL(this.host, this.collection + '/refresh')
|
|
91
127
|
const response = await this.api.post(url, { refresh: this.tokensManager.refresh })
|
|
92
128
|
.headers({ 'Content-Type': 'application/json' });
|
|
93
129
|
|
|
94
130
|
await this._handleApiError(response);
|
|
95
|
-
const json = await response.json();
|
|
131
|
+
const json: RefreshResponse = await response.json();
|
|
96
132
|
|
|
97
|
-
if (json.ok && json.tokens && json.tokens.access) {
|
|
98
|
-
this.tokensManager.setAccess(json.tokens.access)
|
|
133
|
+
if (json.ok && json.result?.tokens && json.result.tokens.access) {
|
|
134
|
+
this.tokensManager.setAccess(json.result.tokens.access)
|
|
99
135
|
}
|
|
100
136
|
|
|
101
|
-
return json.ok ?
|
|
137
|
+
return json.ok ? { tokens: json.result.tokens } : { tokens: null }
|
|
102
138
|
}
|
|
103
139
|
|
|
104
140
|
/**
|
|
105
|
-
*
|
|
141
|
+
* Logout the user
|
|
106
142
|
* If success, clears tokens in TokensManager automatically
|
|
107
143
|
*/
|
|
108
|
-
|
|
109
|
-
const url = buildURL(this.host, this.collection + '/
|
|
144
|
+
logout = async (): Promise<boolean> => {
|
|
145
|
+
const url = buildURL(this.host, this.collection + '/logout')
|
|
110
146
|
const response = await this.api.post(url, {})
|
|
111
147
|
.headers({ 'Content-Type': 'application/json' });
|
|
112
148
|
|
|
113
149
|
await this._handleApiError(response);
|
|
114
|
-
const json = await response.json();
|
|
150
|
+
const json: LogoutResponse = await response.json();
|
|
115
151
|
|
|
116
152
|
if (json.ok) this.tokensManager.clear()
|
|
117
153
|
|
package/src/auth/index.ts
CHANGED
|
@@ -19,9 +19,9 @@ class KodzeroAuth extends KodzeroAuthBase {
|
|
|
19
19
|
this.email = new KodzeroAuthEmail(options, api, tokensManager)
|
|
20
20
|
|
|
21
21
|
// Default methods set to email strategy methods
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
22
|
+
this.login = this.email.login
|
|
23
|
+
this.register = this.email.register
|
|
24
|
+
this.logout = this.email.logout
|
|
25
25
|
this.verify = this.email.verify
|
|
26
26
|
this.refresh = this.email.refresh
|
|
27
27
|
|