Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The requested path contains undefined segment at index 0

authentication.service.ts in that my login service is define like this

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/Rx';
import 'rxjs/add/operator/map'
import { AuthHttp } from 'angular2-jwt';
import { Router } from '@angular/router';
   @Injectable()
   export class AuthenticationService {
token: string;
redirectUrl: any;
constructor(public router: Router, private http: Http, private authHttp: AuthHttp) {
    //set token if saved in local storage
    if (localStorage.getItem('authToken') != null) {
        try {
            // var currentUser = 
          JSON.parse(localStorage.getItem('currentUser'));
             this.token = localStorage.getItem('authToken');
             // this.token = currentUser && currentUser.token;
         } catch (e) {
             localStorage.removeItem("authToken");
         }
    }
 }

login(username: string, password: string) {
    return this.http.post('http://localhost:3002/api/authenticate',({ username: username, password: password }))
        .map(res => {
            console.log(res);
            var authToken = res.json().token;
            localStorage.setItem('authToken', authToken);
            let redirectUrlTemp: any;
            redirectUrlTemp = this.redirectUrl;
            this.redirectUrl = null;
            if (!redirectUrlTemp) {
                    console.log(redirectUrlTemp);
                redirectUrlTemp = ['/login'];
            }


            this.router.navigate(redirectUrlTemp);


        },
        err => {
            //alert(error.text());
            console.log(err.text());
        });
}
logout(){
    // clear token remove user from local storage to log user out
    this.token = null;
    localStorage.removeItem('authToken');
 }
}

when i try to post data using postman it works properly but from angular 2 it not allowing to redirect from next page

Login.component.ts

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AuthenticationService } from "app/services";
import { LoginRequest } from "app/models/models";
import { AlertService } from 'app/services';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { Http, Response } from '@angular/http';
   @Component({
     moduleId: module.id,
     selector: 'login',
     templateUrl: 'login.component.html'
    })
    export class LoginComponent implements OnInit {

      model: any = {};
      getLogin: LoginRequest[] = [];
      loading = false;
      error = '';
      returnUrl: string;
   constructor(
    private route: ActivatedRoute,
    private router: Router,
    private http: Http,
    private alertService: AlertService,
    private authenticationService: AuthenticationService) { }

login() {
    this.loading = true;
    this.authenticationService.login(this.model.username, this.model.password).subscribe(data => {
                this.router.navigate([this.returnUrl]);
            },
            error => {
                this.alertService.error(error);
                this.loading = false;
            });

      //let redirect = this.authenticationService.redirectUrl ? this.authenticationService.redirectUrl : '/allTask';
 }
}

auth.guadr.ts

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthenticationService } from "app/services";


 @Injectable()
  export class AuthGuard implements CanActivate {
  constructor(private router: Router, private authService: AuthenticationService) { }

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (localStorage.getItem('authToken')) {
        // logged in so return true

        return true;
    }
    this.authService.redirectUrl =[state.url];
    this.router.navigate(['/login']);
    return false;
   }
}
like image 911
Raghavendra Ajegaonkar Avatar asked May 17 '17 12:05

Raghavendra Ajegaonkar


1 Answers

This error means that navigate array parameter with index 0 is undefined. In your case you didn't set this.returnUrl in Login.component.ts this.router.navigate([this.returnUrl]);

like image 107
kostia24 Avatar answered Nov 15 '22 17:11

kostia24