@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.
@@ -1,9 +1,14 @@
1
1
  declare class AuthApiClient {
2
2
  private readonly apiAuthServiceUrl;
3
3
  constructor();
4
- loginOrSignUpByEmail(email: string): Promise<void>;
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;
@@ -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,2 @@
1
+ declare function logout(cookieName?: string): void;
2
+ export { logout };
@@ -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
- interface SendCodeResult {
9
- success: boolean;
10
- error: string | null;
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
- (0, cookieService_1.setCookie)(cookieName, token, options);
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
- error: null,
60
+ ...response,
61
61
  };
62
62
  }
63
63
  catch (error) {
package/lib/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './features/signInOrSignUp';
2
+ export * from './features/logout';
package/lib/index.js CHANGED
@@ -15,3 +15,4 @@ 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);
@@ -6,9 +6,12 @@ interface CookieOptions {
6
6
  secure?: boolean;
7
7
  sameSite?: 'strict' | 'lax' | 'none';
8
8
  }
9
- declare function buildCookieString(name: string, value: string, options?: CookieOptions): string;
10
- declare function setCookie(name: string, value: string, options?: CookieOptions): void;
11
- declare function getCookie(name: string): string | null;
12
- declare function removeCookie(name: string, options?: Omit<CookieOptions, 'expires' | 'maxAge'>): void;
13
- export { buildCookieString, getCookie, removeCookie, setCookie };
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.setCookie = exports.removeCookie = exports.getCookie = exports.buildCookieString = void 0;
4
- function buildCookieString(name, value, options) {
5
- const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
6
- if (options?.expires) {
7
- parts.push(`expires=${options.expires.toUTCString()}`);
8
- }
9
- if (options?.maxAge != null) {
10
- parts.push(`max-age=${options.maxAge}`);
11
- }
12
- if (options?.path) {
13
- parts.push(`path=${options.path}`);
14
- }
15
- if (options?.domain) {
16
- parts.push(`domain=${options.domain}`);
17
- }
18
- if (options?.secure) {
19
- parts.push('secure');
20
- }
21
- if (options?.sameSite) {
22
- parts.push(`samesite=${options.sameSite}`);
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
- return parts.join('; ');
25
- }
26
- exports.buildCookieString = buildCookieString;
27
- function setCookie(name, value, options) {
28
- if (typeof document === 'undefined') {
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
- document.cookie = buildCookieString(name, value, options);
32
- }
33
- exports.setCookie = setCookie;
34
- function getCookie(name) {
35
- if (typeof document === 'undefined') {
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
- const nameEQ = `${encodeURIComponent(name)}=`;
39
- const cookies = document.cookie.split(';');
40
- for (const cookie of cookies) {
41
- const trimmed = cookie.trim();
42
- if (trimmed.startsWith(nameEQ)) {
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.removeCookie = removeCookie;
54
+ exports.CookieService = CookieService;
55
+ const cookieService = new CookieService();
56
+ exports.cookieService = cookieService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveartx/authentication",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "LiveArt authentication package",
5
5
  "files": [
6
6
  "lib/**/*"