2017-04-16 102 views
0

我在流星中添加了一段代碼,它將客戶對象添加到會話中。 我如何使它成爲一個通用代碼,以便我可以爲它正常登錄/ fb登錄/微博等? 我使用angularjs2 /流星流星中的常用登錄代碼

MeteorObservable.call('getCustomer', Meteor.user().emails[0].address).subscribe((cust: Cust) => 
          { 
           Session.set('session_cust',cust); 
           this.router.navigate(['/profile']); 
          }, (error) => 
          { 
           console.log('session cust error'); 
          }); 

回答

0
@Injectable() 
@InjectUser('user') 
export class CustLoginService 
{ 
    constructor(private router: Router) { } 

    handleLogin(): void 
    { 
     if (Meteor.userId()) 
     { 
      MeteorObservable.call('getCustomer', Meteor.user().emails[0].address).subscribe((cust: Cust) => 
      { 
       if (cust) 
       { 
        Session.set('session_cust', cust); 
        this.router.navigate(['/profile']); 
       } 
       else 
       { 
        console.log('Not a customer'); 
        this.router.navigate(['/zone-list']); 
       } 

      }, (error) => 
       { 
        console.log('session cust error'); 
       }); 
     } 
     else 
     { 
      console.log('no meteor user id'); 
     } 
    } 
} 
0

可以使用Tracker.autorun來監視用戶登錄的,獨立的路徑:

Tracker.autorun(() => { 
    if (Meteor.user()){ 
    ... your code - will only run when the user logs in 
    } 
});