From 9963ca7b3a20433a9d2cab36a2fe9274366b2c08 Mon Sep 17 00:00:00 2001 From: Junseo Park Date: Sat, 27 Feb 2021 19:47:29 +0900 Subject: [PATCH] feat: added csrfToken and token reset modal --- pages/developers/applications/bots/[id].tsx | 61 +++++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/pages/developers/applications/bots/[id].tsx b/pages/developers/applications/bots/[id].tsx index f0a73e2..c4a3b0b 100644 --- a/pages/developers/applications/bots/[id].tsx +++ b/pages/developers/applications/bots/[id].tsx @@ -6,25 +6,40 @@ import { Form, Formik } from 'formik' import useCopyClipboard from 'react-use-clipboard' import { get } from '@utils/Query' -import { DeveloperBotSchema } from '@utils/Yup' -import { parseCookie, redirectTo } from '@utils/Tools' +import { DeveloperBot, DeveloperBotSchema } from '@utils/Yup' +import { cleanObject, parseCookie, redirectTo } from '@utils/Tools' +import { getToken } from '@utils/Csrf' +import Fetch from '@utils/Fetch' import { ParsedUrlQuery } from 'querystring' -import { Bot, BotSpec } from '@types' +import { Bot, BotSpec, ResponseProps, Theme } from '@types' import NotFound from 'pages/404' const Button = dynamic(() => import('@components/Button')) +const Divider = dynamic(() => import('@components/Divider')) const Input = dynamic(() => import('@components/Form/Input')) const DeveloperLayout = dynamic(() => import('@components/DeveloperLayout')) const DiscordAvatar = dynamic(() => import('@components/DiscordAvatar')) +const Message = dynamic(() => import('@components/Message')) +const Modal = dynamic(() => import('@components/Modal')) -const BotApplication: NextPage = ({ user, spec, bot }) => { +const BotApplication: NextPage = ({ user, spec, bot, theme, csrfToken }) => { const router = useRouter() + const [ data, setData ] = useState>(null) + const [ modalOpened, setModalOpen ] = useState(false) const [ showToken, setShowToken ] = useState(false) const [ tokenCopied, setTokenCopied ] = useCopyClipboard(spec?.token, { successDuration: 1000 }) + async function updateApplication(d: DeveloperBot) { + const res = await Fetch(`/applications/bots/${bot.id}`, { + method: 'PATCH', + body: JSON.stringify(cleanObject(d)) + }) + setData(res) + } + if(!user) { localStorage.redirectTo = window.location.href redirectTo(router, 'login') @@ -33,25 +48,47 @@ const BotApplication: NextPage = ({ user, spec, bot }) => { if(!bot || !spec) return return

봇 설정

-

한국 디스코드봇 리스트 API를 사용해보세요.

+

한국 디스코드봇 리스트 API에 사용할 정보를 이곳에서 설정하실 수 있습니다.

-

{bot.name}

+ { + !data ? '' : data.code === 200 ? + +

수정 성공!

+

봇 정보를 저장했습니다.

+
: +

{data.message}

+
    + { + data.errors?.map((el, i)=>
  • {el}
  • ) + } +
+
+ } +

{bot.name}#{bot.tag}

봇 토큰

-
{showToken ? spec.token : '*********'}
+
{showToken ? spec.token : '******************'}
- + + setModalOpen(false)} dark={theme === 'dark'} header='정말로 토큰을 재발급하시겠습니까?'> +

기존에 사용중이시던 토큰은 더 이상 사용하실 수 없습니다

+
+ + +
+
console.log(d)}> + onSubmit={(data) => updateApplication(data)}> {({ errors, touched }) => (
@@ -75,6 +112,8 @@ interface BotApplicationProps { user: string spec: BotSpec bot: Bot + csrfToken: string + theme: Theme } export const getServerSideProps = async (ctx: Context) => { @@ -82,7 +121,7 @@ export const getServerSideProps = async (ctx: Context) => { const user = await get.Authorization(parsed?.token) || '' return { - props: { user, spec: await get.botSpec(ctx.query.id, user), bot: await get.bot.load(ctx.query.id) } + props: { user, spec: await get.botSpec(ctx.query.id, user), bot: await get.bot.load(ctx.query.id), csrfToken: getToken(ctx.req, ctx.res) } } }