Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Luchando para conectar la solicitud asíncrona del servicio de autenticación con ActivationGuard

Tengo problemas para conectar ActivationGuard con un UserAuthenticationService , que regresa del servidor si el usuario dado está conectado o no. El principal problema aquí, en mi opinión, es el hecho de que proviene de una solicitud asincrónica.

Tengo algo de código, hecho con la ayuda de un amable usuario en Stack. Está hecho con observables.

Pero el problema aquí es que recibo el siguiente error al ingresar los componentes protegidos:

zone.js:388 Unhandled Promise rejection: Cannot read property 'do' of undefined;

Mis archivos:

ActivationGuard.ts

 import {Injectable} from '@angular/core'; import {Router, CanActivate, RouterStateSnapshot, ActivatedRouteSnapshot} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {UserAuthenticationService} from './UserAuthenticationService'; @Injectable() export class ActivationGuard implements CanActivate { constructor(private router: Router, private userService: UserAuthenticationService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { return this.userService.isUserAuthenticated .do(success => { if (!success) { this.router.navigate(['/']); } }); } }

UserAuthenticationService.ts

 import {Injectable, OnInit} from '@angular/core'; import {Http} from '@angular/http'; import {Observable} from "rxjs/Observable"; @Injectable() export class UserAuthenticationService implements OnInit { public isUserAuthenticated: Observable<boolean>; username: string = 'admin'; constructor(private http: Http) {} ngOnInit(){ this.isUserAuthenticated = this.http.get(`http://localhost/api/auth/isLogged/${this.username}`) .map(res => res.json()) .share(); } }

Esperando cualquier tipo de ayuda. Si tiene alguna pregunta, hágamelo saber. Gracias.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!