feat: update org operate
This commit is contained in:
40
src/route.ts
40
src/route.ts
@@ -46,6 +46,46 @@ app
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
app
|
||||
.route({
|
||||
path: 'auth-check',
|
||||
key: 'admin',
|
||||
id: 'check-auth-admin',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
if (!tokenUser) {
|
||||
ctx.throw(401, 'No User For authorized');
|
||||
}
|
||||
try {
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
id: tokenUser.id,
|
||||
},
|
||||
});
|
||||
if (!user) {
|
||||
ctx.throw(404, 'user not found');
|
||||
}
|
||||
user.setTokenUser(tokenUser);
|
||||
const orgs = await user.getOrgs();
|
||||
if (orgs.includes('admin')) {
|
||||
ctx.body = 'admin';
|
||||
ctx.state.tokenAdmin = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
orgs,
|
||||
};
|
||||
return;
|
||||
}
|
||||
ctx.body = 'not admin';
|
||||
} catch (e) {
|
||||
console.error(`auth-admin error`, e);
|
||||
console.error('tokenUser', tokenUser?.id, tokenUser?.username, tokenUser?.uid);
|
||||
ctx.throw(500, e.message);
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
|
||||
Reference in New Issue
Block a user