Need some help regarding Observables in a service

29 צפיות
מעבר להודעה הראשונה שלא נקראה

Simon Azzopardi

לא נקראה,
30 ביוני 2016, 6:44:5030.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 6:48:4230.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 6:50:5930.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 6:54:1930.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 6:56:1530.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 7:07:0430.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 7:13:1730.6.2016
עד AngularJS
For which one you referring to as a solution?

Lucas Lacroix

לא נקראה,
30 ביוני 2016, 7:14:2030.6.2016
עד AngularJS

Redirecting to another component when login is required.

Simon Azzopardi

לא נקראה,
30 ביוני 2016, 7:19:1630.6.2016
עד AngularJS
of DrClick?

Simon Azzopardi

לא נקראה,
30 ביוני 2016, 7:31:1730.6.2016
עד AngularJS
Hi Lucas,

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

Simon Azzopardi

לא נקראה,
30 ביוני 2016, 7:53:3130.6.2016
עד 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

לא נקראה,
30 ביוני 2016, 7:54:5530.6.2016
עד AngularJS
Take a look at the first plunkr. 
השב לכולם
השב למחבר
העבר לנמענים
0 הודעות חדשות