How do I get my token in the header?

Asked

Viewed 153 times

1

Guys, I’m 'stuck' in this part of the code, I’m generating my token pro browser but I can’t access it to save in my localStorage. I can’t, in any way access my header this way, every time returns null

My Python class, responsible for submitting the token:

class Login(Resource):
    def post(self):
        parser.add_argument('username', type=str)
        parser.add_argument('password', type=str)
        args = parser.parse_args()
        password = hashlib.md5(args['password'].encode('utf-8')).hexdigest()
        sql = "SELECT * FROM usuarios WHERE username = (%s) and password = (%s)"
        value = (args['username'], password)
        dbcursor.execute(sql, value)
        result = dbcursor.fetchall()
        if len(result) > 0:
            id_user = result[0][0]
            username = result[0][1]
            print(id_user)
            payload = {"id": id_user, "username": username}
            print(payload)
            authtoken = jwt.encode(payload, config.JWT.secret, algorithm='HS256')
            print(authtoken)
            data = json.dumps({"id": id_user, "username": result[0][1], "token": authtoken .decode("UTF-8)")})
            print(data)
            response = Response(str(data),mimetype='application/json')
            print(response)
            response.headers['x-access-token'] = "JWT" + authtoken.decode("UTF-8")
            print(response.headers)
            return response
        else:
            return {"message": "user invalido"}

My component in Angular

  userLogin(){
// Pegando os valores do HTML INPUT e jogando nas constantes
    const username = this.loginForm.get('username').value;
    const password = this.loginForm.get('password').value;

    this.authService
        .authenticate(username, password)
    .subscribe((res) => {
      const authToken = res.headers['x-access-token']
      console.log(authToken)
      if (res ["message"] == 'user valido') {
          this.router.navigate(['dashboard'])
      }
      else {
        console.log('Caiu no else')
        this.loginForm.reset()

usernameInput.nativeElement */ this.platformDetectorService.isPlatformBrower() && this.usernameInput.nativeElement.Focus()
} } )}

My service in Angular:

  authenticate(username: string, password: string) {    
    return this.http
      .post(
        API_URL,
        { "username":username, "password":password }, { observe: 'response' } ) 
      .pipe(tap(res => {
        const authToken = res.headers.get('x-access-token')
        console.log(authToken)
      }))
  }

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Please someone to give me a north ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.