2023-06-07 23:46:42 +08:00
|
|
|
import { AuthConfig, BaseAuth } from '@nocobase/auth';
|
|
|
|
import { Model } from '@nocobase/database';
|
|
|
|
import { AuthModel } from '@nocobase/plugin-auth';
|
2023-06-30 11:20:35 +08:00
|
|
|
import VerificationPlugin from '@nocobase/plugin-verification';
|
2023-06-07 23:46:42 +08:00
|
|
|
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: @nocobase/plugin-verification is required');
|
|
|
|
}
|
|
|
|
let user: Model;
|
|
|
|
await verificationPlugin.intercept(ctx, async () => {
|
|
|
|
const {
|
|
|
|
values: { phone },
|
|
|
|
} = ctx.action.params;
|
|
|
|
try {
|
|
|
|
// History data compatible processing
|
2023-06-30 11:20:35 +08:00
|
|
|
user = await this.userRepository.findOne({
|
2023-06-07 23:46:42 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|