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

72 lines
1.3 KiB
TypeScript
Raw Normal View History

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';
import AccessToken from '../../../../../models/access-token';
import { pack } from '../../../../../models/user';
2018-11-02 05:47:44 +01:00
import define from '../../../define';
2018-11-02 04:49:08 +01:00
export const meta = {
requireCredential: false,
params: {
appSecret: {
validator: $.str
},
token: {
validator: $.str
}
}
};
2016-12-28 23:49:51 +01:00
2018-11-02 05:47:44 +01:00
export default define(meta, (ps) => new Promise(async (res, rej) => {
2017-03-03 20:28:38 +01:00
// Lookup app
const app = await App.findOne({
2018-11-02 04:49:08 +01:00
secret: ps.appSecret
2017-03-03 20:28:38 +01:00
});
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
if (app == null) {
return rej('app not found');
}
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
// Fetch token
const session = await AuthSess
.findOne({
2018-11-02 04:49:08 +01:00
token: ps.token,
2018-03-29 07:48:47 +02:00
appId: app._id
2017-03-03 20:28:38 +01:00
});
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
if (session === null) {
return rej('session not found');
}
2016-12-28 23:49:51 +01:00
2018-03-29 07:48:47 +02:00
if (session.userId == null) {
2017-03-03 20:28:38 +01:00
return rej('this session is not allowed yet');
}
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
// Lookup access token
const accessToken = await AccessToken.findOne({
2018-03-29 07:48:47 +02:00
appId: app._id,
userId: session.userId
2017-03-03 20:28:38 +01:00
});
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
// Delete session
2017-02-08 18:15:34 +01:00
2017-03-03 20:28:38 +01:00
/* https://github.com/Automattic/monk/issues/178
AuthSess.deleteOne({
_id: session._id
});
*/
AuthSess.remove({
_id: session._id
});
2016-12-28 23:49:51 +01:00
2017-03-03 20:28:38 +01:00
// Response
res({
2018-03-29 07:48:47 +02:00
accessToken: accessToken.token,
user: await pack(session.userId, null, {
2017-03-03 20:28:38 +01:00
detail: true
})
2016-12-28 23:49:51 +01:00
});
2018-11-02 05:47:44 +01:00
}));