trashposs/src/server/api/endpoints/auth/session/generate.ts

52 lines
972 B
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
import * as uuid from 'uuid';
2017-03-08 19:50:09 +01:00
import $ from 'cafy';
2018-03-29 13:32:18 +02:00
import App from '../../../../../models/app';
import AuthSess from '../../../../../models/auth-session';
2018-04-02 06:15:53 +02:00
import config from '../../../../../config';
2018-11-02 05:47:44 +01:00
import define from '../../../define';
import { ApiError } from '../../../error';
2018-11-02 04:49:08 +01:00
export const meta = {
requireCredential: false,
params: {
appSecret: {
validator: $.str
}
},
errors: {
noSuchApp: {
message: 'No such app.',
code: 'NO_SUCH_APP',
id: '92f93e63-428e-4f2f-a5a4-39e1407fe998'
}
2018-11-02 04:49:08 +01:00
}
};
2016-12-28 23:49:51 +01:00
export default define(meta, async (ps) => {
2016-12-28 23:49:51 +01:00
// Lookup app
const app = await App.findOne({
2018-11-02 04:49:08 +01:00
secret: ps.appSecret
2016-12-28 23:49:51 +01:00
});
if (app == null) {
throw new ApiError(meta.errors.noSuchApp);
2016-12-28 23:49:51 +01:00
}
// Generate token
const token = uuid.v4();
// Create session token document
2017-01-20 09:38:05 +01:00
const doc = await AuthSess.insert({
2018-03-29 07:48:47 +02:00
createdAt: new Date(),
appId: app._id,
2016-12-28 23:49:51 +01:00
token: token
});
return {
2016-12-28 23:49:51 +01:00
token: doc.token,
url: `${config.auth_url}/${doc.token}`
};
});