0

I have a logout method in my auth service. I'm trying to call it from a component, however this does not seem to be working.

template

<a class="dropdown-item" (click)="auth.logout()">Logout</a>

component

import { AuthService } from '../../_services/auth.service';

constructor(
    public auth:AuthService
  ) {}

auth service

  public logout() {
    return this.http.post<any>(this.apiHost+'/users/logout', {})
    .map(() => {
      localStorage.removeItem('currentUser');
      this.router.navigate(['/']);
      this.alertService.warning('You have been logged out.', true)
    })
    .catch(this.handleError);
  }

What am I doing wrong? Do I need to call a local function instead of calling auth directly from the template?

2
  • tried adding some console logs in the service method. They are not called at all. Commented Feb 5, 2018 at 2:43
  • @pengyy Why strip out the syntax highlight? Commented Feb 5, 2018 at 4:03

1 Answer 1

2

You can returning an Observable from logout function. But calling this function don't means subscribe from the Observable.

Try to add subscribe() to subscribe it.

public logout() {
  return this.http.post<any>(this.apiHost+'/users/logout', {})
    .map(() => {
      localStorage.removeItem('currentUser');
      this.router.navigate(['/']);
      this.alertService.warning('You have been logged out.', true)
    })
    .catch(this.handleError)
    .subscribe();
}
Sign up to request clarification or add additional context in comments.

2 Comments

adding a .subscribe() to the end in the template did the trick.
(click)="auth.logout().subscribe()"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.