-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
279 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { useUser } from "shared/hooks"; | ||
import React from "react"; | ||
import { getCensoredEmail } from "shared/utils"; | ||
import { LanguagesComponent } from "./components"; | ||
import { GithubComponent, LanguagesComponent } from "./components"; | ||
//@ts-ignore | ||
import styles from "./account.module.scss"; | ||
|
||
export const AccountComponent = () => { | ||
const { user } = useUser(); | ||
|
||
if (!user) return <div>loading...</div>; | ||
|
||
return ( | ||
<div> | ||
<div className={styles.content}> | ||
<h2>Account</h2> | ||
<p title={user?.email}>{getCensoredEmail(user?.email)}</p> | ||
<p title={user?.accountId}>{user?.username}</p> | ||
<label title={user?.email}>{getCensoredEmail(user?.email)}</label> | ||
<label title={user?.accountId}>{user?.username}</label> | ||
<LanguagesComponent /> | ||
<GithubComponent /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.content { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
} |
117 changes: 117 additions & 0 deletions
117
app/client/src/modules/account/components/github/github.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
import { useNavigate, useSearchParams } from "react-router-dom"; | ||
import { useAccount, useApi, useUser } from "shared/hooks"; | ||
import { RequestMethod } from "shared/enums"; | ||
import { ButtonComponent, GithubIconComponent } from "@oh/components"; | ||
import { RedirectComponent } from "shared/components"; | ||
|
||
//https://github.com/login/oauth/authorize?client_id=Ov23liP5bbBloeAyvY5h&redirect_uri=http://localhost:2024/account/github&state=akjsdaklsdjkl | ||
export const GithubComponent: React.FC = () => { | ||
const [searchParams] = useSearchParams(); | ||
const { getAccountHeaders, isLogged } = useAccount(); | ||
const { fetch } = useApi(); | ||
const navigate = useNavigate(); | ||
const { user } = useUser(); | ||
|
||
const [code, setCode] = useState(searchParams.get("code")); | ||
const [state, setstate] = useState(searchParams.get("state")); | ||
|
||
const [githubLogin, setGithubLogin] = useState(user?.githubLogin); | ||
|
||
const [enabled, setEnabled] = useState<boolean>(null); | ||
const [loaded, setLoaded] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
if (enabled !== null) return; | ||
fetch({ | ||
method: RequestMethod.GET, | ||
pathname: "/account/misc/github?enabled=enabled", | ||
headers: getAccountHeaders(), | ||
}).then(({ enabled }) => { | ||
setEnabled(enabled); | ||
}); | ||
}, [fetch, getAccountHeaders, setEnabled, enabled]); | ||
|
||
useEffect(() => { | ||
if (!user || !user?.githubLogin || !enabled) return; | ||
setGithubLogin(user?.githubLogin); | ||
}, [user, setGithubLogin, enabled]); | ||
|
||
useEffect(() => { | ||
if (!isLogged || !user || !searchParams.get("github") || !enabled) return; | ||
setLoaded(false); | ||
const url = new URL(window.location.href); | ||
url.searchParams.delete("github"); | ||
url.searchParams.delete("code"); | ||
url.searchParams.delete("state"); | ||
setCode(null); | ||
setstate(null); | ||
|
||
window.history.replaceState({}, "", url); | ||
|
||
fetch({ | ||
method: RequestMethod.POST, | ||
pathname: "/account/misc/github", | ||
body: { | ||
code, | ||
state, | ||
}, | ||
headers: getAccountHeaders(), | ||
}).then(({ login }) => { | ||
setGithubLogin(login); | ||
setLoaded(true); | ||
}); | ||
}, [ | ||
isLogged, | ||
user, | ||
getAccountHeaders, | ||
setstate, | ||
setCode, | ||
setLoaded, | ||
enabled, | ||
]); | ||
|
||
const onClickLinkGithub = useCallback(async () => { | ||
const data = await fetch({ | ||
method: RequestMethod.GET, | ||
pathname: "/account/misc/github", | ||
headers: getAccountHeaders(), | ||
}); | ||
window.location.href = data.url; | ||
}, [fetch, getAccountHeaders, navigate]); | ||
|
||
const onClickUnlinkGithub = useCallback(async () => { | ||
await fetch({ | ||
method: RequestMethod.DELETE, | ||
pathname: "/account/misc/github", | ||
headers: getAccountHeaders(), | ||
}).then(() => setGithubLogin(null)); | ||
}, [fetch, getAccountHeaders, navigate, setGithubLogin]); | ||
|
||
if (!enabled) return null; | ||
|
||
if (code || state) | ||
return ( | ||
<RedirectComponent | ||
to={`/account?github=link&state=${state}&code=${code}`} | ||
/> | ||
); | ||
|
||
return ( | ||
<div> | ||
{loaded ? ( | ||
githubLogin ? ( | ||
<ButtonComponent onClick={onClickUnlinkGithub} color="grey"> | ||
<GithubIconComponent /> Unlink '{githubLogin}' Github account | ||
</ButtonComponent> | ||
) : ( | ||
<ButtonComponent onClick={onClickLinkGithub}> | ||
<GithubIconComponent /> Link Github account | ||
</ButtonComponent> | ||
) | ||
) : ( | ||
<label>loading...</label> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./github.component"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ export type User = { | |
admin?: boolean; | ||
otp?: boolean; | ||
verified?: boolean; | ||
githubLogin?: string; | ||
}; |
110 changes: 110 additions & 0 deletions
110
app/server/src/modules/api/v3/account/misc/github.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
RequestMethod, | ||
RequestType, | ||
getResponse, | ||
getRandomString, | ||
HttpStatusCode, | ||
} from "@oh/utils"; | ||
import { System } from "modules/system/main.ts"; | ||
import { hasRequestAccess } from "shared/utils/scope.utils.ts"; | ||
import { RequestKind } from "shared/enums/request.enums.ts"; | ||
|
||
export const githubGetRequest: RequestType = { | ||
method: RequestMethod.GET, | ||
pathname: "/github", | ||
kind: RequestKind.ACCOUNT, | ||
func: async (request: Request, url: URL) => { | ||
if (!(await hasRequestAccess({ request }))) | ||
return getResponse(HttpStatusCode.FORBIDDEN); | ||
|
||
const config = System.getConfig(); | ||
|
||
const enabled = url.searchParams.get("enabled"); | ||
if (enabled) | ||
return getResponse(HttpStatusCode.OK, { | ||
enabled: config.github.enabled, | ||
}); | ||
|
||
if (!config.github.enabled) return getResponse(HttpStatusCode.IM_A_TEAPOT); | ||
|
||
const account = await System.accounts.getFromRequest(request); | ||
const state = getRandomString(32); | ||
|
||
const redirectUri = `${config.url}/account/github`; | ||
|
||
const expireIn = 60 * 60 * 1000; /* 1h */ | ||
System.db.set(["githubState", account.accountId], state, { expireIn }); | ||
|
||
return getResponse(HttpStatusCode.OK, { | ||
url: `https://github.com/login/oauth/authorize?client_id=${config.github.clientId}&redirect_uri=${redirectUri}&state=${state}`, | ||
}); | ||
}, | ||
}; | ||
|
||
export const githubPostRequest: RequestType = { | ||
method: RequestMethod.POST, | ||
pathname: "/github", | ||
kind: RequestKind.ACCOUNT, | ||
func: async (request: Request) => { | ||
if (!(await hasRequestAccess({ request }))) | ||
return getResponse(HttpStatusCode.FORBIDDEN); | ||
|
||
const config = System.getConfig(); | ||
if (!config.github.enabled) return getResponse(HttpStatusCode.IM_A_TEAPOT); | ||
|
||
const { code, state } = await request.json(); | ||
|
||
const account = await System.accounts.getFromRequest(request); | ||
|
||
const foundState = await System.db.get(["githubState", account.accountId]); | ||
if (state !== foundState) return getResponse(HttpStatusCode.FORBIDDEN); | ||
|
||
const url = new URL("https://github.com/login/oauth/access_token"); | ||
url.searchParams.append("client_id", config.github.clientId); | ||
url.searchParams.append("client_secret", config.github.clientSecret); | ||
url.searchParams.append("code", code); | ||
|
||
const tokenResponse = await fetch(url.href, { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
}); | ||
const { access_token } = await tokenResponse.json(); | ||
|
||
const userResponse = await fetch("https://api.github.com/user", { | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
}, | ||
}); | ||
const { login } = await userResponse.json(); | ||
|
||
await System.db.set(["github", account.accountId], { | ||
login, | ||
}); | ||
|
||
return getResponse(HttpStatusCode.OK, { | ||
login, | ||
}); | ||
}, | ||
}; | ||
|
||
export const githubDeleteRequest: RequestType = { | ||
method: RequestMethod.DELETE, | ||
pathname: "/github", | ||
kind: RequestKind.ACCOUNT, | ||
func: async (request: Request) => { | ||
if (!(await hasRequestAccess({ request }))) | ||
return getResponse(HttpStatusCode.FORBIDDEN); | ||
|
||
const config = System.getConfig(); | ||
if (!config.github.enabled) return getResponse(HttpStatusCode.IM_A_TEAPOT); | ||
|
||
const account = await System.accounts.getFromRequest(request); | ||
|
||
System.db.delete(["github", account.accountId]); | ||
|
||
return getResponse(HttpStatusCode.OK); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import { RequestType, getPathRequestList } from "@oh/utils"; | ||
import { bskyPostRequest } from "./bsky.request.ts"; | ||
import { | ||
githubDeleteRequest, | ||
githubGetRequest, | ||
githubPostRequest, | ||
} from "./github.request.ts"; | ||
|
||
export const miscRequestList: RequestType[] = getPathRequestList({ | ||
requestList: [bskyPostRequest], | ||
requestList: [ | ||
bskyPostRequest, | ||
githubGetRequest, | ||
githubPostRequest, | ||
githubDeleteRequest, | ||
], | ||
pathname: "/misc", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.