-
Notifications
You must be signed in to change notification settings - Fork 15
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 #97 from ZingerLittleBee/feat/token-status
feat: 🎸 token add and delete
- Loading branch information
Showing
10 changed files
with
172 additions
and
71 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
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,29 @@ | ||
import { useState } from 'react' | ||
import { CheckIcon, Copy } from 'lucide-react' | ||
|
||
import { Button } from '@/components/ui/button' | ||
|
||
export default function CopyButton({ content }: { content: string }) { | ||
const [copySuccess, setCopySuccess] = useState(false) | ||
|
||
return ( | ||
<Button | ||
size="sm" | ||
variant="outline" | ||
onClick={async () => { | ||
await navigator.clipboard.writeText(content) | ||
setCopySuccess(true) | ||
setTimeout(() => { | ||
setCopySuccess(false) | ||
}, 2000) | ||
}} | ||
> | ||
<span className="sr-only">Copy</span> | ||
{copySuccess ? ( | ||
<CheckIcon className="size-4" /> | ||
) : ( | ||
<Copy className="size-4" /> | ||
)} | ||
</Button> | ||
) | ||
} |
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,82 @@ | ||
import { env } from '@/env' | ||
import { NotLoggedInError } from '@/server/api/error' | ||
import { createTRPCRouter, protectedProcedure } from '@/server/api/trpc' | ||
import { sign } from 'jsonwebtoken' | ||
import { z } from 'zod' | ||
|
||
import { getLogger } from '@/lib/logging' | ||
|
||
const logger = getLogger('server-token.ts') | ||
|
||
export const serverTokenRouter = createTRPCRouter({ | ||
list: protectedProcedure | ||
.input( | ||
z.object({ | ||
id: z.string(), | ||
}) | ||
) | ||
.query(async ({ input, ctx }) => { | ||
if (!ctx.session.user) throw NotLoggedInError | ||
const queryResult = await ctx.db.serverToken.findMany({ | ||
where: { | ||
serverId: input.id, | ||
}, | ||
}) | ||
return queryResult.map((item) => ({ | ||
...item, | ||
})) | ||
}), | ||
create: protectedProcedure | ||
.input( | ||
z.object({ | ||
serverId: z.string(), | ||
}) | ||
) | ||
.mutation(async ({ ctx, input }) => { | ||
const payload = { | ||
serverId: input.serverId, | ||
} | ||
|
||
const token = sign(payload, env.SERVER_JWT_SECRET) | ||
|
||
const serverToken = await ctx.db.serverToken.create({ | ||
data: { | ||
token, | ||
server: { connect: { id: input.serverId } }, | ||
}, | ||
}) | ||
return { | ||
id: serverToken.id, | ||
token: serverToken.token, | ||
isExpires: false, | ||
serverId: serverToken.serverId, | ||
} | ||
}), | ||
delete: protectedProcedure | ||
.input( | ||
z.object({ | ||
token: z.string(), | ||
}) | ||
) | ||
.mutation(async ({ input, ctx }) => { | ||
const token = await ctx.db.serverToken.findFirst({ | ||
where: { | ||
token: input.token, | ||
}, | ||
}) | ||
if (token) { | ||
await ctx.db.serverToken.delete({ | ||
where: { | ||
id: token.id, | ||
}, | ||
}) | ||
|
||
await ctx.mongo | ||
.db('serverbee') | ||
.collection('invalid') | ||
.insertOne({ token: input.token }) | ||
|
||
logger.info(`Token ${input.token} deleted`) | ||
} | ||
}), | ||
}) |
Oops, something went wrong.