-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f36d05
commit 18f097b
Showing
14 changed files
with
350 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"js/ts.implicitProjectConfig.checkJs": true | ||
} |
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,13 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddDiscordToConnection1717475269376 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "connection" ADD COLUMN "discordId" VARCHAR;`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "connection" DROP COLUMN "discordId";`); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { REST, RESTGetAPIUserResult, Routes } from "discord.js"; | ||
|
||
export async function getDiscordId( | ||
accessToken: string, | ||
): Promise<string> { | ||
const api = new REST({ version: '10', authPrefix: 'Bearer' }).setToken(accessToken); | ||
|
||
const user: RESTGetAPIUserResult = await (api.get(Routes.user()) as Promise<RESTGetAPIUserResult>); | ||
|
||
return user.id; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { AuthContext } from '../contexts/AuthContext'; | ||
import { Spinner } from 'flowbite-react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { addDiscordToUser } from '../util/api'; | ||
|
||
export function DiscordConnection() { | ||
const { token, profile } = useContext(AuthContext); | ||
const [accessToken, setAccessToken] = useState<string | undefined>( | ||
undefined, | ||
); | ||
const [loading, setLoading] = useState(false); | ||
const [message, setMessage] = useState(''); | ||
|
||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const hash = window.location.hash.substring(1); | ||
const fragments = hash.split('&'); | ||
const accessToken = fragments.find((x) => x.includes('access_token=')); | ||
|
||
if (accessToken) { | ||
setAccessToken(accessToken.replace('access_token=', '')); | ||
} | ||
}, [setAccessToken]); | ||
|
||
useEffect(() => { | ||
if (token && accessToken && !loading && !message) { | ||
setLoading(true); | ||
const run = async () => { | ||
try { | ||
console.log('Sending', accessToken); | ||
const { error } = await addDiscordToUser(token, { | ||
token: accessToken, | ||
}); | ||
|
||
if (error) { | ||
setMessage(JSON.stringify(error)); | ||
return; | ||
} | ||
|
||
setMessage('Discord Connection Complete'); | ||
navigate(`/user/${profile?.username}/edit`); | ||
} catch (e) { | ||
setMessage(`An error occured while connecting, ${e}`); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
run(); | ||
} | ||
}, [accessToken, token, loading, message, navigate, profile]); | ||
|
||
const text = | ||
message ?? | ||
(accessToken | ||
? 'Connecting To Discord...' | ||
: 'Finishing Authentication Flow...'); | ||
|
||
return ( | ||
<div className="h-screen flex items-center justify-center flex-col gap-2"> | ||
<h1 className="text-lg">{text}</h1> | ||
{loading && <Spinner />} | ||
</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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": true, | ||
"removeComments": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"allowSyntheticDefaultImports": true, | ||
"target": "ES2021", | ||
"sourceMap": true, | ||
"incremental": true, | ||
"skipLibCheck": true | ||
} | ||
} |
Oops, something went wrong.