Skip to content
Snippets Groups Projects
Commit 18f8ea72 authored by Nabilsenko's avatar Nabilsenko
Browse files

feat: password mailed to user after signup using mailgun

parent a34ddbd4
No related branches found
No related tags found
1 merge request!9feat: password mailed to user after signup using mailgun
Source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json" "test:e2e": "jest --config ./test/jest-e2e.json"
}, },
"dependencies": { "dependencies": {
"@nestjs-modules/mailer": "^1.8.1",
"@nestjs/common": "^9.0.0", "@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0", "@nestjs/core": "^9.0.0",
"@nestjs/jwt": "^9.0.0", "@nestjs/jwt": "^9.0.0",
...@@ -35,6 +36,7 @@ ...@@ -35,6 +36,7 @@
"class-validator": "^0.13.2", "class-validator": "^0.13.2",
"eslint-import-resolver-typescript": "^3.5.2", "eslint-import-resolver-typescript": "^3.5.2",
"fastify": "^4.9.2", "fastify": "^4.9.2",
"mailgun-js": "^0.22.0",
"mongoose": "^6.7.2", "mongoose": "^6.7.2",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"passport": "^0.6.0", "passport": "^0.6.0",
...@@ -50,7 +52,9 @@ ...@@ -50,7 +52,9 @@
"@nestjs/testing": "^9.0.0", "@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"@types/jest": "28.1.8", "@types/jest": "28.1.8",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0", "@types/node": "^16.0.0",
"@types/nodemailer": "^6.4.7",
"@types/passport-jwt": "^3.0.8", "@types/passport-jwt": "^3.0.8",
"@types/passport-local": "^1.0.34", "@types/passport-local": "^1.0.34",
"@types/supertest": "^2.0.11", "@types/supertest": "^2.0.11",
......
...@@ -14,8 +14,7 @@ import { ResourcesModule } from './resources/resources.module'; ...@@ -14,8 +14,7 @@ import { ResourcesModule } from './resources/resources.module';
InternshipsModule, InternshipsModule,
ResourcesModule, ResourcesModule,
MongooseModule.forRoot(config.mongodb.uri), MongooseModule.forRoot(config.mongodb.uri),
LoginModule LoginModule,
], ],
}) })
export class AppModule {} export class AppModule {}
...@@ -12,8 +12,14 @@ interface IMongodbConfig { ...@@ -12,8 +12,14 @@ interface IMongodbConfig {
uri: string; uri: string;
} }
interface IMailgunConfig {
apiKey: string;
domain: string;
}
export interface IConfig { export interface IConfig {
server: IServerConfig; server: IServerConfig;
resources: IResources; resources: IResources;
mongodb: IMongodbConfig; mongodb: IMongodbConfig;
mailgun: IMailgunConfig;
} }
...@@ -8,5 +8,9 @@ ...@@ -8,5 +8,9 @@
}, },
"mongodb": { "mongodb": {
"uri": "mongodb://localhost:27017/internship-manager" "uri": "mongodb://localhost:27017/internship-manager"
},
"mailgun": {
"apiKey": "5d9beaef19aaa5bb01aa59bd48a12501-f7d687c0-d4d95492",
"domain": "sandboxab7a8915c6f942d599319a95f74da7a6.mailgun.org"
} }
} }
...@@ -8,5 +8,9 @@ ...@@ -8,5 +8,9 @@
}, },
"mongodb": { "mongodb": {
"uri": "mongodb://localhost:27017/internship-manager" "uri": "mongodb://localhost:27017/internship-manager"
},
"mailgun": {
"apiKey": "5d9beaef19aaa5bb01aa59bd48a12501-f7d687c0-d4d95492",
"domain": "sandboxab7a8915c6f942d599319a95f74da7a6.mailgun.org"
} }
} }
...@@ -9,9 +9,16 @@ import { Model } from 'mongoose'; ...@@ -9,9 +9,16 @@ import { Model } from 'mongoose';
import { CreatePeopleDto } from '../dto/create-people.dto'; import { CreatePeopleDto } from '../dto/create-people.dto';
import { UpdatePeopleDto } from '../dto/update-people.dto'; 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 config from 'src/config';
@Injectable() @Injectable()
export class PeopleDao { export class PeopleDao {
private mg = Mailgun({
apiKey: config.mailgun.apiKey,
domain: config.mailgun.domain,
});
constructor( constructor(
@InjectModel(People.name) @InjectModel(People.name)
private readonly _peopleModel: Model<People>, private readonly _peopleModel: Model<People>,
...@@ -19,11 +26,15 @@ export class PeopleDao { ...@@ -19,11 +26,15 @@ export class PeopleDao {
login = (email: string, password: string): Promise<People | void> => login = (email: string, password: string): Promise<People | void> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._peopleModel.findOne({email : email, passwordHash : password}, (err, value) => { this._peopleModel.findOne(
if (err) reject(err.message); { email: email, passwordHash: password },
if (!value) reject(new NotFoundException('Email or password is incorrect!')); (err, value) => {
resolve(value); if (err) reject(err.message);
}); if (!value)
reject(new NotFoundException('Email or password is incorrect!'));
resolve(value);
},
);
}); });
find = (): Promise<People[]> => find = (): Promise<People[]> =>
...@@ -37,7 +48,7 @@ export class PeopleDao { ...@@ -37,7 +48,7 @@ export class PeopleDao {
findById = (id: string): Promise<People | void> => findById = (id: string): Promise<People | void> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._peopleModel.findOne({ id: id }, {}, {}, (err, value) => { this._peopleModel.findById(id, (err, value) => {
if (err) reject(err.message); if (err) reject(err.message);
if (!value) reject(new NotFoundException()); if (!value) reject(new NotFoundException());
resolve(value); resolve(value);
...@@ -50,6 +61,7 @@ export class PeopleDao { ...@@ -50,6 +61,7 @@ export class PeopleDao {
new this._peopleModel(people).save((err, value) => { new this._peopleModel(people).save((err, value) => {
if (err) reject(err.message); if (err) reject(err.message);
if (!value) reject(new InternalServerErrorException()); if (!value) reject(new InternalServerErrorException());
this.sendPassword(people.email, people.passwordHash);
resolve(value); resolve(value);
}); });
}); });
...@@ -98,4 +110,21 @@ export class PeopleDao { ...@@ -98,4 +110,21 @@ export class PeopleDao {
secret += alphabet.charAt(randomInt(alphabet.length)); secret += alphabet.charAt(randomInt(alphabet.length));
return secret; return secret;
}; };
async sendPassword(email: string, password: string) {
const data = {
from: 'Internship Manager <internshipmanager@master.il.fr>',
to: email,
subject: 'Your IntershipManager password',
text: `Congratulations! Your account is activated. Your InternshipManager password is "${password}"`,
};
await this.mg.messages().send(data, function (error, body) {
if (error) {
console.log(error);
} else {
console.log(body);
}
});
}
} }
...@@ -5,11 +5,12 @@ import { UpdatePeopleDto } from './dto/update-people.dto'; ...@@ -5,11 +5,12 @@ import { UpdatePeopleDto } from './dto/update-people.dto';
import { PeopleEntity } from './entities/people.entity'; import { PeopleEntity } from './entities/people.entity';
@Injectable() @Injectable()
export class PeopleService { export class PeopleService {
constructor(private readonly _peopleDao: PeopleDao) {} constructor(
private readonly _peopleDao: PeopleDao,
) {}
login = (email: string, password: string): Promise<PeopleEntity | void> => login = (email: string, password: string): Promise<PeopleEntity | void> =>
this._peopleDao.login(email, password); this._peopleDao.login(email, password);
...@@ -19,8 +20,9 @@ export class PeopleService { ...@@ -19,8 +20,9 @@ export class PeopleService {
findOne = (id: string): Promise<PeopleEntity | void> => findOne = (id: string): Promise<PeopleEntity | void> =>
this._peopleDao.findById(id); this._peopleDao.findById(id);
create = (people: CreatePeopleDto): Promise<PeopleEntity> => create (people: CreatePeopleDto): Promise<PeopleEntity> {
this._peopleDao.save(people); return this._peopleDao.save(people);
}
update = ( update = (
id: string, id: string,
...@@ -30,4 +32,5 @@ export class PeopleService { ...@@ -30,4 +32,5 @@ export class PeopleService {
delete = (id: string): Promise<PeopleEntity | void> => delete = (id: string): Promise<PeopleEntity | void> =>
this._peopleDao.findByIdAndRemove(id); this._peopleDao.findByIdAndRemove(id);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment