tachybase_todo/packages/plugins/@nocobase/plugin-cas/src/server/actions/service.ts
sealday 3e58c54aa8 feat: 仓库二期 (#719)
Co-authored-by: hello@lv <2256334253@qq.com>
Co-authored-by: wjh <wwwjh0710@163.com>
Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#719
2024-05-08 16:20:31 +08:00

30 lines
976 B
TypeScript

import { Context, Next } from '@tachybase/actions';
import { AppSupervisor } from '@tachybase/server';
import { CASAuth } from '../auth';
export const service = async (ctx: Context, next: Next) => {
const { authenticator, __appName: appName, redirect } = ctx.action.params;
let prefix = process.env.APP_PUBLIC_PATH || '';
if (appName && appName !== 'main') {
const appSupervisor = AppSupervisor.getInstance();
if (appSupervisor?.runningMode !== 'single') {
prefix += `apps/${appName}`;
}
}
const auth = (await ctx.app.authManager.get(authenticator, ctx)) as CASAuth;
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}
try {
const { token } = await auth.signIn();
ctx.redirect(`${prefix}${redirect || '/admin'}?authenticator=${authenticator}&token=${token}`);
} catch (error) {
ctx.redirect(`${prefix}/signin?authenticator=${authenticator}&error=${error.message}&redirect=${redirect}`);
}
return next();
};