@holo-js/auth-social-github 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.
@@ -0,0 +1,5 @@
1
+ import { SocialProviderRuntime } from '@holo-js/auth-social';
2
+
3
+ declare const githubSocialProvider: SocialProviderRuntime;
4
+
5
+ export { githubSocialProvider as default, githubSocialProvider };
package/dist/index.mjs ADDED
@@ -0,0 +1,91 @@
1
+ // src/index.ts
2
+ function applyScopes(url, config) {
3
+ const scopes = (config.scopes ?? []).length > 0 ? config.scopes ?? [] : ["read:user", "user:email"];
4
+ url.searchParams.set("scope", scopes.join(" "));
5
+ }
6
+ async function readJson(response) {
7
+ const text = await response.text();
8
+ return text ? JSON.parse(text) : {};
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
+ });
17
+ const response = await fetch("https://github.com/login/oauth/access_token", {
18
+ method: "POST",
19
+ headers: {
20
+ accept: "application/json",
21
+ "content-type": "application/x-www-form-urlencoded"
22
+ },
23
+ body
24
+ });
25
+ if (!response.ok) {
26
+ throw new Error("[@holo-js/auth-social-github] GitHub token exchange failed.");
27
+ }
28
+ return await readJson(response);
29
+ }
30
+ function normalizeTokens(payload) {
31
+ return {
32
+ accessToken: String(payload.access_token ?? ""),
33
+ refreshToken: typeof payload.refresh_token === "string" ? payload.refresh_token : void 0,
34
+ refreshTokenExpiresIn: payload.refresh_token_expires_in,
35
+ tokenType: payload.token_type,
36
+ scope: payload.scope
37
+ };
38
+ }
39
+ function normalizeProfile(profilePayload, emailsPayload) {
40
+ const id = profilePayload.id;
41
+ if (typeof id !== "number" && typeof id !== "string") {
42
+ throw new Error('[@holo-js/auth-social-github] GitHub user profile did not include "id".');
43
+ }
44
+ const primaryVerified = emailsPayload.find((entry) => entry.primary === true && entry.verified === true) ?? emailsPayload.find((entry) => entry.verified === true);
45
+ const fallbackEmail = typeof profilePayload.email === "string" ? profilePayload.email : void 0;
46
+ return {
47
+ id: String(id),
48
+ email: typeof primaryVerified?.email === "string" ? primaryVerified.email : fallbackEmail,
49
+ emailVerified: primaryVerified?.verified === true,
50
+ name: typeof profilePayload.name === "string" ? profilePayload.name : typeof profilePayload.login === "string" ? profilePayload.login : void 0,
51
+ avatar: typeof profilePayload.avatar_url === "string" ? profilePayload.avatar_url : void 0
52
+ };
53
+ }
54
+ var githubSocialProvider = Object.freeze({
55
+ buildAuthorizationUrl(context) {
56
+ const url = new URL("https://github.com/login/oauth/authorize");
57
+ url.searchParams.set("client_id", context.config.clientId ?? "");
58
+ url.searchParams.set("redirect_uri", context.config.redirectUri ?? "");
59
+ url.searchParams.set("state", context.state);
60
+ applyScopes(url, context.config);
61
+ return url.toString();
62
+ },
63
+ async exchangeCode(context) {
64
+ const tokenPayload = await exchangeToken(context);
65
+ const accessToken = String(tokenPayload.access_token ?? "");
66
+ const headers = {
67
+ authorization: `Bearer ${accessToken}`,
68
+ accept: "application/vnd.github+json",
69
+ "user-agent": "holo-js"
70
+ };
71
+ const profileResponse = await fetch("https://api.github.com/user", { headers });
72
+ if (!profileResponse.ok) {
73
+ throw new Error("[@holo-js/auth-social-github] GitHub user request failed.");
74
+ }
75
+ const emailsResponse = await fetch("https://api.github.com/user/emails", { headers });
76
+ if (!emailsResponse.ok) {
77
+ throw new Error("[@holo-js/auth-social-github] GitHub email request failed.");
78
+ }
79
+ const profilePayload = await readJson(profileResponse);
80
+ const emailsPayload = await readJson(emailsResponse);
81
+ return {
82
+ profile: normalizeProfile(profilePayload, emailsPayload),
83
+ tokens: normalizeTokens(tokenPayload)
84
+ };
85
+ }
86
+ });
87
+ var src_default = githubSocialProvider;
88
+ export {
89
+ src_default as default,
90
+ githubSocialProvider
91
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@holo-js/auth-social-github",
3
+ "version": "0.1.3",
4
+ "description": "Holo-JS Framework - GitHub 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
+ }