Files
test1/src/server/api/endpoints/auth/session/generate.ts
syuilo b9cb6d1c10 refactor: refactoring imports
将来ESMに移行しやすいように
Related: #7658

なんかmochaが起動しなくなってるけど理由不明
すぐ直したい
2021-08-19 18:33:41 +09:00

71 lines
1.3 KiB
TypeScript

import { v4 as uuid } from 'uuid';
import $ from 'cafy';
import config from '@/config/index.js';
import define from '../../../define.js';
import { ApiError } from '../../../error.js';
import { Apps, AuthSessions } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
export const meta = {
tags: ['auth'],
requireCredential: false as const,
params: {
appSecret: {
validator: $.str,
}
},
res: {
type: 'object' as const,
optional: false as const, nullable: false as const,
properties: {
token: {
type: 'string' as const,
optional: false as const, nullable: false as const,
},
url: {
type: 'string' as const,
optional: false as const, nullable: false as const,
format: 'url',
},
}
},
errors: {
noSuchApp: {
message: 'No such app.',
code: 'NO_SUCH_APP',
id: '92f93e63-428e-4f2f-a5a4-39e1407fe998'
}
}
};
export default define(meta, async (ps) => {
// Lookup app
const app = await Apps.findOne({
secret: ps.appSecret
});
if (app == null) {
throw new ApiError(meta.errors.noSuchApp);
}
// Generate token
const token = uuid();
// Create session token document
const doc = await AuthSessions.save({
id: genId(),
createdAt: new Date(),
appId: app.id,
token: token
});
return {
token: doc.token,
url: `${config.authUrl}/${doc.token}`
};
});