@holo-js/auth-social-discord 0.1.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.
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +90 -0
- package/package.json +35 -0
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
async function readJson(response) {
|
|
3
|
+
const text = await response.text();
|
|
4
|
+
return text ? JSON.parse(text) : {};
|
|
5
|
+
}
|
|
6
|
+
function applyScopes(url, config) {
|
|
7
|
+
const scopes = (config.scopes ?? []).length > 0 ? config.scopes ?? [] : ["identify", "email"];
|
|
8
|
+
url.searchParams.set("scope", scopes.join(" "));
|
|
9
|
+
}
|
|
10
|
+
async function exchangeToken(context) {
|
|
11
|
+
const body = new URLSearchParams({
|
|
12
|
+
code: context.code,
|
|
13
|
+
client_id: context.config.clientId ?? "",
|
|
14
|
+
client_secret: context.config.clientSecret ?? "",
|
|
15
|
+
redirect_uri: context.config.redirectUri ?? "",
|
|
16
|
+
grant_type: "authorization_code",
|
|
17
|
+
code_verifier: context.codeVerifier
|
|
18
|
+
});
|
|
19
|
+
const response = await fetch("https://discord.com/api/oauth2/token", {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers: {
|
|
22
|
+
accept: "application/json",
|
|
23
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
24
|
+
},
|
|
25
|
+
body
|
|
26
|
+
});
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error("[@holo-js/auth-social-discord] Discord token exchange failed.");
|
|
29
|
+
}
|
|
30
|
+
return await readJson(response);
|
|
31
|
+
}
|
|
32
|
+
function normalizeTokens(payload) {
|
|
33
|
+
const expiresIn = typeof payload.expires_in === "number" ? payload.expires_in : typeof payload.expires_in === "string" ? Number.parseInt(payload.expires_in, 10) : void 0;
|
|
34
|
+
return {
|
|
35
|
+
accessToken: String(payload.access_token ?? ""),
|
|
36
|
+
refreshToken: typeof payload.refresh_token === "string" ? payload.refresh_token : void 0,
|
|
37
|
+
expiresAt: Number.isFinite(expiresIn) ? new Date(Date.now() + expiresIn * 1e3) : void 0,
|
|
38
|
+
tokenType: payload.token_type,
|
|
39
|
+
scope: payload.scope
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function normalizeProfile(payload) {
|
|
43
|
+
const id = typeof payload.id === "string" ? payload.id : "";
|
|
44
|
+
if (!id) {
|
|
45
|
+
throw new Error('[@holo-js/auth-social-discord] Discord user profile did not include "id".');
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
id,
|
|
49
|
+
email: typeof payload.email === "string" ? payload.email : void 0,
|
|
50
|
+
emailVerified: payload.verified === true,
|
|
51
|
+
name: typeof payload.global_name === "string" ? payload.global_name : typeof payload.username === "string" ? payload.username : void 0,
|
|
52
|
+
avatar: typeof payload.avatar === "string" ? `https://cdn.discordapp.com/avatars/${id}/${payload.avatar}.png` : void 0
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
var discordSocialProvider = Object.freeze({
|
|
56
|
+
buildAuthorizationUrl(context) {
|
|
57
|
+
const url = new URL("https://discord.com/oauth2/authorize");
|
|
58
|
+
url.searchParams.set("client_id", context.config.clientId ?? "");
|
|
59
|
+
url.searchParams.set("redirect_uri", context.config.redirectUri ?? "");
|
|
60
|
+
url.searchParams.set("response_type", "code");
|
|
61
|
+
url.searchParams.set("state", context.state);
|
|
62
|
+
url.searchParams.set("code_challenge", context.codeChallenge);
|
|
63
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
64
|
+
applyScopes(url, context.config);
|
|
65
|
+
return url.toString();
|
|
66
|
+
},
|
|
67
|
+
async exchangeCode(context) {
|
|
68
|
+
const tokenPayload = await exchangeToken(context);
|
|
69
|
+
const accessToken = String(tokenPayload.access_token ?? "");
|
|
70
|
+
const response = await fetch("https://discord.com/api/users/@me", {
|
|
71
|
+
headers: {
|
|
72
|
+
authorization: `Bearer ${accessToken}`,
|
|
73
|
+
accept: "application/json"
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
throw new Error("[@holo-js/auth-social-discord] Discord user request failed.");
|
|
78
|
+
}
|
|
79
|
+
const payload = await readJson(response);
|
|
80
|
+
return {
|
|
81
|
+
profile: normalizeProfile(payload),
|
|
82
|
+
tokens: normalizeTokens(tokenPayload)
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
var src_default = discordSocialProvider;
|
|
87
|
+
export {
|
|
88
|
+
src_default as default,
|
|
89
|
+
discordSocialProvider
|
|
90
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@holo-js/auth-social-discord",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Holo-JS Framework - Discord social auth provider",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup",
|
|
21
|
+
"stub": "tsup",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
23
|
+
"test": "vitest --run"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@holo-js/auth-social": "workspace:*",
|
|
27
|
+
"@holo-js/config": "workspace:*"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^22.10.2",
|
|
31
|
+
"tsup": "^8.3.5",
|
|
32
|
+
"typescript": "^5.7.2",
|
|
33
|
+
"vitest": "^2.1.8"
|
|
34
|
+
}
|
|
35
|
+
}
|