* create-nocobase-app template from [develop] * change create-nocobase-app package.json config * feat: load configuration from directory * feat: configuration repository toObject * feat: create application from configuration dir * feat: application factory with plugins options * export type * feat: read application config & application with plugins options * feat: release command * fix: database release * chore: workflow package.json * feat: nocobase cli package * feat: console command * chore: load application in command * fix: load packages from process.cwd * feat: cli load env file * feat: create-nocobase-app * fix: gitignore create-nocobase-app lib * fix: sqlite path * feat: create plugin * chore: plugin files template * chore: move cli into application * chore: create-nocobase-app * fix: create plugin * chore: app-client && app-server * chore: package.json * feat: create-nocobase-app download template from npm * chore: create-nocobase-app template * fix: config of plugin-users * fix: yarn.lock * fix: database build error * fix: yarn.lock * fix: resourcer config * chore: cross-env * chore: app-client dependents * fix: env * chore: v0.6.0-alpha.1 * chore: verdaccio * chore(versions): 😊 publish v0.6.0 * chore(versions): 😊 publish v0.6.1-alpha.0 * chore(versions): 😊 publish v0.6.2-alpha.0 * chore(versions): 😊 publish v0.6.2-alpha.1 * chore: 0.6.2-alpha.2 * feat: workspaces * chore(versions): 😊 publish v0.6.2-alpha.3 * chore(versions): 😊 publish v0.6.2-alpha.4 * chore: create-nocobase-app * chore: create-nocobase-app lib * fix: update tsconfig.jest.json * chore: .env * chore(versions): 😊 publish v0.6.2-alpha.5 * chore(versions): 😊 publish v0.6.2-alpha.6 * feat: improve code * chore(versions): 😊 publish v0.6.2-alpha.7 * fix: cleanup * chore(versions): 😊 publish v0.6.2-alpha.8 * chore: tsconfig for app server package * fix: move files * fix: move files Co-authored-by: chenos <chenlinxh@gmail.com>
164 lines
3.9 KiB
TypeScript
164 lines
3.9 KiB
TypeScript
import { Context, Next } from '@nocobase/actions';
|
|
import { PasswordField } from '@nocobase/database';
|
|
import crypto from 'crypto';
|
|
|
|
export async function check(ctx: Context, next: Next) {
|
|
if (ctx.state.currentUser) {
|
|
const user = ctx.state.currentUser.toJSON();
|
|
ctx.body = user;
|
|
} else {
|
|
ctx.body = {};
|
|
}
|
|
await next();
|
|
}
|
|
|
|
export async function signin(ctx: Context, next: Next) {
|
|
const { uniqueField = 'email', values } = ctx.action.params;
|
|
|
|
if (!values[uniqueField]) {
|
|
ctx.throw(401, '请填写邮箱账号');
|
|
}
|
|
const User = ctx.db.getCollection('users');
|
|
const user = await User.model.findOne<any>({
|
|
where: {
|
|
[uniqueField]: values[uniqueField],
|
|
},
|
|
});
|
|
if (!user) {
|
|
ctx.throw(401, '邮箱账号未注册');
|
|
}
|
|
const pwd = User.getField<PasswordField>('password');
|
|
const isValid = await pwd.verify(values.password, user.password);
|
|
if (!isValid) {
|
|
ctx.throw(401, '密码错误,请您重新输入');
|
|
}
|
|
|
|
const pluginUser = ctx.app.getPlugin('@nocobase/plugin-users');
|
|
|
|
ctx.body = {
|
|
...user.toJSON(),
|
|
token: pluginUser.jwtService.sign({
|
|
userId: user.get('id'),
|
|
}),
|
|
};
|
|
await next();
|
|
}
|
|
|
|
export async function signout(ctx: Context, next: Next) {
|
|
ctx.body = ctx.state.currentUser;
|
|
await next();
|
|
}
|
|
|
|
export async function signup(ctx: Context, next: Next) {
|
|
const User = ctx.db.getRepository('users');
|
|
const { values } = ctx.action.params;
|
|
const user = await User.create({ values });
|
|
ctx.body = user;
|
|
await next();
|
|
}
|
|
|
|
export async function lostpassword(ctx: Context, next: Next) {
|
|
const {
|
|
values: { email },
|
|
} = ctx.action.params;
|
|
if (!email) {
|
|
ctx.throw(401, '请填写邮箱账号');
|
|
}
|
|
const User = ctx.db.getCollection('users');
|
|
const user = await User.model.findOne<any>({
|
|
where: {
|
|
email,
|
|
},
|
|
});
|
|
if (!user) {
|
|
ctx.throw(401, '邮箱账号未注册');
|
|
}
|
|
user.resetToken = crypto.randomBytes(20).toString('hex');
|
|
await user.save();
|
|
ctx.body = user;
|
|
await next();
|
|
}
|
|
|
|
export async function resetpassword(ctx: Context, next: Next) {
|
|
const {
|
|
values: { email, password, resetToken },
|
|
} = ctx.action.params;
|
|
const User = ctx.db.getCollection('users');
|
|
const user = await User.model.findOne<any>({
|
|
where: {
|
|
email,
|
|
resetToken,
|
|
},
|
|
});
|
|
if (!user) {
|
|
ctx.throw(401, 'Unauthorized');
|
|
}
|
|
user.token = null;
|
|
user.resetToken = null;
|
|
user.password = password;
|
|
await user.save();
|
|
ctx.body = user;
|
|
await next();
|
|
}
|
|
|
|
export async function getUserByResetToken(ctx: Context, next: Next) {
|
|
const { token } = ctx.action.params;
|
|
const User = ctx.db.getCollection('users');
|
|
const user = await User.model.findOne({
|
|
where: {
|
|
resetToken: token,
|
|
},
|
|
});
|
|
if (!user) {
|
|
ctx.throw(401, 'Unauthorized');
|
|
}
|
|
ctx.body = user;
|
|
await next();
|
|
}
|
|
|
|
export async function updateProfile(ctx: Context, next: Next) {
|
|
const { values } = ctx.action.params;
|
|
if (!ctx.state.currentUser) {
|
|
ctx.throw(401, 'Unauthorized');
|
|
}
|
|
await ctx.state.currentUser.update(values);
|
|
ctx.body = ctx.state.currentUser;
|
|
await next();
|
|
}
|
|
|
|
export async function changePassword(ctx: Context, next: Next) {
|
|
const {
|
|
values: { oldPassword, newPassword },
|
|
} = ctx.action.params;
|
|
if (!ctx.state.currentUser) {
|
|
ctx.throw(401, 'Unauthorized');
|
|
}
|
|
const User = ctx.db.getCollection('users');
|
|
const user = await User.model.findOne<any>({
|
|
where: {
|
|
email: ctx.state.currentUser.email,
|
|
},
|
|
});
|
|
const pwd = User.getField<PasswordField>('password');
|
|
const isValid = await pwd.verify(oldPassword, user.password);
|
|
if (!isValid) {
|
|
ctx.throw(401, '密码错误,请您重新输入');
|
|
}
|
|
user.password = newPassword;
|
|
user.save();
|
|
ctx.body = ctx.state.currentUser.toJSON();
|
|
await next();
|
|
}
|
|
|
|
export async function setDefaultRole(ctx: Context, next: Next) {
|
|
const {
|
|
values: { roleName },
|
|
} = ctx.action.params;
|
|
|
|
await ctx.state.currentUser.setDefaultRole(roleName);
|
|
|
|
ctx.body = 'ok';
|
|
|
|
await next();
|
|
}
|