64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
|
import { AuthConfig, BaseAuth } from '@nocobase/auth';
|
||
|
import { Model } from '@nocobase/database';
|
||
|
import VerificationPlugin from '@nocobase/plugin-verification';
|
||
|
import { AuthModel } from '@nocobase/plugin-auth';
|
||
|
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
|
||
|
const userRepo = this.userCollection.repository;
|
||
|
user = await userRepo.findOne({
|
||
|
filter: { phone },
|
||
|
});
|
||
|
if (user) {
|
||
|
await this.authenticator.addUser(user, {
|
||
|
through: {
|
||
|
uuid: phone,
|
||
|
},
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
// New data
|
||
|
const { autoSignup } = this.authenticator.options?.public || {};
|
||
|
console.log(autoSignup);
|
||
|
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;
|
||
|
}
|
||
|
}
|