@meshagent/meshagent-ts-auth 0.35.6 → 0.35.7

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.
@@ -5,10 +5,8 @@ const pkce_generator_1 = require("./pkce-generator");
5
5
  const mauth_response_1 = require("./mauth-response");
6
6
  const meshagent_auth_1 = require("./meshagent-auth");
7
7
  const storage_1 = require("./storage");
8
+ const mauth_response_2 = require("./mauth-response");
8
9
  const DEFAULT_SERVER_URL = "https://api.meshagent.com";
9
- const CALLBACK_URL_KEY = "ma:callback_url";
10
- const CALLBACK_SOURCE_QUERY_PARAM = "ma_origin";
11
- const CALLBACK_SOURCE_QUERY_VALUE = "meshagent_auth";
12
10
  function getCurrentUrl() {
13
11
  if (typeof window === "undefined") {
14
12
  throw new Error("ensureLogin requires a browser environment");
@@ -17,23 +15,17 @@ function getCurrentUrl() {
17
15
  }
18
16
  function clearMeshagentAuthQueryParams(url) {
19
17
  url.searchParams.delete("code");
18
+ url.searchParams.delete("scope");
20
19
  url.searchParams.delete("state");
21
20
  url.searchParams.delete("error");
22
21
  url.searchParams.delete("error_description");
23
22
  url.searchParams.delete("error_uri");
24
- url.searchParams.delete(CALLBACK_SOURCE_QUERY_PARAM);
25
- }
26
- function hasMeshagentCallbackMarker(url) {
27
- return url.searchParams.get(CALLBACK_SOURCE_QUERY_PARAM) === CALLBACK_SOURCE_QUERY_VALUE;
28
23
  }
29
24
  function isMeshagentCallbackUrl(callbackUrl) {
30
25
  const currentUrl = getCurrentUrl();
31
26
  if (!currentUrl.searchParams.has("code")) {
32
27
  return false;
33
28
  }
34
- if (!callbackUrl) {
35
- return hasMeshagentCallbackMarker(currentUrl);
36
- }
37
29
  const expectedCallbackUrl = new URL(callbackUrl, currentUrl);
38
30
  clearMeshagentAuthQueryParams(currentUrl);
39
31
  clearMeshagentAuthQueryParams(expectedCallbackUrl);
@@ -45,25 +37,8 @@ function getRedirectCallbackUrl(callbackUrl) {
45
37
  }
46
38
  const currentUrl = getCurrentUrl();
47
39
  clearMeshagentAuthQueryParams(currentUrl);
48
- currentUrl.searchParams.set(CALLBACK_SOURCE_QUERY_PARAM, CALLBACK_SOURCE_QUERY_VALUE);
49
40
  return currentUrl.toString();
50
41
  }
51
- function getExchangeCallbackUrl(callbackUrl, storage) {
52
- if (callbackUrl) {
53
- return callbackUrl;
54
- }
55
- const storedCallbackUrl = storage.getItem(CALLBACK_URL_KEY);
56
- if (storedCallbackUrl) {
57
- return storedCallbackUrl;
58
- }
59
- return getRedirectCallbackUrl(undefined);
60
- }
61
- function saveCallbackUrl(storage, callbackUrl) {
62
- storage.setItem(CALLBACK_URL_KEY, callbackUrl);
63
- }
64
- function clearCallbackUrl(storage) {
65
- storage.removeItem(CALLBACK_URL_KEY);
66
- }
67
42
  function replaceCurrentUrlWithoutAuthParams() {
68
43
  if (typeof window === "undefined" || typeof window.history.replaceState !== "function") {
69
44
  return;
@@ -76,11 +51,11 @@ async function ensureLogin({ oauthClientId, serverUrl, callbackUrl, provider, })
76
51
  const serverBaseUrl = serverUrl ?? DEFAULT_SERVER_URL;
77
52
  const storage = (0, storage_1.getDefaultStorage)();
78
53
  const cache = new pkce_generator_1.LocalStoragePkceCache(storage);
79
- const authorizationCode = isMeshagentCallbackUrl(callbackUrl)
54
+ const authorizationCode = isMeshagentCallbackUrl(callbackUrl ?? window.location.href)
80
55
  ? (0, mauth_response_1.getAuthorizationCode)()
81
56
  : null;
82
57
  if (authorizationCode) {
83
- const exchangeCallbackUrl = getExchangeCallbackUrl(callbackUrl, storage);
58
+ const exchangeCallbackUrl = getRedirectCallbackUrl(callbackUrl);
84
59
  await (0, mauth_response_1.exchangeToken)({
85
60
  oauthClientId,
86
61
  serverUrl: serverBaseUrl,
@@ -90,18 +65,22 @@ async function ensureLogin({ oauthClientId, serverUrl, callbackUrl, provider, })
90
65
  auth: meshagent_auth_1.MeshagentAuth.current,
91
66
  });
92
67
  await cache.clearVerifier();
93
- clearCallbackUrl(storage);
94
68
  replaceCurrentUrlWithoutAuthParams();
95
69
  return;
96
70
  }
97
71
  if (meshagent_auth_1.MeshagentAuth.current.isLoggedIn()) {
72
+ const auth = meshagent_auth_1.MeshagentAuth.current;
73
+ const client = (0, mauth_response_2.getMeshagentClient)(auth, serverUrl ?? DEFAULT_SERVER_URL);
74
+ const me = await client.getUserProfile("me");
75
+ if (me) {
76
+ auth.setUser(me);
77
+ }
98
78
  return;
99
79
  }
100
80
  const redirectCallbackUrl = getRedirectCallbackUrl(callbackUrl);
101
81
  const generator = new pkce_generator_1.PkceGenerator();
102
82
  const pkce = await generator.generate();
103
83
  await cache.saveVerifier(pkce.codeVerifier);
104
- saveCallbackUrl(storage, redirectCallbackUrl);
105
84
  const query = new URLSearchParams({
106
85
  scope: "email",
107
86
  client_id: oauthClientId,
@@ -2,10 +2,8 @@ import { LocalStoragePkceCache, PkceGenerator } from "./pkce-generator";
2
2
  import { exchangeToken, getAuthorizationCode } from "./mauth-response";
3
3
  import { MeshagentAuth } from "./meshagent-auth";
4
4
  import { getDefaultStorage } from "./storage";
5
+ import { getMeshagentClient } from "./mauth-response";
5
6
  const DEFAULT_SERVER_URL = "https://api.meshagent.com";
6
- const CALLBACK_URL_KEY = "ma:callback_url";
7
- const CALLBACK_SOURCE_QUERY_PARAM = "ma_origin";
8
- const CALLBACK_SOURCE_QUERY_VALUE = "meshagent_auth";
9
7
  function getCurrentUrl() {
10
8
  if (typeof window === "undefined") {
11
9
  throw new Error("ensureLogin requires a browser environment");
@@ -14,23 +12,17 @@ function getCurrentUrl() {
14
12
  }
15
13
  function clearMeshagentAuthQueryParams(url) {
16
14
  url.searchParams.delete("code");
15
+ url.searchParams.delete("scope");
17
16
  url.searchParams.delete("state");
18
17
  url.searchParams.delete("error");
19
18
  url.searchParams.delete("error_description");
20
19
  url.searchParams.delete("error_uri");
21
- url.searchParams.delete(CALLBACK_SOURCE_QUERY_PARAM);
22
- }
23
- function hasMeshagentCallbackMarker(url) {
24
- return url.searchParams.get(CALLBACK_SOURCE_QUERY_PARAM) === CALLBACK_SOURCE_QUERY_VALUE;
25
20
  }
26
21
  function isMeshagentCallbackUrl(callbackUrl) {
27
22
  const currentUrl = getCurrentUrl();
28
23
  if (!currentUrl.searchParams.has("code")) {
29
24
  return false;
30
25
  }
31
- if (!callbackUrl) {
32
- return hasMeshagentCallbackMarker(currentUrl);
33
- }
34
26
  const expectedCallbackUrl = new URL(callbackUrl, currentUrl);
35
27
  clearMeshagentAuthQueryParams(currentUrl);
36
28
  clearMeshagentAuthQueryParams(expectedCallbackUrl);
@@ -42,25 +34,8 @@ function getRedirectCallbackUrl(callbackUrl) {
42
34
  }
43
35
  const currentUrl = getCurrentUrl();
44
36
  clearMeshagentAuthQueryParams(currentUrl);
45
- currentUrl.searchParams.set(CALLBACK_SOURCE_QUERY_PARAM, CALLBACK_SOURCE_QUERY_VALUE);
46
37
  return currentUrl.toString();
47
38
  }
48
- function getExchangeCallbackUrl(callbackUrl, storage) {
49
- if (callbackUrl) {
50
- return callbackUrl;
51
- }
52
- const storedCallbackUrl = storage.getItem(CALLBACK_URL_KEY);
53
- if (storedCallbackUrl) {
54
- return storedCallbackUrl;
55
- }
56
- return getRedirectCallbackUrl(undefined);
57
- }
58
- function saveCallbackUrl(storage, callbackUrl) {
59
- storage.setItem(CALLBACK_URL_KEY, callbackUrl);
60
- }
61
- function clearCallbackUrl(storage) {
62
- storage.removeItem(CALLBACK_URL_KEY);
63
- }
64
39
  function replaceCurrentUrlWithoutAuthParams() {
65
40
  if (typeof window === "undefined" || typeof window.history.replaceState !== "function") {
66
41
  return;
@@ -73,11 +48,11 @@ export async function ensureLogin({ oauthClientId, serverUrl, callbackUrl, provi
73
48
  const serverBaseUrl = serverUrl ?? DEFAULT_SERVER_URL;
74
49
  const storage = getDefaultStorage();
75
50
  const cache = new LocalStoragePkceCache(storage);
76
- const authorizationCode = isMeshagentCallbackUrl(callbackUrl)
51
+ const authorizationCode = isMeshagentCallbackUrl(callbackUrl ?? window.location.href)
77
52
  ? getAuthorizationCode()
78
53
  : null;
79
54
  if (authorizationCode) {
80
- const exchangeCallbackUrl = getExchangeCallbackUrl(callbackUrl, storage);
55
+ const exchangeCallbackUrl = getRedirectCallbackUrl(callbackUrl);
81
56
  await exchangeToken({
82
57
  oauthClientId,
83
58
  serverUrl: serverBaseUrl,
@@ -87,18 +62,22 @@ export async function ensureLogin({ oauthClientId, serverUrl, callbackUrl, provi
87
62
  auth: MeshagentAuth.current,
88
63
  });
89
64
  await cache.clearVerifier();
90
- clearCallbackUrl(storage);
91
65
  replaceCurrentUrlWithoutAuthParams();
92
66
  return;
93
67
  }
94
68
  if (MeshagentAuth.current.isLoggedIn()) {
69
+ const auth = MeshagentAuth.current;
70
+ const client = getMeshagentClient(auth, serverUrl ?? DEFAULT_SERVER_URL);
71
+ const me = await client.getUserProfile("me");
72
+ if (me) {
73
+ auth.setUser(me);
74
+ }
95
75
  return;
96
76
  }
97
77
  const redirectCallbackUrl = getRedirectCallbackUrl(callbackUrl);
98
78
  const generator = new PkceGenerator();
99
79
  const pkce = await generator.generate();
100
80
  await cache.saveVerifier(pkce.codeVerifier);
101
- saveCallbackUrl(storage, redirectCallbackUrl);
102
81
  const query = new URLSearchParams({
103
82
  scope: "email",
104
83
  client_id: oauthClientId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshagent/meshagent-ts-auth",
3
- "version": "0.35.6",
3
+ "version": "0.35.7",
4
4
  "description": "Meshagent Typescript Auth",
5
5
  "homepage": "https://github.com/meshagent/meshagent-ts-auth",
6
6
  "scripts": {
@@ -47,4 +47,4 @@
47
47
  "overrides": {
48
48
  "rollup": "4.52.5"
49
49
  }
50
- }
50
+ }