jwt connection from angular project to jhipster

前端 未结 2 550
既然无缘
既然无缘 2021-01-28 15:16

i have an angular basic project and a simple microservice jhipster project. since i chosed jwt option in my jhipster project , i want to consume methods from my angular project.

相关标签:
2条回答
  • 2021-01-28 15:37

    Well, seems your server does not provide authorities in the JWT response. Since the data is missing, the saveAuthorities saves string "undefined" that cannot be served later.

    If the reason authorities is missing is that you blindly copied some random code off the internet, delete the authorities from class JwtResponse and saveAuthorities, getAuthorities and all references to them.

    Otherwise, just provide the authorities in your response.

    Or you can check if authorities exist when saving them in onSubmit callback.

    if (data.authorities)
        this.tokenStorage.saveAuthorities(data.authorities);
    
    0 讨论(0)
  • 2021-01-28 15:46

    You have a typo in your condition in the getAuthorities method. The token is undefined. The error you have comes from trying to parse undefined in JSON.parse(...). You check the TOKEN_KEY in storage but read AUTHORITIES_KEY

    Your code:

    if (sessionStorage.getItem(TOKEN_KEY)) {
      JSON.parse(sessionStorage.getItem(AUTHORITIES_KEY)).forEach(authority => {
    

    Correct code:

    if (sessionStorage.getItem(AUTHORITIES_KEY)) {
      JSON.parse(sessionStorage.getItem(AUTHORITIES_KEY)).forEach(authority => {
    
    0 讨论(0)
提交回复
热议问题