-
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.
Merge pull request #8 from Nexite/master
Add "Discord Information" section
- Loading branch information
Showing
15 changed files
with
268 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
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,5 @@ | ||
mutation UnlinkDiscordMutation { | ||
account { | ||
unlinkDiscord(userId: "") | ||
} | ||
} |
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,90 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import FormControl, { Label, HelpText } from '@codeday/topo/Molecule/FormControl' | ||
import Image from '@codeday/topo/Atom/Image'; | ||
import Box from '@codeday/topo/Atom/Box'; | ||
import Text from '@codeday/topo/Atom/Text'; | ||
import Button from '@codeday/topo/Atom/Button'; | ||
import { UnlinkDiscordMutation } from './Discord.gql' | ||
import { tryAuthenticatedApiQuery } from '../../util/api'; | ||
import { useRouter } from 'next/router' | ||
import { useToasts } from '@codeday/topo/utils'; | ||
import Link from '@codeday/topo/Atom/Text/Link'; | ||
import { Popover, PopoverTrigger, PopoverArrow, PopoverContent, PopoverHeader, PopoverCloseButton, PopoverBody } from '@chakra-ui/core'; | ||
|
||
const unlinkDiscord = async (token) => { | ||
const { error } = await tryAuthenticatedApiQuery(UnlinkDiscordMutation, {}, token) | ||
return !error ? true : false | ||
}; | ||
|
||
const Discord = ({ user, token }) => { | ||
const [isLinked, setIsLinked] = useState(user.discordId ? true : false) | ||
const [isPopoverOpen, setIsPopoverOpen] = useState(false) | ||
const router = useRouter() | ||
const { success } = useToasts(); | ||
const picture = user?.discordInformation?.avatar.endsWith("null") ? `https://cdn.discordapp.com/embed/avatars/${user.discordInformation.discriminator % 5}.png` : user?.discordInformation?.avatar || null | ||
|
||
return ( | ||
<FormControl> | ||
<Label fontWeight="bold">Discord Information</Label> | ||
{isLinked ? | ||
<Box> | ||
<Box style={{ clear: 'both', display: "flex", alignItems: "center" }}> | ||
<Image mb={2} src={picture} alt="" float="left" mr={2} height="2em" rounded="full" /> <Text fontSize="1em">{user.discordInformation.handle}</Text> | ||
</Box> | ||
<Popover isOpen={isPopoverOpen} onOpen={() => setIsPopoverOpen(true)} onClose={() => setIsPopoverOpen(false)}> | ||
<PopoverTrigger> | ||
<Button | ||
size="xs" | ||
marginRight="3" | ||
> | ||
Unlink Discord account | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent> | ||
<PopoverArrow /> | ||
<PopoverCloseButton /> | ||
<PopoverHeader>Confirmation!</PopoverHeader> | ||
<PopoverBody> | ||
<p>Are you sure you want to do that?</p> | ||
<Button size="xs" | ||
style={{ width: "50%" }} | ||
onClick={async () => { | ||
await unlinkDiscord(token); | ||
setIsLinked(false); | ||
success("Unlinked Discord Account!") | ||
}}> | ||
Yes | ||
</Button> | ||
<Button size="xs" | ||
variantColor="red" | ||
style={{ width: "50%" }} | ||
onClick={() => setIsPopoverOpen(false)}> | ||
No | ||
</Button> | ||
</PopoverBody> | ||
</PopoverContent> | ||
</Popover> | ||
<HelpText> | ||
Your account is linked and ready! Make sure you are in the <Link href="https://discord.gg/codeday">CodeDay Discord Server</Link>! | ||
</HelpText> | ||
</Box> | ||
: <Box> | ||
<Button onClick={() => | ||
router.push("/api/discord/link") | ||
}> | ||
Link Discord | ||
</Button> | ||
<HelpText> | ||
Link your Discord account to get full access to the <Link href="https://discord.gg/codeday">CodeDay Discord Server</Link>. | ||
</HelpText> | ||
</Box>} | ||
</FormControl> | ||
); | ||
}; | ||
Discord.propTypes = { | ||
user: PropTypes.object.isRequired, | ||
token: PropTypes.string.isRequired, | ||
}; | ||
Discord.provides = "discord"; | ||
export default Discord; |
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 getConfig from 'next/config'; | ||
import OAuth from 'discord-oauth2' | ||
|
||
const { serverRuntimeConfig } = getConfig(); | ||
|
||
export const discordApi = new OAuth({ | ||
version: 'v8', | ||
clientId: serverRuntimeConfig.discord.clientId, | ||
clientSecret: serverRuntimeConfig.discord.clientSecret, | ||
redirectUri: serverRuntimeConfig.discord.redirectUri, | ||
}); |
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,28 @@ | ||
/* eslint-disable no-console */ | ||
import { getSession } from 'next-auth/client'; | ||
import jwt from 'jsonwebtoken'; | ||
import { LinkDiscordMutation, CheckCodeDayLinked } from './discord.gql' | ||
import { tryAuthenticatedServerApiQuery } from '../../../util/api'; | ||
import { discordApi } from '../../../lib/discord' | ||
import getConfig from 'next/config'; | ||
|
||
const { serverRuntimeConfig } = getConfig(); | ||
|
||
export default async (req, res) => { | ||
const code = req.query.code; | ||
const { access_token } = await discordApi.tokenRequest({ | ||
code: code, | ||
scope: "identify guilds", | ||
grantType: "authorization_code", | ||
}) | ||
const {id: discordId} = await discordApi.getUser(access_token) | ||
const { result, error } = await tryAuthenticatedServerApiQuery(CheckCodeDayLinked, { discordId }) | ||
if (result.account.getUser) { | ||
res.redirect('/discord/error?code=discordalreadylinked'); | ||
return | ||
} | ||
const userId = await (await getSession({ req })).user.id | ||
const token = jwt.sign({scopes: "write:users"}, serverRuntimeConfig.gqlAccountSecret, {expiresIn: "1m"}) | ||
await tryAuthenticatedServerApiQuery(LinkDiscordMutation, { discordId, userId }, token) | ||
res.redirect(`/discord/success`); | ||
}; |
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,21 @@ | ||
mutation LinkDiscordMutation ($discordId: String!, $userId: ID!) { | ||
account { | ||
linkDiscord(userId: $userId, discordId: $discordId) | ||
} | ||
} | ||
|
||
query CheckCodeDayLinked ($discordId: String!) { | ||
account { | ||
getUser (where: {discordId: $discordId}, fresh: true) { | ||
discordId | ||
} | ||
} | ||
} | ||
|
||
query CheckDiscordLinked ($userId: ID!) { | ||
account { | ||
getUser (where: {id: $userId}, fresh: true) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-disable no-console */ | ||
import { discordApi } from '../../../lib/discord' | ||
import crypto from 'crypto' | ||
import { getSession } from 'next-auth/client'; | ||
import { CheckDiscordLinked } from './discord.gql' | ||
import { tryAuthenticatedServerApiQuery } from '../../../util/api'; | ||
|
||
export default async (req, res) => { | ||
const session = await getSession({ req }) | ||
const { result } = await tryAuthenticatedServerApiQuery(CheckDiscordLinked, {userId: session.user.id}) | ||
if (result?.account?.getUser?.discordId) { | ||
res.redirect('/discord/error?code=codedayalreadylinked'); | ||
return | ||
} | ||
if (!session || !session.user) { res.redirect('/'); return; } | ||
const discordLink = discordApi.generateAuthUrl({scope: ["identify", "guilds"],state: crypto.randomBytes(16).toString("hex")}) | ||
res.redirect(discordLink); | ||
}; |
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,40 @@ | ||
import React from 'react'; | ||
import Page from '../../components/Page'; | ||
import Button from '@codeday/topo/Atom/Button'; | ||
|
||
const DiscordError = ({ message }) => { | ||
return ( | ||
<Page isLoggedIn={false}> | ||
<p>{message}</p> | ||
<Button | ||
size="xs" | ||
href="/" | ||
as="a"> | ||
Click here to return to the main page. | ||
</Button> | ||
</Page> | ||
) | ||
}; | ||
|
||
export default DiscordError; | ||
|
||
export const getServerSideProps = async ({ req, res, query }) => { | ||
if (query.code == "discordalreadylinked") { | ||
return { | ||
props: { | ||
message: "ERROR: That Discord account is already linked to a CodeDay account!" | ||
} | ||
} | ||
} else if (query.code == "codedayalreadylinked") { | ||
return { | ||
props: { | ||
message: "ERROR: Your CodeDay account is already linked to a Discord account!" | ||
} | ||
} | ||
} else { | ||
res.setHeader("location", "/"); | ||
res.statusCode = 302; | ||
res.end(); | ||
return { props: {} } | ||
} | ||
}; |
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,15 @@ | ||
import React from 'react'; | ||
import Page from '../../components/Page'; | ||
import Button from '@codeday/topo/Atom/Button'; | ||
|
||
export default () => ( | ||
<Page isLoggedIn={false}> | ||
<p>Discord account successfuly linked!</p> | ||
<Button | ||
size="xs" | ||
href="/" | ||
as="a"> | ||
Click here to return to the main page. | ||
</Button> | ||
</Page> | ||
); |
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 |
---|---|---|
|
@@ -31,6 +31,12 @@ query IndexUserQuery { | |
title | ||
} | ||
acceptTos | ||
discordId | ||
discordInformation { | ||
discriminator | ||
handle | ||
avatar | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2295,6 +2295,11 @@ diffie-hellman@^5.0.0: | |
miller-rabin "^4.0.0" | ||
randombytes "^2.0.0" | ||
|
||
discord-oauth2@^2.6.0: | ||
version "2.6.0" | ||
resolved "https://registry.yarnpkg.com/discord-oauth2/-/discord-oauth2-2.6.0.tgz#d362b386e77a3eb3135772307a253514e0dc48be" | ||
integrity sha512-9lEt1Rp9rruFPiVQOPbe2rKJXB40fO2u9Ozjz3n/TlVYnIxCTeohVkNI/c5AoLFDZcSxFc2MtDTf9iG0X7BkRg== | ||
|
||
[email protected]: | ||
version "1.5.0" | ||
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa" | ||
|
@@ -4702,11 +4707,6 @@ [email protected]: | |
dependencies: | ||
he "1.2.0" | ||
|
||
node-fetch@^2.6.1: | ||
version "2.6.1" | ||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" | ||
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== | ||
|
||
node-libs-browser@^2.2.1: | ||
version "2.2.1" | ||
resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.1.tgz#b64f513d18338625f90346d27b0d235e631f6425" | ||
|