diff --git a/pages/api/v2/applications/bots/[id]/reset.ts b/pages/api/v2/applications/bots/[id]/reset.ts index c76ae76..a52f3ae 100644 --- a/pages/api/v2/applications/bots/[id]/reset.ts +++ b/pages/api/v2/applications/bots/[id]/reset.ts @@ -23,8 +23,9 @@ const ResetApplication = RequestHandler const bot = await get.bot.load(req.query.id) if(!bot) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 봇입니다.' }) if(!(bot.owners as User[]).find(el => el.id === user)) return ResponseWrapper(res, { code: 403 }) - await update.resetBotToken(req.query.id, validated.token) - return ResponseWrapper(res, { code: 200 }) + const d = await update.resetBotToken(req.query.id, validated.token) + if(!d) return ResponseWrapper(res, { code: 500, message: '무언가 잘못되었습니다.' }) + return ResponseWrapper(res, { code: 200, data: { token: d }}) }) interface ApiRequest extends NextApiRequest { diff --git a/pages/developers/applications/bots/[id].tsx b/pages/developers/applications/bots/[id].tsx index c4a3b0b..6d1c0dd 100644 --- a/pages/developers/applications/bots/[id].tsx +++ b/pages/developers/applications/bots/[id].tsx @@ -17,7 +17,6 @@ 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')) @@ -40,6 +39,16 @@ const BotApplication: NextPage = ({ user, spec, bot, theme, setData(res) } + async function resetToken() { + const res = await Fetch<{ token: string }>(`/applications/bots/${bot.id}/reset`, { + method: 'POST', + body: JSON.stringify({ token: spec.token, _csrf: csrfToken }) + }) + setData(res) + + return res + } + if(!user) { localStorage.redirectTo = window.location.href redirectTo(router, 'login') @@ -76,13 +85,7 @@ const BotApplication: NextPage = ({ user, spec, bot, theme, - setModalOpen(false)} dark={theme === 'dark'} header='정말로 토큰을 재발급하시겠습니까?'> -

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

-
- - -
-
+ = ({ user, spec, bot, theme, {touched.webhook && errors.webhook ?
{errors.webhook}
: null} + setModalOpen(false)} dark={theme === 'dark'} header='정말로 토큰을 재발급하시겠습니까?'> +

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

+
+ + +
+
)}
diff --git a/utils/Query.ts b/utils/Query.ts index 6e18424..53fcff6 100644 --- a/utils/Query.ts +++ b/utils/Query.ts @@ -226,8 +226,16 @@ async function getBotSpec(id: string, userID: string) { } async function updateBotApplication(id: string, value: { webhook: string }) { - const res = await knex('bots').update({ webhook: value.webhook }).where({ id }) - return + const bot = await knex('bots').update({ webhook: value.webhook }).where({ id }) + if(bot !== 1) return false + return true +} + +async function resetBotToken(id: string, beforeToken: string) { + const token = sign({ id }) + const bot = await knex('bots').update({ token }).where({ id, token: beforeToken }) + if(bot !== 1) return null + return token } async function getImage(url: string) { @@ -343,7 +351,8 @@ export const get = { export const update = { assignToken, - updateBotApplication + updateBotApplication, + resetBotToken } export const put = {