Skip to content

Commit 2222a6d

Browse files
committed
Authentication
1 parent 9d86ac3 commit 2222a6d

File tree

21 files changed

+826
-71
lines changed

21 files changed

+826
-71
lines changed

package-lock.json

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

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,27 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16+
"bcrypt": "^5.0.1",
1617
"celebrate": "^15.0.0",
1718
"cors": "^2.8.5",
1819
"express": "^4.17.1",
1920
"inversify": "^5.1.1",
21+
"jsonwebtoken": "^8.5.1",
2022
"mysql": "^2.14.1",
2123
"passport": "^0.4.1",
24+
"passport-jwt": "^4.0.0",
2225
"reflect-metadata": "^0.1.13",
2326
"typeorm": "0.2.34"
2427
},
2528
"devDependencies": {
29+
"@types/bcrypt": "^5.0.0",
2630
"@types/express": "^4.17.13",
31+
"@types/jsonwebtoken": "^8.5.5",
2732
"@types/node": "^8.0.29",
33+
"@types/passport": "^1.0.7",
34+
"@types/passport-jwt": "^3.0.6",
35+
"nodemon": "^2.0.12",
2836
"ts-node": "3.3.0",
29-
"typescript": "4.3",
30-
"nodemon": "^2.0.12"
37+
"typescript": "4.3"
3138
}
3239
}

src/config/passport.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import passport from "passport";
2+
import passportJWT from "passport-jwt";
3+
import { myContainer } from "../inversify.config";
4+
import { TYPES } from "../types";
5+
import IUserRepositoryInterface from "../repositories/interfaces/userInterface";
6+
7+
const JWTStrategy = passportJWT.Strategy;
8+
const ExtractJWT = passportJWT.ExtractJwt;
9+
10+
const userRepo: IUserRepositoryInterface = myContainer.get<IUserRepositoryInterface>(TYPES.IUserRepositoryInterface);
11+
12+
passport.use(new JWTStrategy({
13+
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
14+
secretOrKey : process.env.passportSecret || 'x?@$xqXVZp?7P#PG'
15+
},
16+
function (jwtPayload, done) {
17+
console.log("token passport: ", jwtPayload);
18+
userRepo.find(jwtPayload.sub)
19+
.then(user => {
20+
done(null, user);
21+
})
22+
.catch(error => {
23+
done(error, null);
24+
})
25+
}
26+
))

src/controllers/authController.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { TYPES } from "../types"
2+
import { inject, injectable } from "inversify"
3+
import { Request, Response, NextFunction } from "express"
4+
import { IUserInputDTO, IAuthUserInputDTO } from "../dtos/userDTO"
5+
import AuthService from "../services/auth"
6+
7+
8+
@injectable()
9+
export default class AuthController {
10+
public authService: AuthService
11+
12+
constructor(@inject(TYPES.AuthService) authService: AuthService) {
13+
this.authService = authService
14+
}
15+
16+
public async register(req: Request, res: Response, next: NextFunction) {
17+
// check if userName is already registered
18+
const token = await this.authService.register(req.body as IAuthUserInputDTO)
19+
res.status(200).send(token);
20+
}
21+
22+
public async signIn(req: Request, res: Response, next: NextFunction) {
23+
const token = await this.authService.signIn(req.body as IAuthUserInputDTO)
24+
res.status(200).send(token);
25+
}
26+
27+
public async deactivate(req: Request, res: Response, next: NextFunction) {
28+
const token = req.headers.authorization.replace("Bearer", "");
29+
await this.authService.deactivate(token);
30+
res.status(200).send("User account decativated");
31+
}
32+
}

src/controllers/postController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { TYPES } from "../types"
2-
import { IPostInterface } from "../repositories/interfaces/postInterface"
2+
import IPostRepositoryInterface from "../repositories/interfaces/postInterface"
33
import { inject, injectable } from "inversify"
44
import { Request, Response, NextFunction } from "express"
55
import { IPostInputDTO } from "../dtos/postDTO"
66

77

88
@injectable()
99
export default class PostController {
10-
public postRepo: IPostInterface
10+
public postRepo: IPostRepositoryInterface
1111

12-
constructor(@inject(TYPES.IPostInterface) postRepo: IPostInterface) {
12+
constructor(@inject(TYPES.IPostRepositoryInterface) postRepo: IPostRepositoryInterface) {
1313
this.postRepo = postRepo;
1414
}
1515

src/controllers/userController.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { TYPES } from "../types"
2+
import IUserRepositoryInterface from "../repositories/interfaces/userInterface"
3+
import { inject, injectable } from "inversify"
4+
import { Request, Response, NextFunction } from "express"
5+
import { IPostInputDTO } from "../dtos/postDTO"
6+
7+
8+
@injectable()
9+
export default class UserController {
10+
public userRepo: IUserRepositoryInterface
11+
12+
constructor(@inject(TYPES.IUserRepositoryInterface) userRepo: IUserRepositoryInterface) {
13+
this.userRepo = userRepo;
14+
}
15+
16+
public async index(req: Request, res: Response, next: NextFunction) {
17+
const users = await this.userRepo.all();
18+
res.status(200).send(users);
19+
}
20+
21+
public async show(req: Request, res: Response, next: NextFunction) {
22+
const user = await this.userRepo.find(+req.params.id);
23+
res.status(200).send(user);
24+
}
25+
}

src/dtos/userDTO.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface IUserDTO {
2+
id: number,
3+
userName: string,
4+
isActive: boolean,
5+
created_at?: string,
6+
updated_at?: string,
7+
}
8+
9+
export interface IUserInputDTO {
10+
userName?: string,
11+
password?: string,
12+
isActive?: boolean,
13+
}
14+
15+
export interface IAuthUserInputDTO {
16+
userName: string,
17+
password: string,
18+
}
19+
20+
export interface IAuthUserDTO extends IAuthUserInputDTO{
21+
id: number
22+
}

src/entity/User.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, OneToMany} from "typeorm";
1+
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, OneToMany, BeforeInsert} from "typeorm";
22
import { Post } from "./Post";
33

44
@Entity()
@@ -8,10 +8,15 @@ export class User extends BaseEntity {
88
id: number;
99

1010
@Column()
11+
password: string;
12+
13+
@Column({
14+
unique: true
15+
})
1116
userName: string;
1217

13-
@Column()
14-
isActive: boolean;
18+
@Column('boolean', {default: true})
19+
isActive: boolean = true;
1520

1621
@OneToMany(() => Post, post => post.user)
1722
posts: Post[];

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {createConnection, Connection} from "typeorm";
33
import * as express from "express";
44
import { Application } from 'express';
55
import routes from "./routes";
6+
import dotenv from "dotenv";
7+
dotenv.config({ path: ".env" });
68

79
const app: Application = express();
810

src/inversify.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { Container } from "inversify";
22
import { TYPES } from "./types";
3-
import { IPostInterface } from "./repositories/interfaces/postInterface";
3+
import IPostRepositoryInterface from "./repositories/interfaces/postInterface";
44
import PostRepository from "./repositories/typeORM/postRepo";
5+
import IUserRepositoryInterface from "./repositories/interfaces/userInterface";
6+
import UserRepository from "./repositories/typeORM/userRepo";
57

68
const myContainer = new Container();
7-
myContainer.bind<IPostInterface>(TYPES.IPostInterface).to(PostRepository);
9+
myContainer.bind<IPostRepositoryInterface>(TYPES.IPostRepositoryInterface).to(PostRepository);
10+
myContainer.bind<IUserRepositoryInterface>(TYPES.IUserRepositoryInterface).to(UserRepository);
811

912
export { myContainer };

0 commit comments

Comments
 (0)