Skip to content

Commit 73997f5

Browse files
author
kamil.mysliwiec
committed
Update - middlewares
1 parent f7c8d10 commit 73997f5

36 files changed

Lines changed: 627 additions & 198 deletions

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
/node_modules
33

44
# IDE
5-
/.idea
5+
/.idea
6+
7+
# misc
8+
npm-debug.log

npm-debug.log

Lines changed: 0 additions & 111 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
1313
"dependencies": {
1414
"@types/body-parser": "0.0.33",
1515
"@types/express": "^4.0.34",
16+
"@types/jsonwebtoken": "^7.2.0",
1617
"@types/lodash": "^4.14.45",
1718
"@types/mongoose": "^4.7.2",
19+
"@types/passport": "^0.3.2",
20+
"@types/passport-jwt": "^2.0.19",
1821
"body-parser": "^1.15.2",
1922
"express": "^4.14.0",
23+
"jsonwebtoken": "^7.2.1",
2024
"lodash": "^4.17.4",
2125
"mongoose": "^4.7.6",
26+
"passport": "^0.3.2",
27+
"passport-jwt": "^2.2.1",
2228
"reflect-metadata": "^0.1.9",
2329
"rxjs": "^5.0.3",
2430
"socket.io": "^1.7.2",

src/app/app.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import * as express from "express";
1+
import* as express from "express";
22
import { ExpressConfig } from "./config";
33
import { NestApplication } from "./../nest/core/interfaces";
4+
import { PassportJWTConfig } from "./config/passport-jwt.config";
45

56
export class Application implements NestApplication {
6-
constructor(private app: express.Express) {
7+
8+
constructor(private app: express.Application) {
79
ExpressConfig.setupConfig(this.app);
10+
PassportJWTConfig.setupConfig(this.app);
811
}
912

1013
public start() {
1114
this.app.listen(3030, () => {
1215
console.log("Application listen on port:", 3030);
1316
});
1417
}
15-
18+
1619
}

src/app/config/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./app.config";
2-
export * from "./express.config";
2+
export * from "./express.config";
3+
export * from "./passport-jwt.config";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as _ from "lodash";
2+
import { Application } from "express";
3+
import * as passport from "passport";
4+
import { ExtractJwt, Strategy, StrategyOptions } from "passport-jwt";
5+
6+
var users = [
7+
{
8+
id: 1,
9+
name: 'jonathanmh',
10+
password: '%2yx4'
11+
},
12+
{
13+
id: 2,
14+
name: 'test',
15+
password: 'test'
16+
}
17+
];
18+
19+
export class PassportJWTConfig {
20+
static readonly secretKey = "XD";
21+
22+
static readonly jwtOptions: StrategyOptions = {
23+
jwtFromRequest: ExtractJwt.fromAuthHeader(),
24+
secretOrKey: PassportJWTConfig.secretKey,
25+
};
26+
27+
static setupConfig(app: Application) {
28+
this.init();
29+
app.use(passport.initialize());
30+
}
31+
32+
static init() {
33+
34+
var strategy = new Strategy(this.jwtOptions, (payload, next) => {
35+
console.log('payload received', payload);
36+
37+
var user = users[_.findIndex(users, {id: payload.id})];
38+
console.log(user);
39+
next(null, user || false);
40+
});
41+
42+
passport.use(strategy);
43+
}
44+
45+
}

src/app/models/db.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import * as mongoose from "mongoose";
2-
mongoose.connect("mongodb://localhost:27017/tracker");
2+
mongoose.connect("mongodb://localhost:27017/tracker", {
3+
server: {
4+
socketOptions: {
5+
socketTimeoutMS: 0,
6+
connectTimeoutMS: 0
7+
}
8+
}
9+
});
310
export { mongoose as db };

src/app/modules/app.module.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { Module } from "./../../nest/core/utils";
22
import { UsersModule } from "./users/users.module";
3+
import { AuthModule } from "./auth/auth.module";
34

45
@Module({
56
modules: [
6-
UsersModule
7+
UsersModule,
8+
AuthModule
79
],
810
})
9-
export class ApplicationModule {}
11+
export class ApplicationModule {
12+
configure() {
13+
console.log("app configured");
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Module } from "./../../../nest/core/utils";
2+
import { AuthRoute } from "./login.route";
3+
import { SharedModule } from "../shared.module";
4+
5+
@Module({
6+
modules: [
7+
SharedModule,
8+
],
9+
routes: [
10+
AuthRoute
11+
],
12+
components: [
13+
]
14+
})
15+
export class AuthModule {
16+
configure() {
17+
console.log("auth configured");
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as _ from "lodash";
2+
import * as jwt from "jsonwebtoken";
3+
import { Request, Response, NextFunction } from "express";
4+
import { Route } from "./../../../nest/core/utils";
5+
import { RequestMapping } from "../../../nest/core/utils/path.decorator";
6+
import { RequestMethod } from "../../../nest/core/enums/request-method.enum";
7+
import { PassportJWTConfig } from "../../config/passport-jwt.config";
8+
9+
var users = [
10+
{
11+
id: 1,
12+
name: 'jonathanmh',
13+
password: '%2yx4'
14+
},
15+
{
16+
id: 2,
17+
name: 'test',
18+
password: 'test'
19+
}
20+
];
21+
22+
@Route({ path: "login" })
23+
export class AuthRoute {
24+
25+
constructor() {}
26+
27+
@RequestMapping({ path: "/", method: RequestMethod.POST })
28+
fetchToken(req: Request, res: Response, next: NextFunction) {
29+
const { name, password } = req.body;
30+
31+
const user = users[_.findIndex(users, {name: name})];
32+
if (!user){
33+
res.status(401).json({message:"no such user found"});
34+
}
35+
36+
if(user.password === password) {
37+
const payload = {id: user.id};
38+
const token = jwt.sign(payload, PassportJWTConfig.secretKey);
39+
res.json({message: "ok", token: token});
40+
}
41+
else {
42+
res.status(401).json({message:"passwords did not match"});
43+
}
44+
}
45+
46+
}
47+

0 commit comments

Comments
 (0)