mirror of
https://github.com/koreanbots/core.git
synced 2025-12-15 14:10:22 +00:00
feat: added reset token button
This commit is contained in:
parent
baf13c2c9e
commit
501c33c137
@ -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 {
|
||||
|
||||
@ -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<BotApplicationProps> = ({ 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<BotApplicationProps> = ({ user, spec, bot, theme,
|
||||
<Button onClick={() => setShowToken(!showToken)}>{showToken ? '숨기기' : '보기'}</Button>
|
||||
<Button onClick={setTokenCopied} className={tokenCopied ? 'bg-green-400 text-white' : null}>{tokenCopied ? '복사됨' : '복사'}</Button>
|
||||
<Button onClick={()=> setModalOpen(true)}>재발급</Button>
|
||||
<Modal isOpen={modalOpened} onClose={() => setModalOpen(false)} dark={theme === 'dark'} header='정말로 토큰을 재발급하시겠습니까?'>
|
||||
<p>기존에 사용중이시던 토큰은 더 이상 사용하실 수 없습니다</p>
|
||||
<div className='text-right pt-6'>
|
||||
<Button className='bg-gray-500 hover:opacity-90' onClick={()=> setModalOpen(false)}>취소</Button>
|
||||
<Button>재발급</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
</div>
|
||||
<Formik validationSchema={DeveloperBotSchema} initialValues={{
|
||||
webhook: spec.webhook || '',
|
||||
@ -98,6 +101,17 @@ const BotApplication: NextPage<BotApplicationProps> = ({ user, spec, bot, theme,
|
||||
{touched.webhook && errors.webhook ? <div className='text-red-500 text-xs font-light mt-1'>{errors.webhook}</div> : null}
|
||||
</div>
|
||||
<Button type='submit'><i className='far fa-save'/> 저장</Button>
|
||||
<Modal isOpen={modalOpened} onClose={() => setModalOpen(false)} dark={theme === 'dark'} header='정말로 토큰을 재발급하시겠습니까?'>
|
||||
<p>기존에 사용중이시던 토큰은 더 이상 사용하실 수 없습니다</p>
|
||||
<div className='text-right pt-6'>
|
||||
<Button className='bg-gray-500 hover:opacity-90' onClick={()=> setModalOpen(false)}>취소</Button>
|
||||
<Button onClick={async ()=> {
|
||||
const res = await resetToken()
|
||||
spec.token = res.data.token
|
||||
setModalOpen(false)
|
||||
}}>재발급</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
|
||||
@ -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 = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user