Skip to content

Commit c00aa37

Browse files
author
Your Name
committed
angular security course
1 parent 1e378c0 commit c00aa37

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

server/auth.middleware.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Request, Response, NextFunction} from 'express';
2+
3+
4+
export function checkIfAuthenticated(req: Request, res: Response, next: NextFunction) {
5+
6+
if (req['userId']) {
7+
next();
8+
}
9+
else {
10+
res.sendStatus(403);
11+
}
12+
13+
14+
}
15+
16+

server/get-user.middleware.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22

33

4-
import {Request, Response, NextFunction} from 'express';
54
import {decodeJwt} from "./security.utils";
5+
import {Request, Response, NextFunction} from 'express';
66

77

88
export function retrieveUserIdFromRequest(req: Request, res: Response, next: NextFunction) {
@@ -17,6 +17,9 @@ export function retrieveUserIdFromRequest(req: Request, res: Response, next: Nex
1717
next();
1818
})
1919
}
20+
else {
21+
next();
22+
}
2023
}
2124

2225

server/get-user.route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getUser(req:Request, res:Response) {
1010
const user = db.findUserById(req["userId"]);
1111

1212
if (user) {
13-
res.status(200).json({email:user.email});
13+
res.status(200).json({email:user.email, id:user.id});
1414
}
1515
else {
1616
res.sendStatus(204);

server/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {getUser} from "./get-user.route";
1010
import {logout} from "./logout.route";
1111
import {login} from "./login.route";
1212
import {retrieveUserIdFromRequest} from "./get-user.middleware";
13+
import {checkIfAuthenticated} from "./auth.middleware";
1314
const bodyParser = require('body-parser');
1415
const cookieParser = require('cookie-parser');
1516

@@ -29,10 +30,9 @@ const optionDefinitions = [
2930

3031
const options = commandLineArgs(optionDefinitions);
3132

32-
3333
// REST API
3434
app.route('/api/lessons')
35-
.get(readAllLessons);
35+
.get(checkIfAuthenticated, readAllLessons);
3636

3737
app.route('/api/signup')
3838
.post(createUser);
@@ -41,7 +41,7 @@ app.route('/api/user')
4141
.get(getUser);
4242

4343
app.route('/api/logout')
44-
.post( logout);
44+
.post(checkIfAuthenticated, logout);
4545

4646
app.route('/api/login')
4747
.post(login);

src/app/services/auth.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export class AuthService {
2626
.subscribe(user => this.subject.next(user ? user : ANONYMOUS_USER));
2727
}
2828

29+
30+
31+
2932
signUp(email:string, password:string ) {
3033

3134
return this.http.post<User>('/api/signup', {email, password})

0 commit comments

Comments
 (0)