@plasmicapp/auth-react 0.0.12 → 0.0.14
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/dist/hooks.d.ts +12 -12
- package/dist/index.d.ts +2 -2
- package/dist/index.esm.js +173 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/index.js +188 -5
- package/dist/index.js.map +7 -0
- package/package.json +14 -35
- package/dist/auth-react.cjs.development.js +0 -607
- package/dist/auth-react.cjs.development.js.map +0 -1
- package/dist/auth-react.cjs.production.min.js +0 -2
- package/dist/auth-react.cjs.production.min.js.map +0 -1
- package/dist/auth-react.esm.js +0 -596
- package/dist/auth-react.esm.js.map +0 -1
package/dist/hooks.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { PlasmicUser } from '@plasmicapp/auth-api';
|
|
2
|
-
/**
|
|
3
|
-
* Handles the authentication flow for Plasmic Auth and returns the user and token
|
|
4
|
-
*/
|
|
5
|
-
export declare function usePlasmicAuth(opts: {
|
|
6
|
-
host?: string;
|
|
7
|
-
appId?: string;
|
|
8
|
-
}): {
|
|
9
|
-
user: PlasmicUser | null | undefined;
|
|
10
|
-
token: string | null | undefined;
|
|
11
|
-
isUserLoading: boolean | undefined;
|
|
12
|
-
};
|
|
1
|
+
import { PlasmicUser } from '@plasmicapp/auth-api';
|
|
2
|
+
/**
|
|
3
|
+
* Handles the authentication flow for Plasmic Auth and returns the user and token
|
|
4
|
+
*/
|
|
5
|
+
export declare function usePlasmicAuth(opts: {
|
|
6
|
+
host?: string;
|
|
7
|
+
appId?: string;
|
|
8
|
+
}): {
|
|
9
|
+
user: PlasmicUser | null | undefined;
|
|
10
|
+
token: string | null | undefined;
|
|
11
|
+
isUserLoading: boolean | undefined;
|
|
12
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from '@plasmicapp/auth-api';
|
|
2
|
-
export * from './hooks';
|
|
1
|
+
export * from '@plasmicapp/auth-api';
|
|
2
|
+
export * from './hooks';
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/index.ts
|
|
23
|
+
export * from "@plasmicapp/auth-api";
|
|
24
|
+
|
|
25
|
+
// src/hooks.ts
|
|
26
|
+
import {
|
|
27
|
+
getPlasmicAppUser,
|
|
28
|
+
getPlasmicAppUserFromToken
|
|
29
|
+
} from "@plasmicapp/auth-api";
|
|
30
|
+
import { useMutablePlasmicQueryData } from "@plasmicapp/query";
|
|
31
|
+
var storageUserKey = (appId) => `$user.${appId}`;
|
|
32
|
+
var isBrowser = typeof window !== "undefined";
|
|
33
|
+
function getCallbackParams() {
|
|
34
|
+
const params = new URLSearchParams(window.location.search);
|
|
35
|
+
const error = params.get("error");
|
|
36
|
+
const code = params.get("code");
|
|
37
|
+
const state = params.get("state");
|
|
38
|
+
return {
|
|
39
|
+
isCallbackError: !!error,
|
|
40
|
+
isCodeExchange: !!code && !!state,
|
|
41
|
+
error,
|
|
42
|
+
code,
|
|
43
|
+
state
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function getCodeVerifier() {
|
|
47
|
+
try {
|
|
48
|
+
return localStorage.getItem("code_verifier");
|
|
49
|
+
} catch (err) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function removeCallbackParams() {
|
|
54
|
+
try {
|
|
55
|
+
window.history.replaceState({}, "", location.pathname);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
console.error(`Error while removing callback params: ${err}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function isContinueToSameLocation(continueTo) {
|
|
61
|
+
const pathname = window.location.pathname;
|
|
62
|
+
const origin = window.location.origin;
|
|
63
|
+
return continueTo === pathname || continueTo === origin + pathname;
|
|
64
|
+
}
|
|
65
|
+
function handleCallback(opts) {
|
|
66
|
+
return __async(this, null, function* () {
|
|
67
|
+
const { host, appId, code, state, codeVerifier } = opts;
|
|
68
|
+
let continueTo = "/";
|
|
69
|
+
try {
|
|
70
|
+
if (state) {
|
|
71
|
+
const parsedState = JSON.parse(state);
|
|
72
|
+
continueTo = parsedState.continueTo;
|
|
73
|
+
}
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error(`Error while parsing state: ${err}`);
|
|
76
|
+
}
|
|
77
|
+
const result = yield getPlasmicAppUser({
|
|
78
|
+
host,
|
|
79
|
+
appId,
|
|
80
|
+
code,
|
|
81
|
+
codeVerifier
|
|
82
|
+
});
|
|
83
|
+
if (result.error) {
|
|
84
|
+
console.log(`Error while performing code exchange: ${result.error}`);
|
|
85
|
+
return void 0;
|
|
86
|
+
}
|
|
87
|
+
localStorage.setItem(storageUserKey(appId), result.token);
|
|
88
|
+
if (!isContinueToSameLocation(continueTo)) {
|
|
89
|
+
window.location.assign(continueTo);
|
|
90
|
+
} else {
|
|
91
|
+
removeCallbackParams();
|
|
92
|
+
}
|
|
93
|
+
return { token: result.token, user: result.user };
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function checkAlreadyLoggedUser(opts) {
|
|
97
|
+
return __async(this, null, function* () {
|
|
98
|
+
const { appId, host } = opts;
|
|
99
|
+
const token = localStorage.getItem(storageUserKey(appId));
|
|
100
|
+
if (!token) {
|
|
101
|
+
return { user: null, token: null };
|
|
102
|
+
}
|
|
103
|
+
const { user, error } = yield getPlasmicAppUserFromToken({
|
|
104
|
+
host,
|
|
105
|
+
token
|
|
106
|
+
});
|
|
107
|
+
if (error) {
|
|
108
|
+
localStorage.removeItem(storageUserKey(appId));
|
|
109
|
+
console.log(`Error while checking logged user`);
|
|
110
|
+
return { user: null, token: null };
|
|
111
|
+
}
|
|
112
|
+
return { user, token };
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function usePlasmicAuth(opts) {
|
|
116
|
+
const { host, appId } = opts;
|
|
117
|
+
const authKey = `$csq$plasmic-auth-${appId}`;
|
|
118
|
+
const { data: userData, isLoading } = useMutablePlasmicQueryData(
|
|
119
|
+
authKey,
|
|
120
|
+
() => __async(this, null, function* () {
|
|
121
|
+
if (!appId || !isBrowser) {
|
|
122
|
+
return { user: null, token: null };
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const callbackParams = getCallbackParams();
|
|
126
|
+
if (callbackParams.isCallbackError || callbackParams.isCodeExchange) {
|
|
127
|
+
if (callbackParams.isCallbackError) {
|
|
128
|
+
removeCallbackParams();
|
|
129
|
+
console.error(`Error: ${callbackParams.error}`);
|
|
130
|
+
return { user: null, token: null };
|
|
131
|
+
} else {
|
|
132
|
+
const codeVerifier = getCodeVerifier();
|
|
133
|
+
if (!codeVerifier) {
|
|
134
|
+
removeCallbackParams();
|
|
135
|
+
console.error("No code verifier found");
|
|
136
|
+
return { user: null, token: null };
|
|
137
|
+
} else {
|
|
138
|
+
const result = yield handleCallback({
|
|
139
|
+
host,
|
|
140
|
+
appId,
|
|
141
|
+
code: callbackParams.code,
|
|
142
|
+
state: callbackParams.state,
|
|
143
|
+
codeVerifier
|
|
144
|
+
});
|
|
145
|
+
if (!result) {
|
|
146
|
+
removeCallbackParams();
|
|
147
|
+
return { user: null, token: null };
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
return yield checkAlreadyLoggedUser({
|
|
154
|
+
appId,
|
|
155
|
+
host
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
} catch (err) {
|
|
159
|
+
console.error(`Error while handling auth: ${err}`);
|
|
160
|
+
}
|
|
161
|
+
return { user: null, token: null };
|
|
162
|
+
})
|
|
163
|
+
);
|
|
164
|
+
return {
|
|
165
|
+
user: userData == null ? void 0 : userData.user,
|
|
166
|
+
token: userData == null ? void 0 : userData.token,
|
|
167
|
+
isUserLoading: isLoading
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
export {
|
|
171
|
+
usePlasmicAuth
|
|
172
|
+
};
|
|
173
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/hooks.ts"],
|
|
4
|
+
"sourcesContent": ["export * from '@plasmicapp/auth-api';\nexport * from './hooks';\n", "import {\n getPlasmicAppUser,\n getPlasmicAppUserFromToken,\n PlasmicUser,\n} from '@plasmicapp/auth-api';\nimport { useMutablePlasmicQueryData } from '@plasmicapp/query';\n\ninterface PlasmicAuthData {\n user: PlasmicUser | null;\n token: string | null;\n}\n\nconst storageUserKey = (appId: string) => `$user.${appId}`;\n\nconst isBrowser = typeof window !== 'undefined';\n\nfunction getCallbackParams() {\n const params = new URLSearchParams(window.location.search);\n const error = params.get('error');\n const code = params.get('code');\n const state = params.get('state');\n\n return {\n isCallbackError: !!error,\n isCodeExchange: !!code && !!state,\n error,\n code,\n state,\n };\n}\n\nfunction getCodeVerifier() {\n try {\n return localStorage.getItem('code_verifier');\n } catch (err) {\n return null;\n }\n}\n\nfunction removeCallbackParams() {\n try {\n window.history.replaceState({}, '', location.pathname);\n } catch (err) {\n console.error(`Error while removing callback params: ${err}`);\n }\n}\n\n// continueTo can be only a pathname or a full url with origin\n// we can consider that currently we are at the callback page\n// with callback params, so ignore the search params\nfunction isContinueToSameLocation(continueTo: string) {\n const pathname = window.location.pathname;\n const origin = window.location.origin;\n return continueTo === pathname || continueTo === origin + pathname;\n}\n\nasync function handleCallback(opts: {\n host?: string;\n appId: string;\n code: string;\n state: string;\n codeVerifier: string;\n}): Promise<PlasmicAuthData | undefined> {\n const { host, appId, code, state, codeVerifier } = opts;\n\n let continueTo = '/';\n try {\n if (state) {\n const parsedState = JSON.parse(state);\n continueTo = parsedState.continueTo;\n }\n } catch (err) {\n console.error(`Error while parsing state: ${err}`);\n }\n\n const result = await getPlasmicAppUser({\n host,\n appId,\n code,\n codeVerifier,\n });\n\n if (result.error) {\n console.log(`Error while performing code exchange: ${result.error}`);\n return undefined;\n }\n\n localStorage.setItem(storageUserKey(appId), result.token);\n\n if (!isContinueToSameLocation(continueTo)) {\n window.location.assign(continueTo);\n } else {\n removeCallbackParams();\n }\n\n return { token: result.token, user: result.user };\n}\n\nasync function checkAlreadyLoggedUser(opts: {\n appId: string;\n host?: string;\n}): Promise<PlasmicAuthData> {\n const { appId, host } = opts;\n\n const token = localStorage.getItem(storageUserKey(appId));\n if (!token) {\n return { user: null, token: null };\n }\n\n const { user, error } = await getPlasmicAppUserFromToken({\n host,\n token,\n });\n\n if (error) {\n // If there is an error, we just remove the token\n // But ideally we should check if the reason is token expired\n localStorage.removeItem(storageUserKey(appId));\n console.log(`Error while checking logged user`);\n return { user: null, token: null };\n }\n\n return { user, token };\n}\n\n/**\n * Handles the authentication flow for Plasmic Auth and returns the user and token\n */\nexport function usePlasmicAuth(opts: { host?: string; appId?: string }) {\n const { host, appId } = opts;\n const authKey = `$csq$plasmic-auth-${appId}`;\n const { data: userData, isLoading } = useMutablePlasmicQueryData(\n authKey,\n async (): Promise<PlasmicAuthData> => {\n if (!appId || !isBrowser) {\n return { user: null, token: null };\n }\n\n // Fail silently for now\n try {\n // We first check if we are currently in the callback flow\n const callbackParams = getCallbackParams();\n if (callbackParams.isCallbackError || callbackParams.isCodeExchange) {\n if (callbackParams.isCallbackError) {\n // If there is an error, we just remove the callback params\n removeCallbackParams();\n console.error(`Error: ${callbackParams.error}`);\n return { user: null, token: null };\n } else {\n const codeVerifier = getCodeVerifier();\n if (!codeVerifier) {\n // If there is no codeVerifier, we just remove the callback params\n removeCallbackParams();\n console.error('No code verifier found');\n return { user: null, token: null };\n } else {\n // Perform code exchange, by the end of the callback handling we will either still be\n // in the callback page or we will be redirected to the continueTo page.\n\n const result = await handleCallback({\n host,\n appId,\n code: callbackParams.code!,\n state: callbackParams.state!,\n codeVerifier,\n });\n\n // Undefined result means that the code exchange failed\n if (!result) {\n removeCallbackParams();\n return { user: null, token: null };\n }\n\n // In the above case where the code exchange failed and the callback page requires login\n // a login redirect will be triggered\n return result;\n }\n }\n } else {\n return await checkAlreadyLoggedUser({\n appId,\n host,\n });\n }\n } catch (err) {\n console.error(`Error while handling auth: ${err}`);\n }\n\n return { user: null, token: null };\n }\n );\n\n return {\n user: userData?.user,\n token: userData?.token,\n isUserLoading: isLoading,\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA,cAAc;;;ACAd;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,kCAAkC;AAO3C,IAAM,iBAAiB,CAAC,UAAkB,SAAS;AAEnD,IAAM,YAAY,OAAO,WAAW;AAEpC,SAAS,oBAAoB;AAC3B,QAAM,SAAS,IAAI,gBAAgB,OAAO,SAAS,MAAM;AACzD,QAAM,QAAQ,OAAO,IAAI,OAAO;AAChC,QAAM,OAAO,OAAO,IAAI,MAAM;AAC9B,QAAM,QAAQ,OAAO,IAAI,OAAO;AAEhC,SAAO;AAAA,IACL,iBAAiB,CAAC,CAAC;AAAA,IACnB,gBAAgB,CAAC,CAAC,QAAQ,CAAC,CAAC;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,SAAS,kBAAkB;AACzB,MAAI;AACF,WAAO,a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AA,YAClC;AAAA,YACA;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF,SAAS,KAAP;AACA,gBAAQ,MAAM,8BAA8B,KAAK;AAAA,MACnD;AAEA,aAAO,EAAE,MAAM,MAAM,OAAO,KAAK;AAAA,IACnC;AAAA,EACF;AAEA,SAAO;AAAA,IACL,MAAM,qCAAU;AAAA,IAChB,OAAO,qCAAU;AAAA,IACjB,eAAe;AAAA,EACjB;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
1
40
|
|
|
2
|
-
|
|
41
|
+
// src/index.ts
|
|
42
|
+
var src_exports = {};
|
|
43
|
+
__export(src_exports, {
|
|
44
|
+
usePlasmicAuth: () => usePlasmicAuth
|
|
45
|
+
});
|
|
46
|
+
module.exports = __toCommonJS(src_exports);
|
|
47
|
+
__reExport(src_exports, require("@plasmicapp/auth-api"), module.exports);
|
|
3
48
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
49
|
+
// src/hooks.ts
|
|
50
|
+
var import_auth_api = require("@plasmicapp/auth-api");
|
|
51
|
+
var import_query = require("@plasmicapp/query");
|
|
52
|
+
var storageUserKey = (appId) => `$user.${appId}`;
|
|
53
|
+
var isBrowser = typeof window !== "undefined";
|
|
54
|
+
function getCallbackParams() {
|
|
55
|
+
const params = new URLSearchParams(window.location.search);
|
|
56
|
+
const error = params.get("error");
|
|
57
|
+
const code = params.get("code");
|
|
58
|
+
const state = params.get("state");
|
|
59
|
+
return {
|
|
60
|
+
isCallbackError: !!error,
|
|
61
|
+
isCodeExchange: !!code && !!state,
|
|
62
|
+
error,
|
|
63
|
+
code,
|
|
64
|
+
state
|
|
65
|
+
};
|
|
8
66
|
}
|
|
67
|
+
function getCodeVerifier() {
|
|
68
|
+
try {
|
|
69
|
+
return localStorage.getItem("code_verifier");
|
|
70
|
+
} catch (err) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function removeCallbackParams() {
|
|
75
|
+
try {
|
|
76
|
+
window.history.replaceState({}, "", location.pathname);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error(`Error while removing callback params: ${err}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function isContinueToSameLocation(continueTo) {
|
|
82
|
+
const pathname = window.location.pathname;
|
|
83
|
+
const origin = window.location.origin;
|
|
84
|
+
return continueTo === pathname || continueTo === origin + pathname;
|
|
85
|
+
}
|
|
86
|
+
function handleCallback(opts) {
|
|
87
|
+
return __async(this, null, function* () {
|
|
88
|
+
const { host, appId, code, state, codeVerifier } = opts;
|
|
89
|
+
let continueTo = "/";
|
|
90
|
+
try {
|
|
91
|
+
if (state) {
|
|
92
|
+
const parsedState = JSON.parse(state);
|
|
93
|
+
continueTo = parsedState.continueTo;
|
|
94
|
+
}
|
|
95
|
+
} catch (err) {
|
|
96
|
+
console.error(`Error while parsing state: ${err}`);
|
|
97
|
+
}
|
|
98
|
+
const result = yield (0, import_auth_api.getPlasmicAppUser)({
|
|
99
|
+
host,
|
|
100
|
+
appId,
|
|
101
|
+
code,
|
|
102
|
+
codeVerifier
|
|
103
|
+
});
|
|
104
|
+
if (result.error) {
|
|
105
|
+
console.log(`Error while performing code exchange: ${result.error}`);
|
|
106
|
+
return void 0;
|
|
107
|
+
}
|
|
108
|
+
localStorage.setItem(storageUserKey(appId), result.token);
|
|
109
|
+
if (!isContinueToSameLocation(continueTo)) {
|
|
110
|
+
window.location.assign(continueTo);
|
|
111
|
+
} else {
|
|
112
|
+
removeCallbackParams();
|
|
113
|
+
}
|
|
114
|
+
return { token: result.token, user: result.user };
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function checkAlreadyLoggedUser(opts) {
|
|
118
|
+
return __async(this, null, function* () {
|
|
119
|
+
const { appId, host } = opts;
|
|
120
|
+
const token = localStorage.getItem(storageUserKey(appId));
|
|
121
|
+
if (!token) {
|
|
122
|
+
return { user: null, token: null };
|
|
123
|
+
}
|
|
124
|
+
const { user, error } = yield (0, import_auth_api.getPlasmicAppUserFromToken)({
|
|
125
|
+
host,
|
|
126
|
+
token
|
|
127
|
+
});
|
|
128
|
+
if (error) {
|
|
129
|
+
localStorage.removeItem(storageUserKey(appId));
|
|
130
|
+
console.log(`Error while checking logged user`);
|
|
131
|
+
return { user: null, token: null };
|
|
132
|
+
}
|
|
133
|
+
return { user, token };
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function usePlasmicAuth(opts) {
|
|
137
|
+
const { host, appId } = opts;
|
|
138
|
+
const authKey = `$csq$plasmic-auth-${appId}`;
|
|
139
|
+
const { data: userData, isLoading } = (0, import_query.useMutablePlasmicQueryData)(
|
|
140
|
+
authKey,
|
|
141
|
+
() => __async(this, null, function* () {
|
|
142
|
+
if (!appId || !isBrowser) {
|
|
143
|
+
return { user: null, token: null };
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const callbackParams = getCallbackParams();
|
|
147
|
+
if (callbackParams.isCallbackError || callbackParams.isCodeExchange) {
|
|
148
|
+
if (callbackParams.isCallbackError) {
|
|
149
|
+
removeCallbackParams();
|
|
150
|
+
console.error(`Error: ${callbackParams.error}`);
|
|
151
|
+
return { user: null, token: null };
|
|
152
|
+
} else {
|
|
153
|
+
const codeVerifier = getCodeVerifier();
|
|
154
|
+
if (!codeVerifier) {
|
|
155
|
+
removeCallbackParams();
|
|
156
|
+
console.error("No code verifier found");
|
|
157
|
+
return { user: null, token: null };
|
|
158
|
+
} else {
|
|
159
|
+
const result = yield handleCallback({
|
|
160
|
+
host,
|
|
161
|
+
appId,
|
|
162
|
+
code: callbackParams.code,
|
|
163
|
+
state: callbackParams.state,
|
|
164
|
+
codeVerifier
|
|
165
|
+
});
|
|
166
|
+
if (!result) {
|
|
167
|
+
removeCallbackParams();
|
|
168
|
+
return { user: null, token: null };
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
return yield checkAlreadyLoggedUser({
|
|
175
|
+
appId,
|
|
176
|
+
host
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
} catch (err) {
|
|
180
|
+
console.error(`Error while handling auth: ${err}`);
|
|
181
|
+
}
|
|
182
|
+
return { user: null, token: null };
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
return {
|
|
186
|
+
user: userData == null ? void 0 : userData.user,
|
|
187
|
+
token: userData == null ? void 0 : userData.token,
|
|
188
|
+
isUserLoading: isLoading
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/hooks.ts"],
|
|
4
|
+
"sourcesContent": ["export * from '@plasmicapp/auth-api';\nexport * from './hooks';\n", "import {\n getPlasmicAppUser,\n getPlasmicAppUserFromToken,\n PlasmicUser,\n} from '@plasmicapp/auth-api';\nimport { useMutablePlasmicQueryData } from '@plasmicapp/query';\n\ninterface PlasmicAuthData {\n user: PlasmicUser | null;\n token: string | null;\n}\n\nconst storageUserKey = (appId: string) => `$user.${appId}`;\n\nconst isBrowser = typeof window !== 'undefined';\n\nfunction getCallbackParams() {\n const params = new URLSearchParams(window.location.search);\n const error = params.get('error');\n const code = params.get('code');\n const state = params.get('state');\n\n return {\n isCallbackError: !!error,\n isCodeExchange: !!code && !!state,\n error,\n code,\n state,\n };\n}\n\nfunction getCodeVerifier() {\n try {\n return localStorage.getItem('code_verifier');\n } catch (err) {\n return null;\n }\n}\n\nfunction removeCallbackParams() {\n try {\n window.history.replaceState({}, '', location.pathname);\n } catch (err) {\n console.error(`Error while removing callback params: ${err}`);\n }\n}\n\n// continueTo can be only a pathname or a full url with origin\n// we can consider that currently we are at the callback page\n// with callback params, so ignore the search params\nfunction isContinueToSameLocation(continueTo: string) {\n const pathname = window.location.pathname;\n const origin = window.location.origin;\n return continueTo === pathname || continueTo === origin + pathname;\n}\n\nasync function handleCallback(opts: {\n host?: string;\n appId: string;\n code: string;\n state: string;\n codeVerifier: string;\n}): Promise<PlasmicAuthData | undefined> {\n const { host, appId, code, state, codeVerifier } = opts;\n\n let continueTo = '/';\n try {\n if (state) {\n const parsedState = JSON.parse(state);\n continueTo = parsedState.continueTo;\n }\n } catch (err) {\n console.error(`Error while parsing state: ${err}`);\n }\n\n const result = await getPlasmicAppUser({\n host,\n appId,\n code,\n codeVerifier,\n });\n\n if (result.error) {\n console.log(`Error while performing code exchange: ${result.error}`);\n return undefined;\n }\n\n localStorage.setItem(storageUserKey(appId), result.token);\n\n if (!isContinueToSameLocation(continueTo)) {\n window.location.assign(continueTo);\n } else {\n removeCallbackParams();\n }\n\n return { token: result.token, user: result.user };\n}\n\nasync function checkAlreadyLoggedUser(opts: {\n appId: string;\n host?: string;\n}): Promise<PlasmicAuthData> {\n const { appId, host } = opts;\n\n const token = localStorage.getItem(storageUserKey(appId));\n if (!token) {\n return { user: null, token: null };\n }\n\n const { user, error } = await getPlasmicAppUserFromToken({\n host,\n token,\n });\n\n if (error) {\n // If there is an error, we just remove the token\n // But ideally we should check if the reason is token expired\n localStorage.removeItem(storageUserKey(appId));\n console.log(`Error while checking logged user`);\n return { user: null, token: null };\n }\n\n return { user, token };\n}\n\n/**\n * Handles the authentication flow for Plasmic Auth and returns the user and token\n */\nexport function usePlasmicAuth(opts: { host?: string; appId?: string }) {\n const { host, appId } = opts;\n const authKey = `$csq$plasmic-auth-${appId}`;\n const { data: userData, isLoading } = useMutablePlasmicQueryData(\n authKey,\n async (): Promise<PlasmicAuthData> => {\n if (!appId || !isBrowser) {\n return { user: null, token: null };\n }\n\n // Fail silently for now\n try {\n // We first check if we are currently in the callback flow\n const callbackParams = getCallbackParams();\n if (callbackParams.isCallbackError || callbackParams.isCodeExchange) {\n if (callbackParams.isCallbackError) {\n // If there is an error, we just remove the callback params\n removeCallbackParams();\n console.error(`Error: ${callbackParams.error}`);\n return { user: null, token: null };\n } else {\n const codeVerifier = getCodeVerifier();\n if (!codeVerifier) {\n // If there is no codeVerifier, we just remove the callback params\n removeCallbackParams();\n console.error('No code verifier found');\n return { user: null, token: null };\n } else {\n // Perform code exchange, by the end of the callback handling we will either still be\n // in the callback page or we will be redirected to the continueTo page.\n\n const result = await handleCallback({\n host,\n appId,\n code: callbackParams.code!,\n state: callbackParams.state!,\n codeVerifier,\n });\n\n // Undefined result means that the code exchange failed\n if (!result) {\n removeCallbackParams();\n return { user: null, token: null };\n }\n\n // In the above case where the code exchange failed and the callback page requires login\n // a login redirect will be triggered\n return result;\n }\n }\n } else {\n return await checkAlreadyLoggedUser({\n appId,\n host,\n });\n }\n } catch (err) {\n console.error(`Error while handling auth: ${err}`);\n }\n\n return { user: null, token: null };\n }\n );\n\n return {\n user: userData?.user,\n token: userData?.token,\n isUserLoading: isLoading,\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,iCAAd;;;ACAA,sBAIO;AACP,mBAA2C;AAO3C,IAAM,iBAAiB,CAAC,UAAkB,SAAS;AAEnD,IAAM,YAAY,OAAO,WAAW;AAEpC,SAAS,oBAAoB;AAC3B,QAAM,SAAS,IAAI,gBAAgB,OAAO,SAAS,MAAM;AACzD,QAAM,QAAQ,OAAO,IAAI,OAAO;AAChC,QAAM,OAAO,OAAO,IAAI,MAAM;AAC9B,QAAM,QAAQ,OAAO,IAAI,OAAO;AAEhC,SAAO;AAAA,IACL,iBAAiB,CAAC,CAAC;AAAA,IACnB,gBAAgB,CAAC,CAAC,QAAQ,CAAC,CAAC;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,SAAS,kBAAkB;AACzB,MAAI;AACF,WAAO,aAAa,QAAQ,eAAe;AAAA,EAC7C,SAAS,KAAP;AACA,WAAO;AAAA,EACT;AACF;AAEA,SAAS,uBAAuB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,8BAA8B,KAAK;AAAA,MACnD;AAEA,aAAO,EAAE,MAAM,MAAM,OAAO,KAAK;AAAA,IACnC;AAAA,EACF;AAEA,SAAO;AAAA,IACL,MAAM,qCAAU;AAAA,IAChB,OAAO,qCAAU;AAAA,IACjB,eAAe;AAAA,EACjB;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.14",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"types": "./dist/index.d.ts",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.esm.js",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": {
|
|
8
9
|
"types": "./dist/index.d.ts",
|
|
9
|
-
"
|
|
10
|
+
"import": "./dist/index.esm.js",
|
|
11
|
+
"require": "./dist/index.js"
|
|
10
12
|
}
|
|
11
13
|
},
|
|
12
14
|
"files": [
|
|
@@ -16,55 +18,32 @@
|
|
|
16
18
|
"node": ">=10"
|
|
17
19
|
},
|
|
18
20
|
"scripts": {
|
|
19
|
-
"
|
|
20
|
-
"build": "
|
|
21
|
-
"
|
|
21
|
+
"build": "yarn build:types && yarn build:index",
|
|
22
|
+
"build:types": "yarn tsc",
|
|
23
|
+
"build:index": "node ../../build.mjs ./src/index.ts",
|
|
22
24
|
"test": "yarn --cwd=../.. test",
|
|
23
|
-
"lint": "
|
|
25
|
+
"lint": "eslint",
|
|
24
26
|
"prepare": "if-env PREPARE_NO_BUILD=true || yarn build",
|
|
25
27
|
"size": "size-limit",
|
|
26
28
|
"analyze": "size-limit --why"
|
|
27
29
|
},
|
|
28
|
-
"husky": {
|
|
29
|
-
"hooks": {
|
|
30
|
-
"pre-commit": "tsdx lint"
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
"prettier": {
|
|
34
|
-
"printWidth": 80,
|
|
35
|
-
"semi": true,
|
|
36
|
-
"singleQuote": true,
|
|
37
|
-
"trailingComma": "es5"
|
|
38
|
-
},
|
|
39
30
|
"name": "@plasmicapp/auth-react",
|
|
40
31
|
"author": "Chung Wu",
|
|
41
|
-
"module": "dist/auth-react.esm.js",
|
|
42
32
|
"size-limit": [
|
|
43
33
|
{
|
|
44
|
-
"path": "dist/
|
|
45
|
-
"limit": "10 KB"
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
"path": "dist/auth-react.esm.js",
|
|
34
|
+
"path": "dist/index.js",
|
|
49
35
|
"limit": "10 KB"
|
|
50
36
|
}
|
|
51
37
|
],
|
|
52
|
-
"devDependencies": {
|
|
53
|
-
"@size-limit/preset-small-lib": "^4.11.0",
|
|
54
|
-
"husky": "^6.0.0",
|
|
55
|
-
"size-limit": "^4.11.0",
|
|
56
|
-
"tsdx": "^0.14.1",
|
|
57
|
-
"tslib": "^2.2.0"
|
|
58
|
-
},
|
|
59
38
|
"dependencies": {
|
|
60
|
-
"@plasmicapp/auth-api": "0.0.
|
|
39
|
+
"@plasmicapp/auth-api": "0.0.11",
|
|
61
40
|
"@plasmicapp/isomorphic-unfetch": "1.0.3",
|
|
62
|
-
"@plasmicapp/query": "0.1.
|
|
41
|
+
"@plasmicapp/query": "0.1.73"
|
|
63
42
|
},
|
|
64
43
|
"publishConfig": {
|
|
65
44
|
"access": "public"
|
|
66
45
|
},
|
|
67
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "061bfb393e0e5faabf92ef07b21e60107a5e7971",
|
|
68
47
|
"peerDependencies": {
|
|
69
48
|
"react": ">=16.8.0"
|
|
70
49
|
}
|