Reputation: 2527
I have the below code where I am getting the Auth Token,
@Injectable()
export class AuthenticationService {
@Input() isValid:boolean=false;
constructor(private http: HttpClient, private router: Router) { }
auth_token:string;
/** sign in user */
login(username, password) {
this.req = this.http.post('URL for Authenticaion', {
"username": username,
"password": password
})
.subscribe(
result => {
console.log(result);
this.router.navigate(['dashboard']);
}
}
Now I want to pass this token to another service which will retrieve all the data. The issue I am facing is result.auth_token is local to this current block so its not recognized outside of it. How do I return its value?
Upvotes: 1
Views: 32
Reputation: 9764
You can use Subject/BehaviorSubject which will act as observer and observable. You can emit the token and catch in the another service. Here is the code
In AuthenticationService:
private modelAlertEvent = new Subject<any>();
public modelAlertEventObservable = this.modelAlertEvent.asObservable();
this.modelAlertEvent.next(token);
In Another Service
this.modelsubscribe = this.authservice.modelAlertEventObservable.subscribe(token=>{
console.log(token);
});
Upvotes: 1