Skip to content
Snippets Groups Projects

Password haching

Merged GUVEN Kemal requested to merge password-haching into master
4 files
+ 495
28
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 28
18
@@ -11,30 +11,36 @@ import { UpdatePeopleDto } from '../dto/update-people.dto';
@@ -11,30 +11,36 @@ import { UpdatePeopleDto } from '../dto/update-people.dto';
import { People } from '../schemas/people.schema';
import { People } from '../schemas/people.schema';
import * as Mailgun from 'mailgun-js';
import * as Mailgun from 'mailgun-js';
import config from 'src/config';
import config from 'src/config';
 
import * as bcrypt from 'bcrypt';
@Injectable()
@Injectable()
export class PeopleDao {
export class PeopleDao {
private mg = Mailgun({
/*private mg = Mailgun({
apiKey: config.mailgun.apiKey,
apiKey: config.mailgun.apiKey,
domain: config.mailgun.domain,
domain: config.mailgun.domain,
});
});
*/
constructor(
constructor(
@InjectModel(People.name)
@InjectModel(People.name)
private readonly _peopleModel: Model<People>,
private readonly _peopleModel: Model<People>,
) {}
) {}
login = (email: string, password: string): Promise<People | void> =>
login = (email: string, password: string): Promise<People | void> =>
new Promise((resolve, reject) => {
new Promise(async (resolve, reject) => {
this._peopleModel.findOne(
this._peopleModel.findOne({ email: email }, async (err, value) => {
{ email: email, passwordHash: password },
if (err) reject(err.message);
(err, value) => {
if (!value)
if (err) reject(err.message);
reject(new NotFoundException('Email or password is incorrect!'));
if (!value)
const isPasswordCorrect = await bcrypt.compare(
reject(new NotFoundException('Email or password is incorrect!'));
password,
resolve(value);
value.passwordHash,
},
);
);
if (!isPasswordCorrect) {
 
reject(new NotFoundException('Email or password is incorrect!'));
 
}
 
value.passwordHash = password
 
resolve(value);
 
});
});
});
find = (): Promise<People[]> =>
find = (): Promise<People[]> =>
@@ -55,8 +61,8 @@ export class PeopleDao {
@@ -55,8 +61,8 @@ export class PeopleDao {
});
});
});
});
save = (people: CreatePeopleDto): Promise<People> => {
save = async (people: CreatePeopleDto): Promise<People> => {
people.passwordHash = this.secret();
people.passwordHash = await this.secret();
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
new this._peopleModel(people).save((err, value) => {
new this._peopleModel(people).save((err, value) => {
if (err) reject(err.message);
if (err) reject(err.message);
@@ -95,7 +101,7 @@ export class PeopleDao {
@@ -95,7 +101,7 @@ export class PeopleDao {
});
});
});
});
secret = (length = 10) => {
secret = async (length = 10) => {
const upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const lowerCase = 'abcdefghijklmnopqrstuvwxyz';
const lowerCase = 'abcdefghijklmnopqrstuvwxyz';
const digits = '0123456789';
const digits = '0123456789';
@@ -108,7 +114,11 @@ export class PeopleDao {
@@ -108,7 +114,11 @@ export class PeopleDao {
let secret = '';
let secret = '';
for (let index = 0; index < length; index++)
for (let index = 0; index < length; index++)
secret += alphabet.charAt(randomInt(alphabet.length));
secret += alphabet.charAt(randomInt(alphabet.length));
return secret;
 
const saltOrRounds = 10;
 
const hash = await bcrypt.hash(secret, saltOrRounds);
 
console.log(secret);
 
return hash;
};
};
async sendPassword(email: string, password: string) {
async sendPassword(email: string, password: string) {
@@ -119,12 +129,12 @@ export class PeopleDao {
@@ -119,12 +129,12 @@ export class PeopleDao {
text: `Congratulations! Your account is activated. Your InternshipManager password is "${password}"`,
text: `Congratulations! Your account is activated. Your InternshipManager password is "${password}"`,
};
};
await this.mg.messages().send(data, function (error, body) {
/*await this.mg.messages().send(data, function (error, body) {
if (error) {
if (error) {
console.log(error);
console.log(error);
} else {
} else {
console.log(body);
console.log(body);
}
}
});
});*/
}
}
}
}
Loading