Skip to content

Commit

Permalink
user find method
Browse files Browse the repository at this point in the history
  • Loading branch information
committed Oct 30, 2019
1 parent 00e630e commit 14321aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ export async function addRole (ctx) {
return;
}

const user = await User.findByEmail(email);
const user = await User.findOne({
attributes: ['id'],
where: { email, enable: 1 }
});
if (!user) {
ctx.flash('error', 'User is not existed');
ctx.redirect(ctx._routes.admin.roles);
Expand Down
11 changes: 9 additions & 2 deletions src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function session (ctx) {
const returnTo = ctx.session.returnTo;

ctx.session.returnTo = null;
delete user.totp_key;
ctx.session.user = user;
await ctx.log(user.id, 'LOGIN');
ctx.redirect(returnTo || ctx._routes.home);
Expand All @@ -114,7 +115,10 @@ export async function passwordReset (ctx) {
return;
}

const user = await User.findByEmail(email);
const user = await User.findOne({
attributes: ['id', 'email'],
where: { email, enable: 1 }
});
if (!user) {
ctx.flash('error', 'User not found');
ctx.redirect(ctx._routes.password_reset);
Expand Down Expand Up @@ -232,7 +236,10 @@ export async function sendToken (ctx) {
const now = Date.now();
ctx.assert(!lastTime || (now - lastTime) > min, 400, 'Try again in a minute');

const user = await User.findByEmail(email);
const user = await User.findOne({
attributes: ['totp_key'],
where: { email, enable: 1 }
});
ctx.assert(user, 400, 'User not found');

await ctx.sendMail(email, 'send_token', {
Expand Down
11 changes: 3 additions & 8 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default function (sequelize, DataTypes) {
});

User.auth = async function (email, password) {
const user = await this.findByEmail(email);
const user = await this.findOne({
where: { email, enable: 1 }
});
if (!user) return null;
if (user.pass_hash !== encrypt(password, user.pass_salt)) {
return null;
Expand All @@ -55,13 +57,6 @@ export default function (sequelize, DataTypes) {
return user;
};

User.findByEmail = function (email) {
const enable = 1;
return this.findOne({
where: { email, enable }
});
};

User.add = function ({ id, password, email, totp_key, is_admin }, options) {
const salt = makeSalt();
const hash = encrypt(password, salt);
Expand Down

0 comments on commit 14321aa

Please sign in to comment.