tachybase_todo/packages/plugins/@tachybase/plugin-sms-auth/src/server/sms-auth.ts
sealday ede7ead8b1 chore(version): release v0.21.34 (#1045)
Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#1045
2024-05-24 01:06:06 +08:00

63 lines
1.8 KiB
TypeScript

import { AuthConfig, BaseAuth } from '@tachybase/auth';
import { Model } from '@tachybase/database';
import { AuthModel } from '@tachybase/plugin-auth';
import VerificationPlugin from '@tachybase/plugin-verification';
import { namespace } from '../constants';
export class SMSAuth extends BaseAuth {
constructor(config: AuthConfig) {
const { ctx } = config;
super({
...config,
userCollection: ctx.db.getCollection('users'),
});
}
async validate() {
const ctx = this.ctx;
const verificationPlugin: VerificationPlugin = ctx.app.getPlugin('verification');
if (!verificationPlugin) {
throw new Error('sms-auth: @tachybase/plugin-verification is required');
}
let user: Model;
await verificationPlugin.intercept(ctx, async () => {
const {
values: { phone },
} = ctx.action.params;
try {
// History data compatible processing
user = await this.userRepository.findOne({
filter: { phone },
});
if (user) {
await this.authenticator.addUser(user, {
through: {
uuid: phone,
},
});
return;
}
// New data
const { autoSignup } = this.authenticator.options?.public || {};
const authenticator = this.authenticator as AuthModel;
if (autoSignup) {
user = await authenticator.findOrCreateUser(phone, {
nickname: phone,
phone,
});
return;
}
user = await authenticator.findUser(phone);
if (!user) {
throw new Error(ctx.t('The phone number is not registered, please register first', { ns: namespace }));
}
} catch (err) {
console.log(err);
throw new Error(err.message);
}
});
return user;
}
}