2016-12-29 2 views
0

인증을 위해 Auth0을 사용하고 데이터베이스에 AngularFire를 사용하여 Angular2 앱을 구축하고 있습니다. 내 생성자에서 AngularFire af 인스턴스를 전달했지만 콜백 이벤트에서 액세스 할 수 없습니다. AngularFire 인스턴스에 액세스 할 수있는 또 다른 방법이 있습니까?Angular2 - 예외 : 정의되지 않은 'af'속성을 읽을 수 없습니다.

// app/auth.service.ts 

import { Injectable } from '@angular/core'; 
import { tokenNotExpired } from 'angular2-jwt'; 
import { AngularFire, AuthMethods, AuthProviders } from 'angularfire2'; 


// Avoid name not found warnings 
declare var Auth0Lock: any; 
declare var Auth0: any; 

@Injectable() 
export class Auth { 
// Configure Auth0 
    lock = new Auth0Lock('AUTH0_CLIENT_ID', 'AUTH0_DOMAIN', {}); 


    constructor(private af: AngularFire) { 
    // Add callback for lock `authenticated` event 

    this.lock.on("authenticated", (authResult) => { 
     this.lock.getProfile(authResult.idToken, function(error:any, profile:any){ 
     if(error){ 
      throw new Error(error); 
     } 

     localStorage.setItem('id_token', authResult.idToken); 
     localStorage.setItem('profile', JSON.stringify(profile)); 

     var options = { 
     id_token : authResult.idToken, 
     api : 'firebase', 
     scope : 'openid name email displayName', 
     target: 'AUTH0_CLIENT_ID' 
     }; 


     var auth0 = new Auth0({domain:'AUTH0_DOMAIN', clientID:'AUTH0_CLIENT_ID' }); 
     auth0.getDelegationToken(options, function(err, result){ 

      if(!err){ 


      this.af.auth.login(result.id_token, { 
       provider: AuthProviders.Custom, 
       method: AuthMethods.CustomToken 
      }); 
      console.log(result); 
      } 
     }); 
     }); 
    }); 
    } 
} 

답변

7

귀하의 this 콜백 기능이 해당 기능을 가리키기 때문입니다.

  1. 클래스의이 절감 : 당신은 세 가지 옵션이 let that = this 및 콜백 내부 that.af를 사용합니다.
  2. 사용 람다, 그것은 this을 변경하지 않기 때문에 : 당신은 기능과

은 또한, 나는 람다 접근 방식을 추천 클래스에이 바인딩 .bindTo(this)를 호출 할 수 있습니다 (error, profile) => {...}

  • .

  • +1

    감사합니다. 문제가 무엇인지 알아 내려고 노력했습니다. – iamarnold

    관련 문제