vilmrcs / angular-8-jwt-authentication

Demo project for Angular 8 JWT Authentication with HttpInterceptor and Router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Angular 8 JWT Authentication example

For more detail, please visit:

Angular 8 JWT Authentication with HttpInterceptor and Router

With Spring Boot back-end

Angular 8 + Spring Boot: JWT Authentication & Authorization example

Run ng serve for a dev server. Navigate to http://localhost:4200/.

With Node.js Express back-end

Node.js Express + Angular 8: JWT Authentication & Authorization example

Open app/_helpers/auth.interceptor.js, modify the code to work with x-access-token like this:

...

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

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  ...

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    ...
    if (token != null) {
      // for Spring Boot back-end
      // authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token) });

      // for Node.js Express back-end
      authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, token) });
    }
    return next.handle(authReq);
  }
}

...

Run ng serve --port 8081 for a dev server. Navigate to http://localhost:8081/.

About

Demo project for Angular 8 JWT Authentication with HttpInterceptor and Router


Languages

Language:TypeScript 68.8%Language:HTML 21.9%Language:JavaScript 5.4%Language:CSS 4.0%