Angular: TypeError: Невозможно прочитать свойство 'then' из неопределенного - PullRequest
0 голосов
/ 20 мая 2018

Я использую JHipster Auth-guard.Я пытаюсь вызвать canActivate для пользователя, у которого нет разрешения для маршрута.

Я не могу по жизни, чтобы увидеть, где это идет не так.Консоль говорит, что .then () не определен в коде ниже.Я попытался войти в консоль главного пользователя, и он не является неопределенным.Товарищи стекировщики, кто-нибудь может увидеть, что я делаю не так?

checkLogin(authorities: string[], url: string): Promise<boolean> {
    var principal = this.principal;

    return Promise.resolve(principal.identity().then((account) => {



   ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined
    at AuthGuard.checkLogin (auth-guard.ts:27)
    at AuthGuard.canActivate (auth-guard.ts:21)

Мой код:

app.module

    const routes: Routes = [
    {
        path: '**',
        component: DashboardComponent,
        canActivate: [AuthGuard],
        resolve: {
            data: DashboardService
        }
    }
];

@NgModule({
    declarations: [
        DashboardComponent
    ],
    imports: [
        RouterModule.forChild(routes),
        HttpClientModule,
        HttpModule,
        CdkTableModule,
        MatButtonModule,
        MatDividerModule,
        MatFormFieldModule,
        MatIconModule,
        MatMenuModule,
        MatSelectModule,
        MatSidenavModule,
        MatTableModule,
        MatTabsModule,

        NgxChartsModule,
    ],
    providers: [
        ProjectDashboardService, AuthGuard, Principal, StateStorageService
    ]
})

Auth-guard.ts

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

import { Principal } from './principal.service';
import { StateStorageService } from './state-storage.service';

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(private router: Router,
        private principal: Principal,
        private stateStorageService: StateStorageService) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Promise<boolean> {

        const authorities = route.data['authorities'];
        // We need to call the checkLogin / and so the principal.identity() function, to ensure,
        // that the client has a principal too, if they already logged in by the server.
        // This could happen on a page refresh.
        return this.checkLogin(authorities, state.url);
    }

    checkLogin(authorities: string[], url: string): Promise<boolean> {
        var principal = this.principal;

        return Promise.resolve(principal.identity().then((account) => {

            if (!authorities || authorities.length === 0) {
                return true;
            }

            if (account) {
                return principal.hasAnyAuthority(authorities).then((response) => {
                    if (response) {
                        return true;
                    }
                    return false;
                });
            }

            this.stateStorageService.storeUrl(url);
            //this.router.navigate(['accessdenied']);
            window.location.href = "http://stackoverflow.com";
            return false;
        }));
    }
}

основной сервис.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';


@Injectable()
export class Principal {
    private userIdentity: any;
    private authenticated = false;
    private authenticationState = new Subject<any>();

    constructor(

    ) { }

    authenticate(identity) {
        this.userIdentity = identity;
        this.authenticated = identity !== null;
        this.authenticationState.next(this.userIdentity);
    }

    hasAnyAuthority(authorities: string[]): Promise<boolean> {
        return Promise.resolve(this.hasAnyAuthorityDirect(authorities));
    }

    hasAnyAuthorityDirect(authorities: string[]): boolean {
        if (!this.authenticated || !this.userIdentity || !this.userIdentity.authorities) {
            return false;
        }

        for (let i = 0; i < authorities.length; i++) {
            if (this.userIdentity.authorities.includes(authorities[i])) {
                return true;
            }
        }

        return false;
    }

    hasAuthority(authority: string): Promise<boolean> {
        if (!this.authenticated) {
            return Promise.resolve(false);
        }

        return this.identity().then((id) => {
            return Promise.resolve(id.authorities && id.authorities.includes(authority));
        }, () => {
            return Promise.resolve(false);
        });
    }

    identity(force?: boolean): Promise<any> {
        if (force === true) {
            this.userIdentity = undefined;
        }

        // check and see if we have retrieved the userIdentity data from the server.
        // if we have, reuse it by immediately resolving
        if (this.userIdentity) {
            return Promise.resolve(this.userIdentity);
        }

    }

    isAuthenticated(): boolean {
        return this.authenticated;
    }

    isIdentityResolved(): boolean {
        return this.userIdentity !== undefined;
    }

    getAuthenticationState(): Observable<any> {
        return this.authenticationState.asObservable();
    }

    getImageUrl(): String {
        return this.isIdentityResolved() ? this.userIdentity.imageUrl : null;
    }
}

1 Ответ

0 голосов
/ 20 мая 2018

Спасибо за вашу помощь @ user184994.Это сработало, когда я вернул false.Решение. Измените возвращаемое значение в

Principal.identity ()

, чтобы оно возвращало значение false, если userIdentity имеет значение false, и удаляет это

if (! органы || полномочия. длина === 0) {вернуть истину;}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...