Need some help regarding Observables in a service

已查看 29 次
跳至第一个未读帖子

Simon Azzopardi

未读,
2016年6月30日 06:44:502016/6/30
收件人 AngularJS
Hi guys,

I am trying to do the AuthGuard for CanActivate and I am trying to get, through http, either true or false, if user is logged in.

I have this code in the AuthService.ts

 public subscribeIsUserLoggedIn() : Boolean{

        this.checkIfUserIsLoggedIn().subscribe(
            result => {

               this. _loginResponseModel = result;
                 console.log(this._loginResponseModel.IsAuthenticated);
            }
             
         );

         console.log(this._loginResponseModel.IsAuthenticated);
        return this._loginResponseModel.IsAuthenticated;
    }

The first log is the actual value (true or false) but the other login is undefined.

Outside the subscribe, value is being lost.
Any help pleasE?

Lucas Lacroix

未读,
2016年6月30日 06:48:422016/6/30
收件人 AngularJS

The callback occurs after your second console.log call. Observables are asynchronous.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Simon Azzopardi

未读,
2016年6月30日 06:50:592016/6/30
收件人 AngularJS
Yes I know that, so what I am doing wrong? how can I get that value to return it back?

thanks for your reply and you help.

Lucas Lacroix

未读,
2016年6月30日 06:54:192016/6/30
收件人 AngularJS

It all depends on what is calling your function. You could, for example, just return the observable. But that would require that the caller supports observavles. Angular 2's "CanActivatec might - I haven't looked into it.

Simon Azzopardi

未读,
2016年6月30日 06:56:152016/6/30
收件人 AngularJS
Another Service is calling that method:
 
import { Injectable }             from '@angular/core';
import { CanActivate,
         Router,
         ActivatedRouteSnapshot,
         RouterStateSnapshot }    from '@angular/router';
import { AuthService }            from './auth.service';

import { LoginResponseModel } from './Login/loginResponseModel';

@Injectable() 
export class AuthGuard implements CanActivate{

    constructor(private _authService: AuthService, private _router: Router) {}

    canActivate(

         next:  ActivatedRouteSnapshot,
        state: RouterStateSnapshot

    ){
         
        if(this._authService.subscribeIsUserLoggedIn()){
            return true;
        }else{
            this._router.navigate(['/login']);
            return false;
        }
    }

Lucas Lacroix

未读,
2016年6月30日 07:07:042016/6/30
收件人 AngularJS

A simple Google search turned up a solution which seems to match exactly what you want to do:
https://github.com/angular/angular/issues/4112


--

Simon Azzopardi

未读,
2016年6月30日 07:13:172016/6/30
收件人 AngularJS
For which one you referring to as a solution?

Lucas Lacroix

未读,
2016年6月30日 07:14:202016/6/30
收件人 AngularJS

Redirecting to another component when login is required.

Simon Azzopardi

未读,
2016年6月30日 07:19:162016/6/30
收件人 AngularJS
of DrClick?

Simon Azzopardi

未读,
2016年6月30日 07:31:172016/6/30
收件人 AngularJS
Hi Lucas,

I don't see an actual solution for my problem.

Simon Azzopardi

未读,
2016年6月30日 07:53:312016/6/30
收件人 AngularJS
This is the solution:

import { Injectable }             from '@angular/core';
import { CanActivate,
         Router,
         ActivatedRouteSnapshot,
         RouterStateSnapshot }    from '@angular/router';
import { AuthService }            from './auth.service';

import {Observable} from 'rxjs/Observable';


@Injectable() 
export class AuthGuard {

    constructor(private _authService: AuthService, private _router: Router) {}

    canActivate(): Observable<boolean>{
        return this._authService.checkIfUserIsLoggedIn()
        .map((result) => {
                console.log(result["_body"])
if (result["_body"] == "true") {
return true;
} else {
this._router.navigate(['/login']);
return false;
}
});  
    }

Lucas Lacroix

未读,
2016年6月30日 07:54:552016/6/30
收件人 AngularJS
Take a look at the first plunkr. 
回复全部
回复作者
转发
0 个新帖子