mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-05-11 18:10:01 +08:00
feat: add JWT
This commit is contained in:
@@ -17,8 +17,8 @@ async function email(message, env, ctx) {
|
||||
const parsedEmail = await parser.parse(rawEmail);
|
||||
|
||||
const { success } = await env.DB.prepare(
|
||||
`INSERT INTO mails (address, message) VALUES (?, ?)`
|
||||
).bind(message.to, parsedEmail.html).run();
|
||||
`INSERT INTO mails (source, address, message) VALUES (?, ?, ?)`
|
||||
).bind(message.from, message.to, parsedEmail.html).run();
|
||||
if (!success) {
|
||||
message.setReject(`Failed save message to ${message.to}`);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Hono } from 'hono'
|
||||
import { Jwt } from 'hono/utils/jwt'
|
||||
|
||||
const api = new Hono()
|
||||
|
||||
@@ -8,11 +9,15 @@ api.get('/api/mails', async (c) => {
|
||||
return c.json({ "error": "No address" }, 400)
|
||||
}
|
||||
const { results } = await c.env.DB.prepare(
|
||||
`SELECT id, message FROM mails where address = ? order by id desc limit 10`
|
||||
`SELECT id, source, message FROM mails where address = ? order by id desc limit 10`
|
||||
).bind(address).all();
|
||||
return c.json(results);
|
||||
})
|
||||
|
||||
api.get('/api/settings', async (c) => {
|
||||
return c.json(c.get("jwtPayload"));
|
||||
})
|
||||
|
||||
api.get('/api/new_address', async (c) => {
|
||||
// insert new address
|
||||
const name = Math.random().toString(36).substring(2, 15)
|
||||
@@ -22,8 +27,12 @@ api.get('/api/new_address', async (c) => {
|
||||
if (!success) {
|
||||
return c.json({ "error": "Failed to create address" }, 500)
|
||||
}
|
||||
return c.json({
|
||||
// create jwt
|
||||
const jwt = await Jwt.sign({
|
||||
address: c.env.PREFIX + name + "@" + c.env.DOMAIN
|
||||
}, c.env.JWT_SECRET)
|
||||
return c.json({
|
||||
jwt: jwt
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,11 +1,21 @@
|
||||
import { Hono } from 'hono'
|
||||
import { cors } from 'hono/cors';
|
||||
import { jwt } from 'hono/jwt'
|
||||
|
||||
import { api } from './router';
|
||||
import { email } from './email';
|
||||
|
||||
const app = new Hono()
|
||||
app.use('/*', cors());
|
||||
app.use('/api/*', async (c, next) => {
|
||||
if (c.req.path.startsWith("/api/new_address")) {
|
||||
await next();
|
||||
return;
|
||||
};
|
||||
return jwt({ secret: c.env.JWT_SECRET })(c, next);
|
||||
});
|
||||
|
||||
|
||||
app.route('/', api)
|
||||
|
||||
app.all('/*', async c => c.html(`<h1>Hello World</h1>`))
|
||||
|
||||
Reference in New Issue
Block a user