@redzone/taunt-logins 0.0.4 → 0.0.6
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/cjs/index.cjs +814 -0
- package/dist/cjs/index.d.cts +403 -0
- package/dist/es/index.d.ts +403 -0
- package/dist/es/index.js +760 -0
- package/package.json +28 -16
- package/dist/index.d.ts +0 -3
- package/dist/magic.d.ts +0 -51
- package/dist/metamask.d.ts +0 -16
- package/dist/taunt-logins.d.ts +0 -2
- package/dist/taunt-logins.js +0 -240
- package/dist/taunt-logins.js.map +0 -1
- package/dist/taunt-logins.umd.cjs +0 -2
- package/dist/taunt-logins.umd.cjs.map +0 -1
- package/dist/taunt.d.ts +0 -50
package/package.json
CHANGED
|
@@ -1,38 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@redzone/taunt-logins",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/es/index.js",
|
|
10
|
+
"module": "./dist/es/index.js",
|
|
11
|
+
"types": "./dist/es/index.d.ts",
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
|
-
"import":
|
|
12
|
-
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/es/index.d.ts",
|
|
16
|
+
"default": "./dist/es/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/cjs/index.d.cts",
|
|
20
|
+
"default": "./dist/cjs/index.cjs"
|
|
21
|
+
}
|
|
13
22
|
}
|
|
14
23
|
},
|
|
15
24
|
"scripts": {
|
|
16
|
-
"build": "
|
|
17
|
-
"
|
|
25
|
+
"build": "bunchee",
|
|
26
|
+
"dev": "bunchee --watch",
|
|
27
|
+
"prepublish": "npm run build",
|
|
28
|
+
"publish": "npm publish --access public"
|
|
18
29
|
},
|
|
19
30
|
"dependencies": {
|
|
20
31
|
"@magic-ext/oauth2": "^12.0.0",
|
|
21
32
|
"@metamask/providers": "^22.1.1",
|
|
33
|
+
"axios": "^1.12.2",
|
|
22
34
|
"magic-sdk": "^30.0.0"
|
|
23
35
|
},
|
|
24
36
|
"devDependencies": {
|
|
37
|
+
"@repo/typescript-config": "*",
|
|
25
38
|
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
|
|
26
39
|
"@types/chrome": "^0.1.16",
|
|
27
40
|
"@types/node": "^24.6.1",
|
|
28
41
|
"@vitejs/plugin-react": "^5.0.4",
|
|
29
|
-
"
|
|
42
|
+
"@repo/eslint-config": "*",
|
|
43
|
+
"eslint": "latest",
|
|
44
|
+
"prettier": "latest",
|
|
45
|
+
"prettier-config-custom": "*",
|
|
46
|
+
"bunchee": "^6.4.0",
|
|
30
47
|
"tsup": "^8.5.0",
|
|
31
|
-
"typescript": "^5.9.3"
|
|
32
|
-
|
|
33
|
-
"vite-plugin-dts": "^4.5.4"
|
|
34
|
-
},
|
|
35
|
-
"files": [
|
|
36
|
-
"dist"
|
|
37
|
-
]
|
|
48
|
+
"typescript": "^5.9.3"
|
|
49
|
+
}
|
|
38
50
|
}
|
package/dist/index.d.ts
DELETED
package/dist/magic.d.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { OAuthRedirectError, OAuthRedirectResult } from '@magic-ext/oauth2';
|
|
2
|
-
export declare function emailOTPWithMagic(magicKey: string, email: string): import('magic-sdk').PromiEvent<string | null, {
|
|
3
|
-
"email-otp-sent": () => void;
|
|
4
|
-
"login-throttled": () => void;
|
|
5
|
-
"invalid-email-otp": () => void;
|
|
6
|
-
"invalid-mfa-otp": () => void;
|
|
7
|
-
"expired-email-otp": () => void;
|
|
8
|
-
"mfa-sent-handle": () => void;
|
|
9
|
-
"recovery-code-sent-handle": () => void;
|
|
10
|
-
"invalid-recovery-code": () => void;
|
|
11
|
-
"recovery-code-success": () => void;
|
|
12
|
-
"Auth/id-token-created": (idToken: string) => void;
|
|
13
|
-
"Wallet/wallet-info-fetched": () => void;
|
|
14
|
-
"verify-email-otp": (otp: string) => void;
|
|
15
|
-
"verify-mfa-code": (mfa: string) => void;
|
|
16
|
-
"lost-device": () => void;
|
|
17
|
-
"verify-recovery-code": (recoveryCode: string) => void;
|
|
18
|
-
cancel: () => void;
|
|
19
|
-
} & {
|
|
20
|
-
"device-needs-approval": () => void;
|
|
21
|
-
"device-verification-email-sent": () => void;
|
|
22
|
-
"device-verification-link-expired": () => void;
|
|
23
|
-
"device-approved": () => void;
|
|
24
|
-
"device-retry": () => void;
|
|
25
|
-
} & {
|
|
26
|
-
done: (result: string | null) => void;
|
|
27
|
-
error: (reason: any) => void;
|
|
28
|
-
settled: () => void;
|
|
29
|
-
"closed-by-user": () => void;
|
|
30
|
-
}>;
|
|
31
|
-
export declare function telegramWithMagic(magicKey: string): import('magic-sdk').PromiEvent<OAuthRedirectResult | OAuthRedirectError, {
|
|
32
|
-
done: (result: OAuthRedirectResult | OAuthRedirectError) => void;
|
|
33
|
-
error: (reason: any) => void;
|
|
34
|
-
settled: () => void;
|
|
35
|
-
"closed-by-user": () => void;
|
|
36
|
-
}>;
|
|
37
|
-
export declare function tauntMagicTelegramLogin(tauntServiceEndpoint: string, magicKey: string): Promise<{
|
|
38
|
-
accessToken: string;
|
|
39
|
-
refreshToken: string;
|
|
40
|
-
isNewUser: string;
|
|
41
|
-
}>;
|
|
42
|
-
export declare function tauntMagicEmailOTPLogin(tauntServiceEndpoint: string, magicKey: string, email: string): Promise<{
|
|
43
|
-
accessToken: string;
|
|
44
|
-
refreshToken: string;
|
|
45
|
-
isNewUser: string;
|
|
46
|
-
}>;
|
|
47
|
-
export declare function tauntMagicDidLogin(tauntServiceEndpoint: string, did: string): Promise<{
|
|
48
|
-
accessToken: string;
|
|
49
|
-
refreshToken: string;
|
|
50
|
-
isNewUser: string;
|
|
51
|
-
}>;
|
package/dist/metamask.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from '@metamask/providers';
|
|
2
|
-
declare global {
|
|
3
|
-
interface Window {
|
|
4
|
-
ethereum?: BaseProvider;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export declare function tauntSignWithMetamask(tauntServiceEndpoint: string, provider?: BaseProvider): Promise<{
|
|
8
|
-
walletAddress: string;
|
|
9
|
-
message: string;
|
|
10
|
-
signature: string;
|
|
11
|
-
}>;
|
|
12
|
-
export declare function tauntMetamaskLogin(tauntServiceEndpoint: string, providerParam?: BaseProvider): Promise<{
|
|
13
|
-
accessToken: string;
|
|
14
|
-
refreshToken: string;
|
|
15
|
-
isNewUser: string;
|
|
16
|
-
}>;
|
package/dist/taunt-logins.d.ts
DELETED
package/dist/taunt-logins.js
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import { OAuthExtension as w } from "@magic-ext/oauth2";
|
|
2
|
-
import { Magic as d } from "magic-sdk";
|
|
3
|
-
class i {
|
|
4
|
-
constructor(t, n = this) {
|
|
5
|
-
this.endpoint = t, this.api = n;
|
|
6
|
-
}
|
|
7
|
-
async stdPGET(t, n, e, o = {}) {
|
|
8
|
-
const r = await fetch(this.endpoint + n, {
|
|
9
|
-
method: t,
|
|
10
|
-
headers: {
|
|
11
|
-
"Content-Type": "application/json",
|
|
12
|
-
// Include credentials (cookies) in the request
|
|
13
|
-
Accept: "application/json",
|
|
14
|
-
...o
|
|
15
|
-
},
|
|
16
|
-
body: e ? JSON.stringify(e) : null,
|
|
17
|
-
credentials: "include"
|
|
18
|
-
// This ensures cookies are sent with the request
|
|
19
|
-
});
|
|
20
|
-
return this.withResp(r);
|
|
21
|
-
}
|
|
22
|
-
async withResp(t) {
|
|
23
|
-
if (!t.ok) {
|
|
24
|
-
const n = await t.json().catch(() => ({})), e = new Error("Network response was not ok");
|
|
25
|
-
throw Object.assign(e, { response: t, data: n }), e;
|
|
26
|
-
}
|
|
27
|
-
return t.json().then((n) => ({ data: n, response: t }));
|
|
28
|
-
}
|
|
29
|
-
async post(t, n = {}) {
|
|
30
|
-
return this.stdPGET("POST", t, n);
|
|
31
|
-
}
|
|
32
|
-
async get(t, n = {}) {
|
|
33
|
-
return this.stdPGET("GET", t, void 0, n);
|
|
34
|
-
}
|
|
35
|
-
async loginWithMagicDid(t) {
|
|
36
|
-
const { data: n } = await this.api.post("/v1/auth/login/did", { did: t });
|
|
37
|
-
return n;
|
|
38
|
-
}
|
|
39
|
-
async loginWithWeb3WalletSignature(t) {
|
|
40
|
-
const { data: n } = await this.api.post("/v1/auth/login/signature", t);
|
|
41
|
-
return n;
|
|
42
|
-
}
|
|
43
|
-
async loginExtWithWeb3WalletSignature(t) {
|
|
44
|
-
const { data: n } = await this.api.post("/v1/auth/login/ext-signature", t);
|
|
45
|
-
return n;
|
|
46
|
-
}
|
|
47
|
-
// Logout of the backend with access token. Assumes user is currently logged in and has a cookie with the access token
|
|
48
|
-
async logout(t) {
|
|
49
|
-
await this.api.post("/v1/auth/logout", { accessToken: t }), await this.writePlayerEvent("player_logged_out");
|
|
50
|
-
}
|
|
51
|
-
// Use the cookie stored in the browser to get the user and save user model in state and local storage
|
|
52
|
-
// This assumes that the user is logged to backend in and has a cookie jwt
|
|
53
|
-
async getLoggedInUser(t) {
|
|
54
|
-
const { data: n } = await this.api.get("/v1/auth/me", {
|
|
55
|
-
Authorization: `Bearer ${t}`
|
|
56
|
-
});
|
|
57
|
-
return n;
|
|
58
|
-
}
|
|
59
|
-
// async loginPlayFabPlayer() {}
|
|
60
|
-
/*async loginPlayFabPlayer() {
|
|
61
|
-
try {
|
|
62
|
-
// Get the user's playfab id from taunt api
|
|
63
|
-
let id = null;
|
|
64
|
-
if (!this.user && !this.user?.id) {
|
|
65
|
-
id = this.getPlayfabAnonId()
|
|
66
|
-
if (!id) {
|
|
67
|
-
const { data: playFabResponse } = await this.api.get('/v1/auth/playfab', {})
|
|
68
|
-
id = playFabResponse.playFabId
|
|
69
|
-
this.setAnonPlayfabUserId(playFabResponse.playFabId);
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
const { data: playFabResponse } = await this.api.get('/v1/auth/playfab', {})
|
|
73
|
-
id = playFabResponse.playFabId
|
|
74
|
-
}
|
|
75
|
-
// Login with playfab via the unique id from taunt api
|
|
76
|
-
const { data: playFabLoginResults } = await Axios.post(loginEndpoint, {
|
|
77
|
-
CustomId: id,
|
|
78
|
-
CreateAccount: true,
|
|
79
|
-
titleId: playFabTitleId,
|
|
80
|
-
})
|
|
81
|
-
const playFabSession = JSON.stringify(playFabLoginResults.data)
|
|
82
|
-
this.setPlayFabSession(playFabSession)
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
const err = error as AxiosError
|
|
86
|
-
if (err?.response) {
|
|
87
|
-
console.log(err.response.status, err.response.data)
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
console.log(error)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}*/
|
|
94
|
-
// async getCToken() {
|
|
95
|
-
// try {
|
|
96
|
-
// const url = "v1/claimr/token"
|
|
97
|
-
// const { data } = await this.api.get<{ data: { token: string } }>(`${url}`)
|
|
98
|
-
// this.claimrToken = data.data.token
|
|
99
|
-
// this.setClaimrToken(this.claimrToken)
|
|
100
|
-
// } catch (e) {
|
|
101
|
-
// console.error(e)
|
|
102
|
-
// }
|
|
103
|
-
// }
|
|
104
|
-
// setLastWalletConnection(lastWalletConnection: string) {
|
|
105
|
-
// localStorage.setItem("lastWalletConnection", lastWalletConnection)
|
|
106
|
-
// }
|
|
107
|
-
// setRefId(ref = "") {
|
|
108
|
-
// this.refId = ref
|
|
109
|
-
// }
|
|
110
|
-
// clearLocalAuthStorage() {
|
|
111
|
-
// localStorage.removeItem("user")
|
|
112
|
-
// localStorage.removeItem("refreshToken")
|
|
113
|
-
// localStorage.removeItem("playFabSession")
|
|
114
|
-
// this.user = null
|
|
115
|
-
// }
|
|
116
|
-
// setAccessToken(accessToken: string) {
|
|
117
|
-
// localStorage.setItem("accessToken", accessToken)
|
|
118
|
-
// }
|
|
119
|
-
// setRefreshToken(refreshToken: string) {
|
|
120
|
-
// localStorage.setItem("refreshToken", refreshToken)
|
|
121
|
-
// }
|
|
122
|
-
// setIsFirsTimeLogin(isFirstTimeLogin: string) {
|
|
123
|
-
// localStorage.setItem("isFirstTimeLogin", isFirstTimeLogin)
|
|
124
|
-
// }
|
|
125
|
-
// setPlayFabSession(playFabSession: string) {
|
|
126
|
-
// localStorage.setItem("playFabSession", playFabSession)
|
|
127
|
-
// }
|
|
128
|
-
// setAnonPlayfabUserId(id: string) {
|
|
129
|
-
// localStorage.setItem("playfabAnonId", id)
|
|
130
|
-
// }
|
|
131
|
-
// setClaimrToken(token: string) {
|
|
132
|
-
// localStorage.setItem("ctoken", token)
|
|
133
|
-
// }
|
|
134
|
-
// getAccessToken() {
|
|
135
|
-
// return localStorage.getItem("accessToken")
|
|
136
|
-
// }
|
|
137
|
-
// getRefreshToken() {
|
|
138
|
-
// return localStorage.getItem("refreshToken")
|
|
139
|
-
// }
|
|
140
|
-
// getPlayfabAnonId() {
|
|
141
|
-
// return localStorage.getItem("playfabAnonId")
|
|
142
|
-
// }
|
|
143
|
-
// Write a playfab event. retryfIfLoginNeeded should not be set by caller. It's used for stopping recursive calls when login is needed.
|
|
144
|
-
// If login is needed, for example, if the web browser has been open longer than playfab session timeout, we'll try to login again at most
|
|
145
|
-
// one time. Then we'll retry the event.
|
|
146
|
-
async writePlayerEvent(t, n = null, e = !0) {
|
|
147
|
-
return console.log("writePlayerEvent", {
|
|
148
|
-
eventName: t,
|
|
149
|
-
eventData: n,
|
|
150
|
-
retryIfLoginNeeded: e
|
|
151
|
-
}), Promise.resolve();
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
const p = {
|
|
155
|
-
rpcUrl: "https://polygon-rpc.com",
|
|
156
|
-
// your ethereum, polygon, or optimism mainnet/testnet rpc URL
|
|
157
|
-
chainId: 137
|
|
158
|
-
}, u = (a) => new d(a, {
|
|
159
|
-
network: p,
|
|
160
|
-
extensions: [new w()]
|
|
161
|
-
});
|
|
162
|
-
function y(a, t) {
|
|
163
|
-
return u(a).auth.loginWithEmailOTP({ email: t });
|
|
164
|
-
}
|
|
165
|
-
function m(a) {
|
|
166
|
-
return u(a).oauth2.loginWithPopup({
|
|
167
|
-
provider: "telegram"
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
async function T(a, t) {
|
|
171
|
-
const n = new i(a);
|
|
172
|
-
try {
|
|
173
|
-
let e = await m(t);
|
|
174
|
-
const o = e;
|
|
175
|
-
if (o.error)
|
|
176
|
-
throw new Error(`No ID token returned ${o.error}`);
|
|
177
|
-
return e = e, console.log("Magic telegram login", { result: e }), n.loginWithMagicDid(e.magic.idToken);
|
|
178
|
-
} catch (e) {
|
|
179
|
-
throw console.error("Magic login error:", e), e;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
async function P(a, t, n) {
|
|
183
|
-
const e = new i(a);
|
|
184
|
-
try {
|
|
185
|
-
const o = await y(t, n);
|
|
186
|
-
if (!o) throw new Error("No DID returned");
|
|
187
|
-
return console.log("Magic email OTP login", { did: o }), e.loginWithMagicDid(o);
|
|
188
|
-
} catch (o) {
|
|
189
|
-
throw console.error("Magic login error:", o), o;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
async function O(a, t) {
|
|
193
|
-
return new i(a).loginWithMagicDid(t);
|
|
194
|
-
}
|
|
195
|
-
async function f(a, t) {
|
|
196
|
-
if (t = t || window.ethereum, !t) throw new Error("MetaMask provider not found");
|
|
197
|
-
const n = await t.request({
|
|
198
|
-
method: "eth_requestAccounts"
|
|
199
|
-
});
|
|
200
|
-
if (!n.length) throw new Error("No accounts returned");
|
|
201
|
-
console.log("MetaMask", { accounts: n });
|
|
202
|
-
const e = n[0], o = `${a}/v1/auth`, r = new Uint32Array(3), c = crypto.getRandomValues(r).toString(), h = await fetch(`${o}/nonce/login`, {
|
|
203
|
-
method: "POST",
|
|
204
|
-
body: JSON.stringify({ walletAddress: e, clientNonce: c }),
|
|
205
|
-
headers: { "Content-Type": "application/json" }
|
|
206
|
-
}).then((l) => l.text()), g = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${JSON.stringify({ clientNonce: c, serverNonce: h })}`;
|
|
207
|
-
let s;
|
|
208
|
-
try {
|
|
209
|
-
s = await t.request({
|
|
210
|
-
method: "personal_sign",
|
|
211
|
-
params: [g, e]
|
|
212
|
-
});
|
|
213
|
-
} catch {
|
|
214
|
-
throw new Error("User denied message signature");
|
|
215
|
-
}
|
|
216
|
-
if (!s) throw new Error("No signature returned");
|
|
217
|
-
return { walletAddress: e, message: g, signature: s };
|
|
218
|
-
}
|
|
219
|
-
async function N(a, t) {
|
|
220
|
-
const n = new i(a), { walletAddress: e, message: o, signature: r } = await f(
|
|
221
|
-
a,
|
|
222
|
-
t
|
|
223
|
-
);
|
|
224
|
-
return n.loginWithWeb3WalletSignature({
|
|
225
|
-
walletAddress: e,
|
|
226
|
-
message: o,
|
|
227
|
-
signature: r
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
export {
|
|
231
|
-
i as TauntApi,
|
|
232
|
-
y as emailOTPWithMagic,
|
|
233
|
-
O as tauntMagicDidLogin,
|
|
234
|
-
P as tauntMagicEmailOTPLogin,
|
|
235
|
-
T as tauntMagicTelegramLogin,
|
|
236
|
-
N as tauntMetamaskLogin,
|
|
237
|
-
f as tauntSignWithMetamask,
|
|
238
|
-
m as telegramWithMagic
|
|
239
|
-
};
|
|
240
|
-
//# sourceMappingURL=taunt-logins.js.map
|
package/dist/taunt-logins.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"taunt-logins.js","sources":["../src/lib/taunt.ts","../src/lib/magic.ts","../src/lib/metamask.ts"],"sourcesContent":["export type TauntSigProps = {\n walletAddress: string\n message: string\n signature: string\n}\nexport type TauntExtSigProps = TauntSigProps & { extNonce: string }\n\ntype TauntRespType = {\n accessToken: string\n refreshToken: string\n isNewUser: string\n}\n\nexport type TauntUser = {\n id: number\n role: string\n email: string | undefined\n ethAddress: string | null\n avatar: string | null\n\n refreshToken: string\n accessToken: string\n}\n\nexport class TauntApi {\n constructor(\n private endpoint: string,\n private api = this\n ) {}\n\n async stdPGET<T = TauntRespType>(\n method: string,\n url: string,\n body?: unknown,\n headers = {}\n ) {\n const response = await fetch(this.endpoint + url, {\n method,\n headers: {\n \"Content-Type\": \"application/json\",\n // Include credentials (cookies) in the request\n Accept: \"application/json\",\n ...headers\n },\n body: body ? JSON.stringify(body) : null,\n credentials: \"include\" // This ensures cookies are sent with the request\n })\n return this.withResp<T>(response)\n }\n\n async withResp<T>(response: Response) {\n if (!response.ok) {\n const errorData = await response.json().catch(() => ({}))\n const error = new Error(\"Network response was not ok\")\n\n Object.assign(error, { response, data: errorData })\n throw error\n }\n return response.json().then((data) => ({ data: data as T, response }))\n }\n\n async post<T = TauntRespType>(url: string, body = {}) {\n return this.stdPGET<T>(\"POST\", url, body)\n }\n\n async get<T = TauntRespType>(url: string, headers = {}) {\n return this.stdPGET<T>(\"GET\", url, undefined, headers)\n }\n\n async loginWithMagicDid(did: string) {\n const { data } = await this.api.post(\"/v1/auth/login/did\", { did })\n return data\n }\n\n async loginWithWeb3WalletSignature(props: TauntSigProps) {\n const { data } = await this.api.post(\"/v1/auth/login/signature\", props)\n return data\n }\n\n async loginExtWithWeb3WalletSignature(props: TauntExtSigProps) {\n const { data } = await this.api.post(\"/v1/auth/login/ext-signature\", props)\n return data\n }\n\n // Logout of the backend with access token. Assumes user is currently logged in and has a cookie with the access token\n async logout(accessToken: string) {\n // If logging out fails on the backend we'll still remove the user info\n await this.api.post(\"/v1/auth/logout\", { accessToken })\n await this.writePlayerEvent(\"player_logged_out\")\n }\n\n // Use the cookie stored in the browser to get the user and save user model in state and local storage\n // This assumes that the user is logged to backend in and has a cookie jwt\n async getLoggedInUser(accessToken: string) {\n const { data } = await this.api.get<{ ethAddress: string }>(\"/v1/auth/me\", {\n Authorization: `Bearer ${accessToken}`\n })\n // this.user = data\n // localStorage.setItem(\"user\", JSON.stringify(data))\n // localStorage.setItem(\n // \"name\",\n // this.user.email ? this.user.email.split(\"@\")[0] : this.user.ethAddress\n // )\n // await this.getCToken()\n // this.setRefId(this.router.currentRoute.value.query['ref_id'])\n\n // const nftStore = useNftStore()\n // nftStore.fetchAll()\n return data as TauntUser\n }\n\n // async loginPlayFabPlayer() {}\n\n /*async loginPlayFabPlayer() {\n try {\n // Get the user's playfab id from taunt api\n let id = null;\n if (!this.user && !this.user?.id) {\n id = this.getPlayfabAnonId()\n if (!id) {\n const { data: playFabResponse } = await this.api.get('/v1/auth/playfab', {})\n id = playFabResponse.playFabId\n this.setAnonPlayfabUserId(playFabResponse.playFabId);\n }\n } else {\n const { data: playFabResponse } = await this.api.get('/v1/auth/playfab', {})\n id = playFabResponse.playFabId\n }\n // Login with playfab via the unique id from taunt api\n const { data: playFabLoginResults } = await Axios.post(loginEndpoint, {\n CustomId: id,\n CreateAccount: true,\n titleId: playFabTitleId,\n })\n const playFabSession = JSON.stringify(playFabLoginResults.data)\n this.setPlayFabSession(playFabSession)\n }\n catch (error) {\n const err = error as AxiosError\n if (err?.response) {\n console.log(err.response.status, err.response.data)\n }\n else {\n console.log(error)\n }\n }\n }*/\n\n // async getCToken() {\n // try {\n // const url = \"v1/claimr/token\"\n // const { data } = await this.api.get<{ data: { token: string } }>(`${url}`)\n // this.claimrToken = data.data.token\n // this.setClaimrToken(this.claimrToken)\n // } catch (e) {\n // console.error(e)\n // }\n // }\n\n // setLastWalletConnection(lastWalletConnection: string) {\n // localStorage.setItem(\"lastWalletConnection\", lastWalletConnection)\n // }\n // setRefId(ref = \"\") {\n // this.refId = ref\n // }\n\n // clearLocalAuthStorage() {\n // localStorage.removeItem(\"user\")\n // localStorage.removeItem(\"refreshToken\")\n // localStorage.removeItem(\"playFabSession\")\n // this.user = null\n // }\n\n // setAccessToken(accessToken: string) {\n // localStorage.setItem(\"accessToken\", accessToken)\n // }\n // setRefreshToken(refreshToken: string) {\n // localStorage.setItem(\"refreshToken\", refreshToken)\n // }\n // setIsFirsTimeLogin(isFirstTimeLogin: string) {\n // localStorage.setItem(\"isFirstTimeLogin\", isFirstTimeLogin)\n // }\n // setPlayFabSession(playFabSession: string) {\n // localStorage.setItem(\"playFabSession\", playFabSession)\n // }\n // setAnonPlayfabUserId(id: string) {\n // localStorage.setItem(\"playfabAnonId\", id)\n // }\n // setClaimrToken(token: string) {\n // localStorage.setItem(\"ctoken\", token)\n // }\n // getAccessToken() {\n // return localStorage.getItem(\"accessToken\")\n // }\n // getRefreshToken() {\n // return localStorage.getItem(\"refreshToken\")\n // }\n // getPlayfabAnonId() {\n // return localStorage.getItem(\"playfabAnonId\")\n // }\n\n // Write a playfab event. retryfIfLoginNeeded should not be set by caller. It's used for stopping recursive calls when login is needed.\n // If login is needed, for example, if the web browser has been open longer than playfab session timeout, we'll try to login again at most\n // one time. Then we'll retry the event.\n async writePlayerEvent(\n eventName: string,\n eventData: unknown = null,\n retryIfLoginNeeded = true\n ) {\n console.log(\"writePlayerEvent\", {\n eventName,\n eventData,\n retryIfLoginNeeded\n })\n return Promise.resolve()\n }\n}\n","import {\n OAuthExtension,\n OAuthRedirectError,\n OAuthRedirectResult\n} from \"@magic-ext/oauth2\"\nimport { Magic } from \"magic-sdk\"\n\nimport { TauntApi } from \"./taunt\"\n\nconst customNodeOptions = {\n rpcUrl: \"https://polygon-rpc.com\", // your ethereum, polygon, or optimism mainnet/testnet rpc URL\n chainId: 137\n}\nconst getMagic = (magicKey: string) =>\n new Magic(magicKey, {\n network: customNodeOptions,\n extensions: [new OAuthExtension()]\n })\n\nexport function emailOTPWithMagic(magicKey: string, email: string) {\n const magic = getMagic(magicKey)\n return magic.auth.loginWithEmailOTP({ email })\n}\n\nexport function telegramWithMagic(magicKey: string) {\n const magic = getMagic(magicKey)\n return magic.oauth2.loginWithPopup({\n provider: \"telegram\"\n })\n}\n\nexport async function tauntMagicTelegramLogin(\n tauntServiceEndpoint: string,\n magicKey: string\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n try {\n let result = await telegramWithMagic(magicKey)\n const errResult = result as OAuthRedirectError\n if (errResult.error) {\n throw new Error(`No ID token returned ${errResult.error}`)\n }\n result = result as OAuthRedirectResult\n console.log(\"Magic telegram login\", { result })\n return taunt.loginWithMagicDid(result.magic.idToken)\n } catch (err) {\n console.error(\"Magic login error:\", err)\n throw err\n }\n}\n\nexport async function tauntMagicEmailOTPLogin(\n tauntServiceEndpoint: string,\n magicKey: string,\n email: string\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n try {\n const did = await emailOTPWithMagic(magicKey, email)\n if (!did) throw new Error(\"No DID returned\")\n console.log(\"Magic email OTP login\", { did })\n return taunt.loginWithMagicDid(did)\n } catch (err) {\n console.error(\"Magic login error:\", err)\n throw err\n }\n}\n\nexport async function tauntMagicDidLogin(\n tauntServiceEndpoint: string,\n did: string\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n return taunt.loginWithMagicDid(did)\n}\n","import { BaseProvider } from \"@metamask/providers\"\n\nimport { TauntApi } from \"./taunt\"\n\ndeclare global {\n interface Window {\n ethereum?: BaseProvider\n }\n}\n\nexport async function tauntSignWithMetamask(\n tauntServiceEndpoint: string,\n provider?: BaseProvider\n) {\n provider = provider || window.ethereum\n if (!provider) throw new Error(\"MetaMask provider not found\")\n\n const accounts = (await provider.request({\n method: \"eth_requestAccounts\"\n })) as string[]\n if (!accounts.length) throw new Error(\"No accounts returned\")\n\n console.log(\"MetaMask\", { accounts })\n\n const walletAddress = accounts[0]\n const baseUrl = `${tauntServiceEndpoint}/v1/auth`\n const cryptoValuesArray = new Uint32Array(3)\n const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()\n\n const serverNonce = await fetch(`${baseUrl}/nonce/login`, {\n method: \"POST\",\n body: JSON.stringify({ walletAddress, clientNonce }),\n headers: { \"Content-Type\": \"application/json\" }\n }).then((res) => res.text())\n\n const payload = JSON.stringify({ clientNonce, serverNonce })\n const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`\n let signature\n try {\n signature = await provider.request<string>({\n method: \"personal_sign\",\n params: [message, walletAddress]\n })\n } catch (err) {\n throw new Error(\"User denied message signature\")\n }\n if (!signature) throw new Error(\"No signature returned\")\n\n return { walletAddress, message, signature }\n}\n\nexport async function tauntMetamaskLogin(\n tauntServiceEndpoint: string,\n providerParam?: BaseProvider\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n const { walletAddress, message, signature } = await tauntSignWithMetamask(\n tauntServiceEndpoint,\n providerParam\n )\n return taunt.loginWithWeb3WalletSignature({\n walletAddress,\n message,\n signature\n })\n}\n"],"names":["TauntApi","endpoint","api","method","url","body","headers","response","errorData","error","data","did","props","accessToken","eventName","eventData","retryIfLoginNeeded","customNodeOptions","getMagic","magicKey","Magic","OAuthExtension","emailOTPWithMagic","email","telegramWithMagic","tauntMagicTelegramLogin","tauntServiceEndpoint","taunt","result","errResult","err","tauntMagicEmailOTPLogin","tauntMagicDidLogin","tauntSignWithMetamask","provider","accounts","walletAddress","baseUrl","cryptoValuesArray","clientNonce","serverNonce","res","message","signature","tauntMetamaskLogin","providerParam"],"mappings":";;AAwBO,MAAMA,EAAS;AAAA,EACpB,YACUC,GACAC,IAAM,MACd;AAFQ,SAAA,WAAAD,GACA,KAAA,MAAAC;AAAA,EACP;AAAA,EAEH,MAAM,QACJC,GACAC,GACAC,GACAC,IAAU,CAAA,GACV;AACA,UAAMC,IAAW,MAAM,MAAM,KAAK,WAAWH,GAAK;AAAA,MAChD,QAAAD;AAAA,MACA,SAAS;AAAA,QACP,gBAAgB;AAAA;AAAA,QAEhB,QAAQ;AAAA,QACR,GAAGG;AAAA,MAAA;AAAA,MAEL,MAAMD,IAAO,KAAK,UAAUA,CAAI,IAAI;AAAA,MACpC,aAAa;AA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C1BE,IAAU,GAAGX,CAAoB,YACjCY,IAAoB,IAAI,YAAY,CAAC,GACrCC,IAAc,OAAO,gBAAgBD,CAAiB,EAAE,SAAA,GAExDE,IAAc,MAAM,MAAM,GAAGH,CAAO,gBAAgB;AAAA,IACxD,QAAQ;AAAA,IACR,MAAM,KAAK,UAAU,EAAE,eAAAD,GAAe,aAAAG,GAAa;AAAA,IACnD,SAAS,EAAE,gBAAgB,mBAAA;AAAA,EAAmB,CAC/C,EAAE,KAAK,CAACE,MAAQA,EAAI,MAAM,GAGrBC,IAAU,0EADA,KAAK,UAAU,EAAE,aAAAH,GAAa,aAAAC,GAAa,CACsC;AACjG,MAAIG;AACJ,MAAI;AACF,IAAAA,IAAY,MAAMT,EAAS,QAAgB;AAAA,MACzC,QAAQ;AAAA,MACR,QAAQ,CAACQ,GAASN,CAAa;AAAA,IAAA,CAChC;AAAA,EACH,QAAc;AACZ,UAAM,IAAI,MAAM,+BAA+B;AAAA,EACjD;AACA,MAAI,CAACO,EAAW,OAAM,IAAI,MAAM,uBAAuB;AAEvD,SAAO,EAAE,eAAAP,GAAe,SAAAM,GAAS,WAAAC,EAAA;AACnC;AAEA,eAAsBC,EACpBlB,GACAmB,GACA;AACA,QAAMlB,IAAQ,IAAI3B,EAAS0B,CAAoB,GACzC,EAAE,eAAAU,GAAe,SAAAM,GAAS,WAAAC,EAAA,IAAc,MAAMV;AAAA,IAClDP;AAAA,IACAmB;AAAA,EAAA;AAEF,SAAOlB,EAAM,6BAA6B;AAAA,IACxC,eAAAS;AAAA,IACA,SAAAM;AAAA,IACA,WAAAC;AAAA,EAAA,CACD;AACH;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
(function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@magic-ext/oauth2"),require("magic-sdk")):typeof define=="function"&&define.amd?define(["exports","@magic-ext/oauth2","magic-sdk"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i["@redzone/taunt-logins"]={},i["@magic-ext/oauth2"],i["magic-sdk"]))})(this,(function(i,r,p){"use strict";class s{constructor(t,n=this){this.endpoint=t,this.api=n}async stdPGET(t,n,e,o={}){const c=await fetch(this.endpoint+n,{method:t,headers:{"Content-Type":"application/json",Accept:"application/json",...o},body:e?JSON.stringify(e):null,credentials:"include"});return this.withResp(c)}async withResp(t){if(!t.ok){const n=await t.json().catch(()=>({})),e=new Error("Network response was not ok");throw Object.assign(e,{response:t,data:n}),e}return t.json().then(n=>({data:n,response:t}))}async post(t,n={}){return this.stdPGET("POST",t,n)}async get(t,n={}){return this.stdPGET("GET",t,void 0,n)}async loginWithMagicDid(t){const{data:n}=await this.api.post("/v1/auth/login/did",{did:t});return n}async loginWithWeb3WalletSignature(t){const{data:n}=await this.api.post("/v1/auth/login/signature",t);return n}async loginExtWithWeb3WalletSignature(t){const{data:n}=await this.api.post("/v1/auth/login/ext-signature",t);return n}async logout(t){await this.api.post("/v1/auth/logout",{accessToken:t}),await this.writePlayerEvent("player_logged_out")}async getLoggedInUser(t){const{data:n}=await this.api.get("/v1/auth/me",{Authorization:`Bearer ${t}`});return n}async writePlayerEvent(t,n=null,e=!0){return console.log("writePlayerEvent",{eventName:t,eventData:n,retryIfLoginNeeded:e}),Promise.resolve()}}const f={rpcUrl:"https://polygon-rpc.com",chainId:137},u=a=>new p.Magic(a,{network:f,extensions:[new r.OAuthExtension]});function l(a,t){return u(a).auth.loginWithEmailOTP({email:t})}function h(a){return u(a).oauth2.loginWithPopup({provider:"telegram"})}async function M(a,t){const n=new s(a);try{let e=await h(t);const o=e;if(o.error)throw new Error(`No ID token returned ${o.error}`);return e=e,console.log("Magic telegram login",{result:e}),n.loginWithMagicDid(e.magic.idToken)}catch(e){throw console.error("Magic login error:",e),e}}async function T(a,t,n){const e=new s(a);try{const o=await l(t,n);if(!o)throw new Error("No DID returned");return console.log("Magic email OTP login",{did:o}),e.loginWithMagicDid(o)}catch(o){throw console.error("Magic login error:",o),o}}async function W(a,t){return new s(a).loginWithMagicDid(t)}async function d(a,t){if(t=t||window.ethereum,!t)throw new Error("MetaMask provider not found");const n=await t.request({method:"eth_requestAccounts"});if(!n.length)throw new Error("No accounts returned");console.log("MetaMask",{accounts:n});const e=n[0],o=`${a}/v1/auth`,c=new Uint32Array(3),w=crypto.getRandomValues(c).toString(),P=await fetch(`${o}/nonce/login`,{method:"POST",body:JSON.stringify({walletAddress:e,clientNonce:w}),headers:{"Content-Type":"application/json"}}).then(y=>y.text()),m=`Sign this message with your wallet to connect to Taunt Battleworld ::: ${JSON.stringify({clientNonce:w,serverNonce:P})}`;let g;try{g=await t.request({method:"personal_sign",params:[m,e]})}catch{throw new Error("User denied message signature")}if(!g)throw new Error("No signature returned");return{walletAddress:e,message:m,signature:g}}async function E(a,t){const n=new s(a),{walletAddress:e,message:o,signature:c}=await d(a,t);return n.loginWithWeb3WalletSignature({walletAddress:e,message:o,signature:c})}i.TauntApi=s,i.emailOTPWithMagic=l,i.tauntMagicDidLogin=W,i.tauntMagicEmailOTPLogin=T,i.tauntMagicTelegramLogin=M,i.tauntMetamaskLogin=E,i.tauntSignWithMetamask=d,i.telegramWithMagic=h,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
|
2
|
-
//# sourceMappingURL=taunt-logins.umd.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"taunt-logins.umd.cjs","sources":["../src/lib/taunt.ts","../src/lib/magic.ts","../src/lib/metamask.ts"],"sourcesContent":["export type TauntSigProps = {\n walletAddress: string\n message: string\n signature: string\n}\nexport type TauntExtSigProps = TauntSigProps & { extNonce: string }\n\ntype TauntRespType = {\n accessToken: string\n refreshToken: string\n isNewUser: string\n}\n\nexport type TauntUser = {\n id: number\n role: string\n email: string | undefined\n ethAddress: string | null\n avatar: string | null\n\n refreshToken: string\n accessToken: string\n}\n\nexport class TauntApi {\n constructor(\n private endpoint: string,\n private api = this\n ) {}\n\n async stdPGET<T = TauntRespType>(\n method: string,\n url: string,\n body?: unknown,\n headers = {}\n ) {\n const response = await fetch(this.endpoint + url, {\n method,\n headers: {\n \"Content-Type\": \"application/json\",\n // Include credentials (cookies) in the request\n Accept: \"application/json\",\n ...headers\n },\n body: body ? JSON.stringify(body) : null,\n credentials: \"include\" // This ensures cookies are sent with the request\n })\n return this.withResp<T>(response)\n }\n\n async withResp<T>(response: Response) {\n if (!response.ok) {\n const errorData = await response.json().catch(() => ({}))\n const error = new Error(\"Network response was not ok\")\n\n Object.assign(error, { response, data: errorData })\n throw error\n }\n return response.json().then((data) => ({ data: data as T, response }))\n }\n\n async post<T = TauntRespType>(url: string, body = {}) {\n return this.stdPGET<T>(\"POST\", url, body)\n }\n\n async get<T = TauntRespType>(url: string, headers = {}) {\n return this.stdPGET<T>(\"GET\", url, undefined, headers)\n }\n\n async loginWithMagicDid(did: string) {\n const { data } = await this.api.post(\"/v1/auth/login/did\", { did })\n return data\n }\n\n async loginWithWeb3WalletSignature(props: TauntSigProps) {\n const { data } = await this.api.post(\"/v1/auth/login/signature\", props)\n return data\n }\n\n async loginExtWithWeb3WalletSignature(props: TauntExtSigProps) {\n const { data } = await this.api.post(\"/v1/auth/login/ext-signature\", props)\n return data\n }\n\n // Logout of the backend with access token. Assumes user is currently logged in and has a cookie with the access token\n async logout(accessToken: string) {\n // If logging out fails on the backend we'll still remove the user info\n await this.api.post(\"/v1/auth/logout\", { accessToken })\n await this.writePlayerEvent(\"player_logged_out\")\n }\n\n // Use the cookie stored in the browser to get the user and save user model in state and local storage\n // This assumes that the user is logged to backend in and has a cookie jwt\n async getLoggedInUser(accessToken: string) {\n const { data } = await this.api.get<{ ethAddress: string }>(\"/v1/auth/me\", {\n Authorization: `Bearer ${accessToken}`\n })\n // this.user = data\n // localStorage.setItem(\"user\", JSON.stringify(data))\n // localStorage.setItem(\n // \"name\",\n // this.user.email ? this.user.email.split(\"@\")[0] : this.user.ethAddress\n // )\n // await this.getCToken()\n // this.setRefId(this.router.currentRoute.value.query['ref_id'])\n\n // const nftStore = useNftStore()\n // nftStore.fetchAll()\n return data as TauntUser\n }\n\n // async loginPlayFabPlayer() {}\n\n /*async loginPlayFabPlayer() {\n try {\n // Get the user's playfab id from taunt api\n let id = null;\n if (!this.user && !this.user?.id) {\n id = this.getPlayfabAnonId()\n if (!id) {\n const { data: playFabResponse } = await this.api.get('/v1/auth/playfab', {})\n id = playFabResponse.playFabId\n this.setAnonPlayfabUserId(playFabResponse.playFabId);\n }\n } else {\n const { data: playFabResponse } = await this.api.get('/v1/auth/playfab', {})\n id = playFabResponse.playFabId\n }\n // Login with playfab via the unique id from taunt api\n const { data: playFabLoginResults } = await Axios.post(loginEndpoint, {\n CustomId: id,\n CreateAccount: true,\n titleId: playFabTitleId,\n })\n const playFabSession = JSON.stringify(playFabLoginResults.data)\n this.setPlayFabSession(playFabSession)\n }\n catch (error) {\n const err = error as AxiosError\n if (err?.response) {\n console.log(err.response.status, err.response.data)\n }\n else {\n console.log(error)\n }\n }\n }*/\n\n // async getCToken() {\n // try {\n // const url = \"v1/claimr/token\"\n // const { data } = await this.api.get<{ data: { token: string } }>(`${url}`)\n // this.claimrToken = data.data.token\n // this.setClaimrToken(this.claimrToken)\n // } catch (e) {\n // console.error(e)\n // }\n // }\n\n // setLastWalletConnection(lastWalletConnection: string) {\n // localStorage.setItem(\"lastWalletConnection\", lastWalletConnection)\n // }\n // setRefId(ref = \"\") {\n // this.refId = ref\n // }\n\n // clearLocalAuthStorage() {\n // localStorage.removeItem(\"user\")\n // localStorage.removeItem(\"refreshToken\")\n // localStorage.removeItem(\"playFabSession\")\n // this.user = null\n // }\n\n // setAccessToken(accessToken: string) {\n // localStorage.setItem(\"accessToken\", accessToken)\n // }\n // setRefreshToken(refreshToken: string) {\n // localStorage.setItem(\"refreshToken\", refreshToken)\n // }\n // setIsFirsTimeLogin(isFirstTimeLogin: string) {\n // localStorage.setItem(\"isFirstTimeLogin\", isFirstTimeLogin)\n // }\n // setPlayFabSession(playFabSession: string) {\n // localStorage.setItem(\"playFabSession\", playFabSession)\n // }\n // setAnonPlayfabUserId(id: string) {\n // localStorage.setItem(\"playfabAnonId\", id)\n // }\n // setClaimrToken(token: string) {\n // localStorage.setItem(\"ctoken\", token)\n // }\n // getAccessToken() {\n // return localStorage.getItem(\"accessToken\")\n // }\n // getRefreshToken() {\n // return localStorage.getItem(\"refreshToken\")\n // }\n // getPlayfabAnonId() {\n // return localStorage.getItem(\"playfabAnonId\")\n // }\n\n // Write a playfab event. retryfIfLoginNeeded should not be set by caller. It's used for stopping recursive calls when login is needed.\n // If login is needed, for example, if the web browser has been open longer than playfab session timeout, we'll try to login again at most\n // one time. Then we'll retry the event.\n async writePlayerEvent(\n eventName: string,\n eventData: unknown = null,\n retryIfLoginNeeded = true\n ) {\n console.log(\"writePlayerEvent\", {\n eventName,\n eventData,\n retryIfLoginNeeded\n })\n return Promise.resolve()\n }\n}\n","import {\n OAuthExtension,\n OAuthRedirectError,\n OAuthRedirectResult\n} from \"@magic-ext/oauth2\"\nimport { Magic } from \"magic-sdk\"\n\nimport { TauntApi } from \"./taunt\"\n\nconst customNodeOptions = {\n rpcUrl: \"https://polygon-rpc.com\", // your ethereum, polygon, or optimism mainnet/testnet rpc URL\n chainId: 137\n}\nconst getMagic = (magicKey: string) =>\n new Magic(magicKey, {\n network: customNodeOptions,\n extensions: [new OAuthExtension()]\n })\n\nexport function emailOTPWithMagic(magicKey: string, email: string) {\n const magic = getMagic(magicKey)\n return magic.auth.loginWithEmailOTP({ email })\n}\n\nexport function telegramWithMagic(magicKey: string) {\n const magic = getMagic(magicKey)\n return magic.oauth2.loginWithPopup({\n provider: \"telegram\"\n })\n}\n\nexport async function tauntMagicTelegramLogin(\n tauntServiceEndpoint: string,\n magicKey: string\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n try {\n let result = await telegramWithMagic(magicKey)\n const errResult = result as OAuthRedirectError\n if (errResult.error) {\n throw new Error(`No ID token returned ${errResult.error}`)\n }\n result = result as OAuthRedirectResult\n console.log(\"Magic telegram login\", { result })\n return taunt.loginWithMagicDid(result.magic.idToken)\n } catch (err) {\n console.error(\"Magic login error:\", err)\n throw err\n }\n}\n\nexport async function tauntMagicEmailOTPLogin(\n tauntServiceEndpoint: string,\n magicKey: string,\n email: string\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n try {\n const did = await emailOTPWithMagic(magicKey, email)\n if (!did) throw new Error(\"No DID returned\")\n console.log(\"Magic email OTP login\", { did })\n return taunt.loginWithMagicDid(did)\n } catch (err) {\n console.error(\"Magic login error:\", err)\n throw err\n }\n}\n\nexport async function tauntMagicDidLogin(\n tauntServiceEndpoint: string,\n did: string\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n return taunt.loginWithMagicDid(did)\n}\n","import { BaseProvider } from \"@metamask/providers\"\n\nimport { TauntApi } from \"./taunt\"\n\ndeclare global {\n interface Window {\n ethereum?: BaseProvider\n }\n}\n\nexport async function tauntSignWithMetamask(\n tauntServiceEndpoint: string,\n provider?: BaseProvider\n) {\n provider = provider || window.ethereum\n if (!provider) throw new Error(\"MetaMask provider not found\")\n\n const accounts = (await provider.request({\n method: \"eth_requestAccounts\"\n })) as string[]\n if (!accounts.length) throw new Error(\"No accounts returned\")\n\n console.log(\"MetaMask\", { accounts })\n\n const walletAddress = accounts[0]\n const baseUrl = `${tauntServiceEndpoint}/v1/auth`\n const cryptoValuesArray = new Uint32Array(3)\n const clientNonce = crypto.getRandomValues(cryptoValuesArray).toString()\n\n const serverNonce = await fetch(`${baseUrl}/nonce/login`, {\n method: \"POST\",\n body: JSON.stringify({ walletAddress, clientNonce }),\n headers: { \"Content-Type\": \"application/json\" }\n }).then((res) => res.text())\n\n const payload = JSON.stringify({ clientNonce, serverNonce })\n const message = `Sign this message with your wallet to connect to Taunt Battleworld ::: ${payload}`\n let signature\n try {\n signature = await provider.request<string>({\n method: \"personal_sign\",\n params: [message, walletAddress]\n })\n } catch (err) {\n throw new Error(\"User denied message signature\")\n }\n if (!signature) throw new Error(\"No signature returned\")\n\n return { walletAddress, message, signature }\n}\n\nexport async function tauntMetamaskLogin(\n tauntServiceEndpoint: string,\n providerParam?: BaseProvider\n) {\n const taunt = new TauntApi(tauntServiceEndpoint)\n const { walletAddress, message, signature } = await tauntSignWithMetamask(\n tauntServiceEndpoint,\n providerParam\n )\n return taunt.loginWithWeb3WalletSignature({\n walletAddress,\n message,\n signature\n })\n}\n"],"names":["TauntApi","endpoint","api","method","url","body","headers","response","errorData","error","data","did","props","accessToken","eventName","eventData","retryIfLoginNeeded","customNodeOptions","getMagic","magicKey","Magic","OAuthExtension","emailOTPWithMagic","email","telegramWithMagic","tauntMagicTelegramLogin","tauntServiceEndpoint","taunt","result","errResult","err","tauntMagicEmailOTPLogin","tauntMagicDidLogin","tauntSignWithMetamask","provider","accounts","walletAddress","baseUrl","cryptoValuesArray","clientNonce","serverNonce","res","message","signature","tauntMetamaskLogin","providerParam"],"mappings":"6WAwBO,MAAMA,CAAS,CACpB,YACUC,EACAC,EAAM,KACd,CAFQ,KAAA,SAAAD,EACA,KAAA,IAAAC,CACP,CAEH,MAAM,QACJC,EACAC,EACAC,EACAC,EAAU,CAAA,EACV,CACA,MAAMC,EAAW,MAAM,MAAM,KAAK,SAAWH,EAAK,CAChD,OAAAD,EACA,QAAS,CACP,eAAgB,mBAEhB,OAAQ,mBACR,GAAGG,CAAA,EAEL,KAAMD,EAAO,KAAK,UAAUA,CAAI,EAAI,KACpC,YAAa,SAAA,CACd,EACD,OAAO,KAAK,SAAYE,CAAQ,CAClC,CAEA,MAAM,SAAYA,EAAoB,CACpC,GAAI,CAACA,EAAS,GAAI,CAChB,MAAMC,EAAY,MAAMD,EAAS,KAAA,EAAO,MAAM,KAAO,CAAA,EAAG,EAClDE,EAAQ,IAAI,MAAM,6BAA6B,EAErD,aAAO,OAAOA,EAAO,CAAE,SAAAF,EAAU,KAAMC,EAAW,EAC5CC,CACR,CACA,OAAOF,EAAS,OAAO,KAAMG,IAAU,CAAE,KAAAA,EAAiB,SAAAH,CAAA,EAAW,CACvE,CA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I,WAAY,CAAE,SAAAA,CAAA,CAAU,EAEpC,MAAMC,EAAgBD,EAAS,CAAC,EAC1BE,EAAU,GAAGX,CAAoB,WACjCY,EAAoB,IAAI,YAAY,CAAC,EACrCC,EAAc,OAAO,gBAAgBD,CAAiB,EAAE,SAAA,EAExDE,EAAc,MAAM,MAAM,GAAGH,CAAO,eAAgB,CACxD,OAAQ,OACR,KAAM,KAAK,UAAU,CAAE,cAAAD,EAAe,YAAAG,EAAa,EACnD,QAAS,CAAE,eAAgB,kBAAA,CAAmB,CAC/C,EAAE,KAAME,GAAQA,EAAI,MAAM,EAGrBC,EAAU,0EADA,KAAK,UAAU,CAAE,YAAAH,EAAa,YAAAC,EAAa,CACsC,GACjG,IAAIG,EACJ,GAAI,CACFA,EAAY,MAAMT,EAAS,QAAgB,CACzC,OAAQ,gBACR,OAAQ,CAACQ,EAASN,CAAa,CAAA,CAChC,CACH,MAAc,CACZ,MAAM,IAAI,MAAM,+BAA+B,CACjD,CACA,GAAI,CAACO,EAAW,MAAM,IAAI,MAAM,uBAAuB,EAEvD,MAAO,CAAE,cAAAP,EAAe,QAAAM,EAAS,UAAAC,CAAA,CACnC,CAEA,eAAsBC,EACpBlB,EACAmB,EACA,CACA,MAAMlB,EAAQ,IAAI3B,EAAS0B,CAAoB,EACzC,CAAE,cAAAU,EAAe,QAAAM,EAAS,UAAAC,CAAA,EAAc,MAAMV,EAClDP,EACAmB,CAAA,EAEF,OAAOlB,EAAM,6BAA6B,CACxC,cAAAS,EACA,QAAAM,EACA,UAAAC,CAAA,CACD,CACH"}
|
package/dist/taunt.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export type TauntSigProps = {
|
|
2
|
-
walletAddress: string;
|
|
3
|
-
message: string;
|
|
4
|
-
signature: string;
|
|
5
|
-
};
|
|
6
|
-
export type TauntExtSigProps = TauntSigProps & {
|
|
7
|
-
extNonce: string;
|
|
8
|
-
};
|
|
9
|
-
type TauntRespType = {
|
|
10
|
-
accessToken: string;
|
|
11
|
-
refreshToken: string;
|
|
12
|
-
isNewUser: string;
|
|
13
|
-
};
|
|
14
|
-
export type TauntUser = {
|
|
15
|
-
id: number;
|
|
16
|
-
role: string;
|
|
17
|
-
email: string | undefined;
|
|
18
|
-
ethAddress: string | null;
|
|
19
|
-
avatar: string | null;
|
|
20
|
-
refreshToken: string;
|
|
21
|
-
accessToken: string;
|
|
22
|
-
};
|
|
23
|
-
export declare class TauntApi {
|
|
24
|
-
private endpoint;
|
|
25
|
-
private api;
|
|
26
|
-
constructor(endpoint: string, api?: this);
|
|
27
|
-
stdPGET<T = TauntRespType>(method: string, url: string, body?: unknown, headers?: {}): Promise<{
|
|
28
|
-
data: T;
|
|
29
|
-
response: Response;
|
|
30
|
-
}>;
|
|
31
|
-
withResp<T>(response: Response): Promise<{
|
|
32
|
-
data: T;
|
|
33
|
-
response: Response;
|
|
34
|
-
}>;
|
|
35
|
-
post<T = TauntRespType>(url: string, body?: {}): Promise<{
|
|
36
|
-
data: T;
|
|
37
|
-
response: Response;
|
|
38
|
-
}>;
|
|
39
|
-
get<T = TauntRespType>(url: string, headers?: {}): Promise<{
|
|
40
|
-
data: T;
|
|
41
|
-
response: Response;
|
|
42
|
-
}>;
|
|
43
|
-
loginWithMagicDid(did: string): Promise<TauntRespType>;
|
|
44
|
-
loginWithWeb3WalletSignature(props: TauntSigProps): Promise<TauntRespType>;
|
|
45
|
-
loginExtWithWeb3WalletSignature(props: TauntExtSigProps): Promise<TauntRespType>;
|
|
46
|
-
logout(accessToken: string): Promise<void>;
|
|
47
|
-
getLoggedInUser(accessToken: string): Promise<TauntUser>;
|
|
48
|
-
writePlayerEvent(eventName: string, eventData?: unknown, retryIfLoginNeeded?: boolean): Promise<void>;
|
|
49
|
-
}
|
|
50
|
-
export {};
|