0

I’m trying to get a variable I declarated onmy header through back end, but this codes is not working:

In my backend:(express + knex):

    async login(req, res) {
    const {
      user_name,
      email
    } = req.body;

    let {
      id
    } = await connetion('user')
      .where('email', email)
      .andWhere('user_name', user_name)
      .select(['id'])
      .first();

    if (id == undefined) {
      console.log('entrou no if'.italics.blue);

      await connetion('user').insert({
        user_name,
        email
      })

      let {
        id
      } = await connetion('user')
        .where('email', email)
        .andWhere('user_name', user_name)
        .select(['id'])
        .first();

      res.header('authorization', id);
      console.log(id);


      console.log('\n<ยบ>'.bold.yellow + ' novo usuario '.cyan + user_name + ' criado e logado'.cyan);
      res.status(201).send(); //? 201 : created
      return res.json({
        id
      })
    }

Im my service.ts(angular):

    import { retry, map } from 'rxjs/operators';
    import { host } from './host';
    import { User } from './../models/user';
    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';

    @Injectable({providedIn: 'root'})
    export class AuthService {
      constructor(private http: HttpClient) {}

      public login(user: User): any {
        return this.http.post(host+'/login', user, {observe: 'response', headers: 'autorization'})
          .pipe(
            map(res => { return res})
          ) 
      }
    }

Im my component.ts(angular):

  login(){
    this.authService.login(new User({
        user_name: this.user_name,
        email: this.email
      })
    ).pipe(first())
    .subscribe(
      ((data: HttpResponse<any>) => {
        console.log("response :");
        console.log(data);
        console.log("response.headers: ");
        console.log(data.headers);
        console.log("response.headers.get('authorization'): ");
        console.log(data.headers.get('authorization'));
      })
    )

the console.logs: 1

and here is the authorization in the responseHeader(network table):

2

please, if u can help me with this i will give you a big virtual hug (quarantine)

1
  • Can you please show all values in HttpHeaders by expanding it in console? Also, show how your setting the variable in the backend Commented May 24, 2020 at 6:21

1 Answer 1

0

In your response headers from backend you have to add

Access-Control-Expose-Headers: authorization

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.