I and trying to build an using a php API, I have been able to login, but when I tried to access other endpoints like logout I get the error “Preformatted textERROR TypeError: Cannot read property ‘token_type’ of undefined”
below is my code
auth.services TS
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { tap } from 'rxjs/operators';
import { ApiService } from './api.service';
import { User } from '../models/user';
import { NativeStorage } from '@ionic-native/native-storage/ngx';
@Injectable({
providedIn: 'root'
})
export class AuthService {
isLoggedIn = false;
token:any;
constructor(
private http: HttpClient,
private api: ApiService,
private storage: NativeStorage,
) { }
login(account_number: Number, password: String) {
return this.http.post(this.api.API_URL + '/login',
{account_number: account_number, password: password}
).pipe(
tap(token => {
this.storage.setItem('token', token)
.then(
() => {
console.log('Token Stored', token);
},
error => console.error('Error storing item', error)
);
this.token = token;
this.isLoggedIn = true;
return token;
}),
);
}
register(name: String, email: String, phone: Number, reference: String, account_number: String, password: String) {
return this.http.post(this.api.API_URL + '/register',
{name: name, email: email, phone: phone, reference: reference, account_number: account_number, password: password}
)
}
logout() {
const headers = new HttpHeaders({
'Authorization': this.token["token_type"]+" "+this.token["access_token"]
});
return this.http.get(this.api.API_URL + '/logout', { headers: headers })
.pipe(
tap(data => {
this.storage.remove("token");
this.isLoggedIn = false;
delete this.token;
return data;
})
)
}
user() {
const headers = new HttpHeaders({
'Authorization': this.token["token_type"]+" "+this.token["access_token"]
});
return this.http.get<User>(this.api.API_URL + '/account_balance', { headers: headers })
.pipe(
tap(user => {
return user;
})
)
}
getToken() {
return this.storage.getItem('token').then(
data => {
this.token = data;
if(this.token != null) {
this.isLoggedIn=true;
} else {
this.isLoggedIn=false;
}
},
error => {
this.token = null;
this.isLoggedIn=false;
}
);
}
}
the error