77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import { FastifyInstance } from 'fastify';
|
|
import { FromSchema } from 'json-schema-to-ts';
|
|
import { nanoid } from 'nanoid/async';
|
|
|
|
const schema = {
|
|
querystring: {
|
|
type: 'object',
|
|
required: ['session', 'key'],
|
|
properties: {
|
|
session: { type: 'string' },
|
|
key: { type: 'string' }
|
|
}
|
|
}
|
|
} as const;
|
|
|
|
export default async (server: FastifyInstance) =>
|
|
server.get<{ Querystring: FromSchema<typeof schema.querystring> }>(
|
|
'/create',
|
|
async (req, reply) => {
|
|
if (req.query.key !== process.env.URL_TOKEN) return reply.code(403).send('Forbidden');
|
|
const session = await server.db.session.findFirst({
|
|
where: {
|
|
id: req.query.session
|
|
}
|
|
});
|
|
if (!session) return reply.code(404).send('Session is not found');
|
|
const linkedSession = await server.db.link.findFirst({
|
|
where: { session: { id: req.query.session } },
|
|
select: {
|
|
id: true
|
|
}
|
|
});
|
|
if (linkedSession)
|
|
reply.send(
|
|
`${
|
|
process.env.NODE_ENV === 'production'
|
|
? 'https://mc.m0e.space/a'
|
|
: 'http://localhost:8080/a'
|
|
}/${linkedSession.id}`
|
|
);
|
|
let linkId = await nanoid(5);
|
|
// eslint-disable-next-line no-constant-condition
|
|
while (true) {
|
|
const link = await server.db.link.findFirst({
|
|
where: { id: linkId },
|
|
select: { id: true }
|
|
});
|
|
if (!link) break;
|
|
linkId = await nanoid(5);
|
|
}
|
|
await server.db.link.create({
|
|
data: {
|
|
id: linkId,
|
|
link: encodeURI(
|
|
`https://auth.m0e.space/application/o/authorize/?client_id=7f24f967b2faf3d8c1c53771661abff8e07060c0&response_type=code&redirect_uri=${
|
|
process.env.NODE_ENV === 'production'
|
|
? 'https://mc.m0e.space'
|
|
: 'http://localhost:8080'
|
|
}/api/auth/verify?session=${req.query.session}&scope=profile openid`
|
|
),
|
|
session: {
|
|
connect: {
|
|
id: req.query.session
|
|
}
|
|
}
|
|
}
|
|
});
|
|
reply.send(
|
|
`${
|
|
process.env.NODE_ENV === 'production'
|
|
? 'https://mc.m0e.space/a'
|
|
: 'http://localhost:8080/a'
|
|
}/${linkId}`
|
|
);
|
|
}
|
|
);
|