mirror of
https://github.com/ether/etherpad-lite.git
synced 2026-01-23 02:35:34 +00:00
* chore: started with implementation * chore: finished index page * chore: started with double sided modal * chore: continue * chore: completed implementation of transfer token * chore: fixed typescript checks
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import {ArgsExpressType} from "../../types/ArgsExpressType";
|
|
const db = require('../../db/DB');
|
|
import crypto from 'crypto'
|
|
|
|
|
|
type TokenTransferRequest = {
|
|
token: string;
|
|
prefsHttp: string,
|
|
createdAt?: number;
|
|
}
|
|
|
|
const tokenTransferKey = "tokenTransfer:";
|
|
|
|
export const expressCreateServer = (hookName:string, {app}:ArgsExpressType) => {
|
|
app.post('/tokenTransfer', async (req, res) => {
|
|
const token = req.body as TokenTransferRequest;
|
|
if (!token || !token.token) {
|
|
return res.status(400).send({error: 'Invalid request'});
|
|
}
|
|
|
|
const id = crypto.randomUUID()
|
|
token.createdAt = Date.now();
|
|
|
|
await db.set(`${tokenTransferKey}:${id}`, token)
|
|
res.send({id});
|
|
})
|
|
|
|
app.get('/tokenTransfer/:token', async (req, res) => {
|
|
const id = req.params.token;
|
|
if (!id) {
|
|
return res.status(400).send({error: 'Invalid request'});
|
|
}
|
|
|
|
const tokenData = await db.get(`${tokenTransferKey}:${id}`);
|
|
if (!tokenData) {
|
|
return res.status(404).send({error: 'Token not found'});
|
|
}
|
|
|
|
const token = await db.get(`${tokenTransferKey}:${id}`)
|
|
|
|
res.cookie('token', tokenData.token, {path: '/', maxAge: 1000*60*60*24*365});
|
|
res.cookie('prefsHttp', tokenData.prefsHttp, {path: '/', maxAge: 1000*60*60*24*365});
|
|
res.send(token);
|
|
})
|
|
}
|