@liveartx/authentication 1.0.0 → 1.0.2
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/getAuthToken.d.ts +1 -0
- package/lib/features/getAuthToken.js +8 -0
- package/lib/features/logout.d.ts +2 -0
- package/lib/features/logout.js +8 -0
- package/lib/features/signInOrSignUp.d.ts +11 -7
- package/lib/features/signInOrSignUp.js +9 -6
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -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 @@
|
|
|
1
|
+
export declare function getAuthToken(cookieName?: string): string | null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAuthToken = void 0;
|
|
4
|
+
const cookieService_1 = require("../services/cookieService");
|
|
5
|
+
function getAuthToken(cookieName = 'auth_token') {
|
|
6
|
+
return cookieService_1.cookieService.get(cookieName);
|
|
7
|
+
}
|
|
8
|
+
exports.getAuthToken = getAuthToken;
|
|
@@ -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;
|
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import { AuthApiClient } from '~/api/authApiClient';
|
|
2
1
|
interface AuthTokenData {
|
|
3
2
|
accessToken: string;
|
|
4
3
|
tokenType: string;
|
|
5
4
|
userId: string;
|
|
6
5
|
email: string;
|
|
7
6
|
}
|
|
8
|
-
|
|
9
|
-
success:
|
|
10
|
-
error: string
|
|
11
|
-
}
|
|
7
|
+
declare type SendCodeResult = {
|
|
8
|
+
success: false;
|
|
9
|
+
error: string;
|
|
10
|
+
} | {
|
|
11
|
+
success: true;
|
|
12
|
+
message: string;
|
|
13
|
+
email: string;
|
|
14
|
+
user_created: boolean;
|
|
15
|
+
};
|
|
12
16
|
interface VerifyCodeResult {
|
|
13
17
|
success: boolean;
|
|
14
18
|
tokenData: AuthTokenData | null;
|
|
15
19
|
error: string | null;
|
|
16
20
|
}
|
|
17
|
-
declare function sendVerificationCode(email: string
|
|
18
|
-
declare function verifyCodeAndSaveToken(email: string, code: string,
|
|
21
|
+
declare function sendVerificationCode(email: string): Promise<SendCodeResult>;
|
|
22
|
+
declare function verifyCodeAndSaveToken(email: string, code: string, cookieName?: string): Promise<VerifyCodeResult>;
|
|
19
23
|
export { sendVerificationCode, verifyCodeAndSaveToken };
|
|
20
24
|
export type { AuthTokenData, SendCodeResult, VerifyCodeResult };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.verifyCodeAndSaveToken = exports.sendVerificationCode = void 0;
|
|
4
|
+
const authApiClient_1 = require("~/api/authApiClient");
|
|
4
5
|
const cookieService_1 = require("~/services/cookieService");
|
|
5
6
|
function validateEmail(email) {
|
|
6
7
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
@@ -43,21 +44,22 @@ function createCookieOptions() {
|
|
|
43
44
|
}
|
|
44
45
|
function saveAuthTokenToCookie(token, cookieName = 'auth_token') {
|
|
45
46
|
const options = createCookieOptions();
|
|
46
|
-
|
|
47
|
+
cookieService_1.cookieService.set(cookieName, token, options);
|
|
47
48
|
}
|
|
48
|
-
async function sendVerificationCode(email
|
|
49
|
+
async function sendVerificationCode(email) {
|
|
49
50
|
const validation = validateEmailInput(email);
|
|
50
51
|
if (!validation.valid) {
|
|
51
52
|
return {
|
|
52
53
|
success: false,
|
|
53
|
-
error: validation.error,
|
|
54
|
+
error: validation.error ?? 'Invalid email format',
|
|
54
55
|
};
|
|
55
56
|
}
|
|
56
57
|
try {
|
|
57
|
-
|
|
58
|
+
const authClient = new authApiClient_1.AuthApiClient();
|
|
59
|
+
const response = await authClient.loginOrSignUpByEmail(email);
|
|
58
60
|
return {
|
|
59
61
|
success: true,
|
|
60
|
-
|
|
62
|
+
...response,
|
|
61
63
|
};
|
|
62
64
|
}
|
|
63
65
|
catch (error) {
|
|
@@ -68,7 +70,7 @@ async function sendVerificationCode(email, authClient) {
|
|
|
68
70
|
}
|
|
69
71
|
}
|
|
70
72
|
exports.sendVerificationCode = sendVerificationCode;
|
|
71
|
-
async function verifyCodeAndSaveToken(email, code,
|
|
73
|
+
async function verifyCodeAndSaveToken(email, code, cookieName) {
|
|
72
74
|
const validation = validateVerifyInput(email, code);
|
|
73
75
|
if (!validation.valid) {
|
|
74
76
|
return {
|
|
@@ -78,6 +80,7 @@ async function verifyCodeAndSaveToken(email, code, authClient, cookieName) {
|
|
|
78
80
|
};
|
|
79
81
|
}
|
|
80
82
|
try {
|
|
83
|
+
const authClient = new authApiClient_1.AuthApiClient();
|
|
81
84
|
const response = await authClient.verifyCode(email, code);
|
|
82
85
|
const tokenData = transformVerifyCodeResponse(response);
|
|
83
86
|
saveAuthTokenToCookie(tokenData.accessToken, cookieName);
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -15,3 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./features/signInOrSignUp"), exports);
|
|
18
|
+
__exportStar(require("./features/logout"), exports);
|
|
19
|
+
__exportStar(require("./features/getAuthToken"), exports);
|
|
@@ -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;
|