Skip to content

Commit 788f101

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

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

README.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
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:
8-
> [Angular 8 JWT Authentication with HttpInterceptor and Router](https://bezkoder.com/angular-jwt-authentication/)
4+
> [Angular 8 JWT Authentication with Web API](https://bezkoder.com/angular-jwt-authentication/)
5+
6+
> [Angular 10 JWT Authentication with Web API](https://bezkoder.com/angular-10-jwt-auth/)
7+
8+
## With Spring Boot back-end
99

10-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.3.21.
10+
> [Angular 8 + Spring Boot: JWT Authentication & Authorization example](https://bezkoder.com/angular-spring-boot-jwt-auth/)
1111
12-
## Development server
12+
> [Angular 10 + Spring Boot: JWT Authentication & Authorization example](https://bezkoder.com/angular-10-spring-boot-jwt-auth/)
1313
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.
14+
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`.
1515

16-
## Code scaffolding
16+
## With Node.js Express back-end
1717

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`.
18+
> [Node.js Express + Angular 8: JWT Authentication & Authorization example](https://bezkoder.com/node-js-express-angular-jwt-auth/)
1919
20-
## Build
20+
> [Node.js Express + Angular 10: JWT Authentication & Authorization example](https://bezkoder.com/node-js-express-angular-10-jwt-auth/)
2121
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.
22+
Open `app/_helpers/auth.interceptor.js`, modify the code to work with **x-access-token** like this:
23+
```js
24+
...
2325

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

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

28-
## Running end-to-end tests
33+
intercept(req: HttpRequest<any>, next: HttpHandler) {
34+
...
35+
if (token != null) {
36+
// for Spring Boot back-end
37+
// authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token) });
2938

30-
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
39+
// for Node.js Express back-end
40+
authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, token) });
41+
}
42+
return next.handle(authReq);
43+
}
44+
}
3145

32-
## Further help
46+
...
47+
```
3348

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).
49+
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)