@atls/react-identity-links 0.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.
@@ -0,0 +1,3 @@
1
+ import { FC } from 'react';
2
+ import { UseIdentityUrlProps } from './identity-url.interfaces';
3
+ export declare const IdentityLink: FC<UseIdentityUrlProps>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityLink = void 0;
4
+ const use_identity_url_hook_1 = require("./use-identity-url.hook");
5
+ const IdentityLink = ({ children, type, returnTo, subdomain, substractHost, }) => {
6
+ const url = use_identity_url_hook_1.useIdentityUrl({ type, returnTo, subdomain, substractHost });
7
+ if (!children || !url) {
8
+ return null;
9
+ }
10
+ if (typeof children !== 'function') {
11
+ throw new Error('IdentityLink - children must be a function.');
12
+ }
13
+ return children(url);
14
+ };
15
+ exports.IdentityLink = IdentityLink;
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom/extend-expect';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = __importDefault(require("react"));
7
+ const react_2 = require("@testing-library/react");
8
+ require("@testing-library/jest-dom/extend-expect");
9
+ const identity_link_component_1 = require("./identity-link.component");
10
+ describe('identity-link.component', () => {
11
+ const originalLocation = window.location;
12
+ const mockWindowLocation = (newLocation) => {
13
+ delete window.location;
14
+ window.location = newLocation;
15
+ };
16
+ afterEach(() => {
17
+ mockWindowLocation(originalLocation);
18
+ });
19
+ it('without params', async () => {
20
+ mockWindowLocation(new URL('https://identity.atls.tech'));
21
+ const { getByText } = react_2.render(react_1.default.createElement(identity_link_component_1.IdentityLink, null, (url) => react_1.default.createElement("a", { href: url }, "Login")));
22
+ expect(getByText('Login')).toHaveAttribute('href', 'https://accounts.identity.atls.tech/auth/login?return_to=https://identity.atls.tech/');
23
+ });
24
+ });
@@ -0,0 +1,7 @@
1
+ export declare type IdentityUrlType = 'login' | 'registration' | 'settings' | 'verification' | 'recovery' | 'logout';
2
+ export interface UseIdentityUrlProps {
3
+ type?: IdentityUrlType;
4
+ returnTo?: string | false;
5
+ subdomain?: string;
6
+ substractHost?: string;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from './identity-link.component';
2
+ export * from './identity-url.interfaces';
3
+ export * from './use-identity-url.hook';
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./identity-link.component"), exports);
14
+ __exportStar(require("./identity-url.interfaces"), exports);
15
+ __exportStar(require("./use-identity-url.hook"), exports);
@@ -0,0 +1,10 @@
1
+ import { UseIdentityUrlProps } from './identity-url.interfaces';
2
+ export declare const IdentityUrlTypes: {
3
+ login: string;
4
+ registration: string;
5
+ verification: string;
6
+ recovery: string;
7
+ logout: string;
8
+ settings: string;
9
+ };
10
+ export declare const useIdentityUrl: ({ type, subdomain, returnTo, substractHost, }?: UseIdentityUrlProps) => string | null;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useIdentityUrl = exports.IdentityUrlTypes = void 0;
4
+ const react_1 = require("react");
5
+ const react_2 = require("react");
6
+ exports.IdentityUrlTypes = {
7
+ login: '/auth/login',
8
+ registration: '/auth/registration',
9
+ verification: '/auth/verification',
10
+ recovery: '/auth/recovery',
11
+ logout: '/auth/logout',
12
+ settings: '/profile/settings',
13
+ };
14
+ const useIdentityUrl = ({ type = 'login', subdomain = 'accounts', returnTo, substractHost, } = {}) => {
15
+ const [url, setUrl] = react_2.useState(null);
16
+ react_1.useEffect(() => {
17
+ if (typeof window !== 'undefined') {
18
+ const { hostname, origin, href } = window.location;
19
+ const path = exports.IdentityUrlTypes[type];
20
+ let host = hostname;
21
+ if (substractHost && host.startsWith(substractHost)) {
22
+ host = host.replace(substractHost.endsWith('.') ? substractHost : `${substractHost}.`, '');
23
+ }
24
+ if (subdomain) {
25
+ host = `${subdomain}.${host}`;
26
+ }
27
+ if (returnTo === false) {
28
+ setUrl(`https://${host}${path}`);
29
+ }
30
+ else {
31
+ let returnToValue = href;
32
+ if (returnTo) {
33
+ if (returnTo.startsWith('http')) {
34
+ returnToValue = returnTo;
35
+ }
36
+ else if (returnTo.startsWith('/')) {
37
+ returnToValue = `${origin}${returnTo}`;
38
+ }
39
+ }
40
+ setUrl(`https://${host}${path}?return_to=${returnToValue}`);
41
+ }
42
+ }
43
+ }, [type, subdomain, returnTo, substractHost]);
44
+ return url;
45
+ };
46
+ exports.useIdentityUrl = useIdentityUrl;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@atls/react-identity-links",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "scripts": {
10
+ "build": "yarn app library build",
11
+ "postpack": "rm -rf dist",
12
+ "prepack": "yarn run build"
13
+ },
14
+ "devDependencies": {
15
+ "@testing-library/jest-dom": "^5.14.1",
16
+ "@testing-library/react": "^12.1.0",
17
+ "@testing-library/react-hooks": "^7.0.2",
18
+ "@types/react": "^17.0.20",
19
+ "@types/react-dom": "^17.0.9",
20
+ "@types/testing-library__jest-dom": "^5.14.1",
21
+ "react": "^17.0.2",
22
+ "react-dom": "^17.0.2"
23
+ },
24
+ "peerDependencies": {
25
+ "react": "^17.0.1"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "main": "dist/index.js",
30
+ "typings": "dist/index.d.ts"
31
+ },
32
+ "typings": "dist/index.d.ts"
33
+ }