@liveartx/authentication 1.0.0 → 1.0.1
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/lib/api/authApiClient.d.ts +6 -1
- package/lib/api/authApiClient.js +2 -0
- package/lib/features/logout.d.ts +2 -0
- package/lib/features/logout.js +8 -0
- package/lib/features/signInOrSignUp.d.ts +9 -4
- package/lib/features/signInOrSignUp.js +4 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/services/cookieService.d.ts +8 -5
- package/lib/services/cookieService.js +48 -47
- package/package.json +1 -1
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
declare class AuthApiClient {
|
|
2
2
|
private readonly apiAuthServiceUrl;
|
|
3
3
|
constructor();
|
|
4
|
-
loginOrSignUpByEmail(email: string): Promise<
|
|
4
|
+
loginOrSignUpByEmail(email: string): Promise<LoginOrSignUpByEmailResponse>;
|
|
5
5
|
verifyCode(email: string, code: string): Promise<VerifyCodeResponse>;
|
|
6
6
|
}
|
|
7
|
+
interface LoginOrSignUpByEmailResponse {
|
|
8
|
+
message: string;
|
|
9
|
+
email: string;
|
|
10
|
+
user_created: boolean;
|
|
11
|
+
}
|
|
7
12
|
interface VerifyCodeResponse {
|
|
8
13
|
access_token: string;
|
|
9
14
|
token_type: string;
|
package/lib/api/authApiClient.js
CHANGED
|
@@ -18,6 +18,8 @@ class AuthApiClient {
|
|
|
18
18
|
body: JSON.stringify({ email }),
|
|
19
19
|
});
|
|
20
20
|
(0, assert_1.assert)(!!response.ok, 'Code sending failed');
|
|
21
|
+
const data = await response.json();
|
|
22
|
+
return data;
|
|
21
23
|
}
|
|
22
24
|
async verifyCode(email, code) {
|
|
23
25
|
const response = await fetch(`${this.apiAuthServiceUrl}/auth/email/verify-code`, {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logout = void 0;
|
|
4
|
+
const cookieService_1 = require("~/services/cookieService");
|
|
5
|
+
function logout(cookieName = 'auth_token') {
|
|
6
|
+
cookieService_1.cookieService.remove(cookieName, { path: '/' });
|
|
7
|
+
}
|
|
8
|
+
exports.logout = logout;
|
|
@@ -5,10 +5,15 @@ interface AuthTokenData {
|
|
|
5
5
|
userId: string;
|
|
6
6
|
email: string;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
success:
|
|
10
|
-
error: string
|
|
11
|
-
}
|
|
8
|
+
declare type SendCodeResult = {
|
|
9
|
+
success: false;
|
|
10
|
+
error: string;
|
|
11
|
+
} | {
|
|
12
|
+
success: true;
|
|
13
|
+
message: string;
|
|
14
|
+
email: string;
|
|
15
|
+
user_created: boolean;
|
|
16
|
+
};
|
|
12
17
|
interface VerifyCodeResult {
|
|
13
18
|
success: boolean;
|
|
14
19
|
tokenData: AuthTokenData | null;
|
|
@@ -43,21 +43,21 @@ function createCookieOptions() {
|
|
|
43
43
|
}
|
|
44
44
|
function saveAuthTokenToCookie(token, cookieName = 'auth_token') {
|
|
45
45
|
const options = createCookieOptions();
|
|
46
|
-
|
|
46
|
+
cookieService_1.cookieService.set(cookieName, token, options);
|
|
47
47
|
}
|
|
48
48
|
async function sendVerificationCode(email, authClient) {
|
|
49
49
|
const validation = validateEmailInput(email);
|
|
50
50
|
if (!validation.valid) {
|
|
51
51
|
return {
|
|
52
52
|
success: false,
|
|
53
|
-
error: validation.error,
|
|
53
|
+
error: validation.error ?? 'Invalid email format',
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
try {
|
|
57
|
-
await authClient.loginOrSignUpByEmail(email);
|
|
57
|
+
const response = await authClient.loginOrSignUpByEmail(email);
|
|
58
58
|
return {
|
|
59
59
|
success: true,
|
|
60
|
-
|
|
60
|
+
...response,
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
63
|
catch (error) {
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -6,9 +6,12 @@ interface CookieOptions {
|
|
|
6
6
|
secure?: boolean;
|
|
7
7
|
sameSite?: 'strict' | 'lax' | 'none';
|
|
8
8
|
}
|
|
9
|
-
declare
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
declare class CookieService {
|
|
10
|
+
private buildCookieString;
|
|
11
|
+
set(name: string, value: string, options?: CookieOptions): void;
|
|
12
|
+
get(name: string): string | null;
|
|
13
|
+
remove(name: string, options?: Omit<CookieOptions, 'expires' | 'maxAge'>): void;
|
|
14
|
+
}
|
|
15
|
+
declare const cookieService: CookieService;
|
|
16
|
+
export { cookieService, CookieService };
|
|
14
17
|
export type { CookieOptions };
|
|
@@ -1,55 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
3
|
+
exports.CookieService = exports.cookieService = void 0;
|
|
4
|
+
class CookieService {
|
|
5
|
+
buildCookieString(name, value, options) {
|
|
6
|
+
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
|
|
7
|
+
if (options?.expires) {
|
|
8
|
+
parts.push(`expires=${options.expires.toUTCString()}`);
|
|
9
|
+
}
|
|
10
|
+
if (options?.maxAge != null) {
|
|
11
|
+
parts.push(`max-age=${options.maxAge}`);
|
|
12
|
+
}
|
|
13
|
+
if (options?.path) {
|
|
14
|
+
parts.push(`path=${options.path}`);
|
|
15
|
+
}
|
|
16
|
+
if (options?.domain) {
|
|
17
|
+
parts.push(`domain=${options.domain}`);
|
|
18
|
+
}
|
|
19
|
+
if (options?.secure) {
|
|
20
|
+
parts.push('secure');
|
|
21
|
+
}
|
|
22
|
+
if (options?.sameSite) {
|
|
23
|
+
parts.push(`samesite=${options.sameSite}`);
|
|
24
|
+
}
|
|
25
|
+
return parts.join('; ');
|
|
23
26
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return;
|
|
27
|
+
set(name, value, options) {
|
|
28
|
+
if (typeof document === 'undefined') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
document.cookie = this.buildCookieString(name, value, options);
|
|
30
32
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
get(name) {
|
|
34
|
+
if (typeof document === 'undefined') {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const nameEQ = `${encodeURIComponent(name)}=`;
|
|
38
|
+
const cookies = document.cookie.split(';');
|
|
39
|
+
for (const cookie of cookies) {
|
|
40
|
+
const trimmed = cookie.trim();
|
|
41
|
+
if (trimmed.startsWith(nameEQ)) {
|
|
42
|
+
return decodeURIComponent(trimmed.substring(nameEQ.length));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
36
45
|
return null;
|
|
37
46
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return decodeURIComponent(trimmed.substring(nameEQ.length));
|
|
44
|
-
}
|
|
47
|
+
remove(name, options) {
|
|
48
|
+
this.set(name, '', {
|
|
49
|
+
...options,
|
|
50
|
+
expires: new Date(0),
|
|
51
|
+
});
|
|
45
52
|
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
exports.getCookie = getCookie;
|
|
49
|
-
function removeCookie(name, options) {
|
|
50
|
-
setCookie(name, '', {
|
|
51
|
-
...options,
|
|
52
|
-
expires: new Date(0),
|
|
53
|
-
});
|
|
54
53
|
}
|
|
55
|
-
exports.
|
|
54
|
+
exports.CookieService = CookieService;
|
|
55
|
+
const cookieService = new CookieService();
|
|
56
|
+
exports.cookieService = cookieService;
|