Skip to content

Commit 9fdd455

Browse files
passport integration
1 parent b02824a commit 9fdd455

File tree

8 files changed

+150
-8
lines changed

8 files changed

+150
-8
lines changed

package-lock.json

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
"@nestjs/common": "^7.6.15",
2929
"@nestjs/config": "^0.6.3",
3030
"@nestjs/core": "^7.6.15",
31+
"@nestjs/passport": "^7.1.5",
3132
"@nestjs/platform-express": "^7.6.15",
3233
"@nestjs/typeorm": "^7.1.5",
3334
"bcrypt": "^5.0.1",
3435
"dotenv": "^10.0.0",
3536
"joi": "^17.4.0",
37+
"passport": "^0.4.1",
38+
"passport-local": "^1.0.0",
3639
"pg": "^8.6.0",
3740
"reflect-metadata": "^0.1.13",
3841
"rimraf": "^3.0.2",
@@ -47,6 +50,7 @@
4750
"@types/jest": "^26.0.22",
4851
"@types/joi": "^17.2.3",
4952
"@types/node": "^14.14.36",
53+
"@types/passport-local": "^1.0.33",
5054
"@types/pg": "^8.6.0",
5155
"@types/supertest": "^2.0.10",
5256
"@typescript-eslint/eslint-plugin": "^4.19.0",

src/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { LanguageService } from './services/language/language.service';
1010
import { AuthModule } from './auth/auth.module';
1111
import { UserService } from './services/user/user.service';
1212
import { UsersController } from './controllers/users/users.controller';
13+
import { AuthService } from './services/auth/auth.service';
14+
import { AuthModule } from './modules/auth/auth.module';
1315
import config from './config';
1416
@Module({
1517
imports: [
@@ -23,6 +25,6 @@ import config from './config';
2325
AuthModule,
2426
],
2527
controllers: [AppController, ProjectsController, LanguagesController, UsersController],
26-
providers: [AppService, ProjectService, LanguageService, UserService],
28+
providers: [AppService, ProjectService, LanguageService, UserService, AuthService],
2729
})
2830
export class AppModule { }

src/modules/auth/auth.module.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Module } from '@nestjs/common';
2+
import { LocalStrategy } from './strategies/local.strategy'
3+
import { PassportModule } from '@nestjs/passport'
4+
import { AuthService } from '../../services/auth/auth.service';
5+
//import {UserModule}
6+
@Module({
7+
imports: [PassportModule],
8+
providers: [AuthService, LocalStrategy]
9+
})
10+
export class AuthModule { }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable, UnauthorizedException } from '@nestjs/common';
2+
import { PassportStrategy } from '@nestjs/passport';
3+
4+
import { AuthService } from './../../../services/auth/auth.service';
5+
import { Strategy } from 'passport-local';
6+
7+
@Injectable()
8+
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
9+
constructor(private authService: AuthService) {
10+
super();
11+
}
12+
13+
async validate(email: string, password: string) {
14+
const user = this.authService.validateUser(email, password);
15+
if (!user) {
16+
throw new UnauthorizedException('not allow');
17+
}
18+
return user;
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { AuthService } from './auth.service';
3+
4+
describe('AuthService', () => {
5+
let service: AuthService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [AuthService],
10+
}).compile();
11+
12+
service = module.get<AuthService>(AuthService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

src/services/auth/auth.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from '@nestjs/common'
2+
import { UserService } from './../user/user.service'
3+
import * as bcrypt from 'bcrypt';
4+
5+
@Injectable()
6+
export class AuthService {
7+
constructor(private userService: UserService) { }
8+
9+
async validateUser(email: string, password: string) {
10+
const user = await this.userService.getByEmail(email)
11+
if (!user) {
12+
return null
13+
}
14+
const match = await bcrypt.compare(password, user.password)
15+
if (user) {
16+
return user
17+
}
18+
return null
19+
}
20+
}

src/services/user/user.service.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@ export class UserService {
2323
const user = await this.userRepo.findOne(
2424
{
2525
where: { id },
26-
select: ["username", "email", "role"]
26+
select: ["username", "email", "role", "creationDate"]
2727
}
2828
);
2929
if (!user) {
3030
throw new NotFoundException(`${this.singular} with id ${id} not found`);
3131
}
32-
return {
33-
username: user.username,
34-
email: user.email,
35-
role: user.role,
36-
projects: user.projects,
37-
creationDate: user.creationDate,
32+
return user
33+
}
34+
async getByEmail(email: string) {
35+
const user = await this.userRepo.findOne(
36+
{
37+
where: { email },
38+
select: ["username", "email", "role", "password", "creationDate"]
39+
}
40+
);
41+
if (!user) {
42+
throw new NotFoundException(`${this.singular} with email ${email} not found`);
3843
}
44+
return user
3945
}
4046
/*
4147

0 commit comments

Comments
 (0)