Skip to content

Commit b6f0063

Browse files
committed
add options for Node.js Express back-end
1 parent 6de87d2 commit b6f0063

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
# Angular 8 Spring Boot JWT Authentication example
2-
3-
> [Angular 8 + Spring Boot: JWT Authentication example](https://bezkoder.com/angular-spring-boot-jwt-auth/)
4-
51
# Angular 8 JWT Authentication example
62

73
For more detail, please visit:
84
> [Angular 8 JWT Authentication with HttpInterceptor and Router](https://bezkoder.com/angular-jwt-authentication/)
95
10-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.3.21.
11-
12-
## Development server
6+
## With Spring Boot back-end
137

14-
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
8+
> [Angular 8 + Spring Boot: JWT Authentication & Authorization example](https://bezkoder.com/angular-spring-boot-jwt-auth/)
159
16-
## Code scaffolding
10+
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`.
1711

18-
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
12+
## With Node.js Express back-end
1913

20-
## Build
14+
> [Node.js Express + Angular 8: JWT Authentication & Authorization example](https://bezkoder.com/node-js-express-angular-jwt-auth/)
2115
22-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
16+
Open `app/_helpers/auth.interceptor.js`, modify the code to work with **x-access-token** like this:
17+
```js
18+
...
2319

24-
## Running unit tests
20+
// const TOKEN_HEADER_KEY = 'Authorization'; // for Spring Boot back-end
21+
const TOKEN_HEADER_KEY = 'x-access-token'; // for Node.js Express back-end
2522

26-
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
23+
@Injectable()
24+
export class AuthInterceptor implements HttpInterceptor {
25+
...
2726

28-
## Running end-to-end tests
27+
intercept(req: HttpRequest<any>, next: HttpHandler) {
28+
...
29+
if (token != null) {
30+
// for Spring Boot back-end
31+
// authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token) });
2932

30-
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
33+
// for Node.js Express back-end
34+
authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, token) });
35+
}
36+
return next.handle(authReq);
37+
}
38+
}
3139

32-
## Further help
40+
...
41+
```
3342

34-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
43+
Run `ng serve --port 8081` for a dev server. Navigate to `http://localhost:8081/`.

src/app/_helpers/auth.interceptor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'
44

55
import { TokenStorageService } from '../_services/token-storage.service';
66

7-
const TOKEN_HEADER_KEY = 'Authorization';
7+
const TOKEN_HEADER_KEY = 'Authorization'; // for Spring Boot back-end
8+
// const TOKEN_HEADER_KEY = 'x-access-token'; // for Node.js Express back-end
89

910
@Injectable()
1011
export class AuthInterceptor implements HttpInterceptor {
@@ -14,7 +15,11 @@ export class AuthInterceptor implements HttpInterceptor {
1415
let authReq = req;
1516
const token = this.token.getToken();
1617
if (token != null) {
18+
// for Spring Boot back-end
1719
authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token) });
20+
21+
// for Node.js Express back-end
22+
// authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, token) });
1823
}
1924
return next.handle(authReq);
2025
}

0 commit comments

Comments
 (0)