@atls/react-identity-links 0.0.2 → 0.0.3

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.
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IdentityLink = void 0;
4
4
  const use_identity_url_hook_1 = require("./use-identity-url.hook");
5
- const IdentityLink = ({ children, type, returnTo, subdomain, substractHost, }) => {
6
- const url = (0, use_identity_url_hook_1.useIdentityUrl)({ type, returnTo, subdomain, substractHost });
5
+ const IdentityLink = ({ children, type, returnTo }) => {
6
+ const url = (0, use_identity_url_hook_1.useIdentityUrl)({ type, returnTo });
7
7
  if (!children || !url) {
8
8
  return null;
9
9
  }
@@ -18,7 +18,7 @@ describe('identity-link.component', () => {
18
18
  });
19
19
  it('without params', async () => {
20
20
  mockWindowLocation(new URL('https://identity.monstrs.dev'));
21
- const { getByText } = (0, react_1.render)(react_2.default.createElement(identity_link_component_1.IdentityLink, null, (url) => react_2.default.createElement("a", { href: url }, "Login")));
21
+ const { getByText } = (0, react_1.render)(react_2.default.createElement(identity_link_component_1.IdentityLink, { returnTo: true }, (url) => react_2.default.createElement("a", { href: url }, "Login")));
22
22
  expect(getByText('Login')).toHaveAttribute('href', 'https://accounts.monstrs.dev/auth/login?return_to=https://identity.monstrs.dev/');
23
23
  });
24
24
  });
@@ -12,26 +12,29 @@ exports.identityUrlTypes = {
12
12
  logout: '/auth/logout',
13
13
  settings: '/profile/settings',
14
14
  };
15
- const useIdentityUrl = ({ type = 'login', returnTo } = {}) => {
15
+ const useIdentityUrl = ({ type = 'login', returnTo = false } = {}) => {
16
16
  const [url, setUrl] = (0, react_1.useState)(null);
17
17
  (0, use_browser_effect_hook_1.useBrowserEffect)(() => {
18
- const { hostname, origin, href, protocol } = window.location;
18
+ const { hostname, origin, href, protocol, pathname } = window.location;
19
19
  const path = exports.identityUrlTypes[type];
20
- const domain = hostname === 'localhost' ? 'localhost:4433' : (0, tldjs_1.getDomain)(hostname);
21
- if (returnTo === false) {
22
- setUrl(`${protocol}//accounts.${domain}${path}`);
20
+ if (hostname === 'localhost') {
21
+ setUrl(`${protocol}//localhost:3000${path}`);
23
22
  }
24
23
  else {
25
- let returnToValue = href;
26
- if (returnTo) {
27
- if (returnTo.startsWith('http')) {
28
- returnToValue = returnTo;
29
- }
30
- else if (returnTo.startsWith('/')) {
31
- returnToValue = `${origin}${returnTo}`;
32
- }
24
+ const domain = (0, tldjs_1.getDomain)(hostname);
25
+ if (returnTo === false) {
26
+ setUrl(`${protocol}//accounts.${domain}${path}`);
27
+ }
28
+ else if (returnTo === true) {
29
+ setUrl(`${protocol}//accounts.${domain}${path}?return_to=${href}`);
30
+ }
31
+ else {
32
+ const returnToOrigin = returnTo.subdomain
33
+ ? `${protocol}//${returnTo.subdomain}.${domain}`
34
+ : origin;
35
+ const returnToValue = `${returnToOrigin}${returnTo.pathname ? returnTo.pathname : pathname}`;
36
+ setUrl(`${protocol}//accounts.${domain}${path}?return_to=${returnToValue}`);
33
37
  }
34
- setUrl(`${protocol}//accounts.${domain}${path}?return_to=${returnToValue}`);
35
38
  }
36
39
  }, [type, returnTo]);
37
40
  return url;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atls/react-identity-links",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [