1

I have an Angular-springboot program with jwt authentication, I would like to know how I can once logged in on angular, recover the data of who logged in, for example the username. I looked at some guides on authguard and httpinterceptors but I am a bit confused in rigurado, can any of you please post me a code in which it retrieves data? I have services copied from the guides which are auth-guard, authentication, basic-auth-http-interceptors and httpclient I think are pretty standard but if needed I will publish them

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
matteo sperti
  • 31
  • 1
  • 7
  • Hello, First off, some of the services you listed aren't really used for retrieving/getting data. For example: - Authguard: If you're referring to a RouteGuard, these are used to allow/block the user from accessing a particular route, which is commonly based on authentication (https://angular.io/guide/router#milestone-5-route-guards) - HttpInterceptors: These are used to perform any additional logic for whichever API requests you want to target (https://angular.io/api/common/http/HttpInterceptor) What do you do with the authenticated user's details once they log in? (like their id) – christian Nov 05 '19 at 19:20
  • **TYPO** alert - it's a **guard** - not a "gaurd" .... – marc_s Feb 26 '20 at 19:40

2 Answers2

0

When user log in to the system save the token type and jwt token in the local storage. With the help of TokenInterceptor it sends header details in every request.

import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor, HttpResponse
} from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  constructor() { }
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    request = request.clone({
      setHeaders: {
        Authorization: `${localStorage.getItem('token_type')} ${localStorage.getItem('jwt_token')}`
      }
    });
    return next.handle(request);
  }
}

In app.module.ts add provider { provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true }

dasunse
  • 2,839
  • 1
  • 14
  • 32
0

thank you, yes i have an interceptor like this that save in the local storage, but if i am in another component, and i want call this, for example i press a button and, in the method i want do for example, a post that if you press this button you have to be logged ok?, and you have to update db, so i need to know who pressed this, how can retrieve this?

matteo sperti
  • 31
  • 1
  • 7