Skip to content

Commit f433039

Browse files
committed
Fix auth bug
1 parent 2222a6d commit f433039

File tree

8 files changed

+34
-17
lines changed

8 files changed

+34
-17
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"bcrypt": "^5.0.1",
1717
"celebrate": "^15.0.0",
1818
"cors": "^2.8.5",
19+
"dotenv": "^10.0.0",
1920
"express": "^4.17.1",
2021
"inversify": "^5.1.1",
2122
"jsonwebtoken": "^8.5.1",

src/config/passport.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import passport from "passport";
2-
import passportJWT from "passport-jwt";
1+
import * as passport from "passport";
2+
import * as passportJWT from "passport-jwt";
33
import { myContainer } from "../inversify.config";
44
import { TYPES } from "../types";
55
import IUserRepositoryInterface from "../repositories/interfaces/userInterface";

src/controllers/authController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { inject, injectable } from "inversify"
33
import { Request, Response, NextFunction } from "express"
44
import { IUserInputDTO, IAuthUserInputDTO } from "../dtos/userDTO"
55
import AuthService from "../services/auth"
6+
import { myContainer } from "../inversify.config";
67

78

89
@injectable()
910
export default class AuthController {
1011
public authService: AuthService
1112

12-
constructor(@inject(TYPES.AuthService) authService: AuthService) {
13-
this.authService = authService
13+
constructor() {
14+
this.authService = myContainer.resolve<AuthService>(AuthService);
1415
}
1516

1617
public async register(req: Request, res: Response, next: NextFunction) {

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ 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" });
6+
import * as dotenv from 'dotenv';
7+
8+
const env = dotenv.config();
9+
if (!env) {
10+
throw new Error("⚠️ Couldn't find .env file ⚠️");
11+
}
812

913
const app: Application = express();
1014

11-
const PORT = process.env.PORT || 8000;
15+
const PORT = process.env.PORT || 7000;
1216

1317
createConnection().then(async connection => {
1418
console.log("Connection to database successful")

src/routes/auth.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {Router, Request, Response, NextFunction} from 'express'
22
import AuthController from '../controllers/authController';
33
import { myContainer } from "../inversify.config";
4-
import passport from "passport";
5-
import * as passportConfig from "../config/passport";
4+
import * as passport from "passport";
5+
import "../config/passport";
66

77
const router = Router();
88
const authController: AuthController = myContainer.resolve<AuthController>(AuthController);
@@ -19,4 +19,8 @@ router.post('/deactivate', passport.authenticate('jwt',{session: false}), async
1919
await authController.deactivate(req, res, next)
2020
})
2121

22+
router.get('/test', passport.authenticate('jwt',{session: false}), async (req: Request, res: Response, next: NextFunction) => {
23+
res.send({message:"it worked"})
24+
})
25+
2226
export default router

src/routes/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Router } from 'express';
22
import userRoutes from './user';
3-
import postRoutes from './post'
3+
import postRoutes from './post';
4+
import authRoutes from './auth';
45

56

67
export default () => {
78
const appRouter = Router()
89
appRouter.use('/users', userRoutes);
910
appRouter.use('/posts', postRoutes);
11+
appRouter.use('/auth', authRoutes);
1012
return appRouter
1113
}

src/routes/post.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import {Router, Request, Response, NextFunction} from 'express'
22
import PostController from '../controllers/postController';
33
import { myContainer } from "../inversify.config";
4-
import passport from "passport";
5-
import * as passportConfig from "../config/passport";
6-
74
const router = Router();
5+
import * as passport from "passport";
6+
import "../config/passport";
7+
88
const postController: PostController = myContainer.resolve<PostController>(PostController);
99

1010
router.get('/', async (req: Request, res: Response, next: NextFunction) => {
1111
await postController.index(req, res, next)
1212
})
1313

14-
router.post('/', passport.authenticate('jwt',{session: false}), async (req: Request, res: Response, next: NextFunction) => {
14+
router.post('/', passport.authenticate('jwt',{session: false}), async (req: Request, res: Response, next: NextFunction) => {
1515
await postController.store(req, res, next)
1616
})
1717

0 commit comments

Comments
 (0)