생성자에서이 삭제
ngOnInit() {
this.auth.login()
}
을
auth.component.ts :
이 시도 로그인 기능이 호출 될 때 팝업.
그래서 원하는 위치에서 다시 사용하려면 인증 서비스를 호출하려는 구성 요소에 인증 서비스를 주입해야합니다. 그런 다음 메서드를 호출하면됩니다. 예를 들어, 여기에 내 시작 구성 요소에 대한 html이 있습니다. 당신은 로그인의 버튼의 클릭 이벤트가 "submitLogin()"구성 요소의 방법 (시작 구성 요소)에 결합되는 것을 볼 수 있습니다
<div class="splash-back" *ngIf="!authService.authenticated()">
<div id="splash">
<div id="logo"><span class="silver">GCO</span>TeamKeeper
<p class="silver tagline">The other teams could make trouble for us if they win.</p>
<p class="silver attribution">~ Yogi Berra</p></div>
<div class="call">
<br>
<button class="btn-sign-in" (click) = "submitLogin()">Sign up or Log in</button>
</div>
<!--<mtm-authentication></mtm-authentication>-->
</div>
</div>
그리고 여기 시작 구성 요소 코드 (의 주입을주의 생성자의 인증 서비스) :
import {Injectable} from "@angular/core";
import { tkConfig } from './user-authentication.config';
import {Router} from "@angular/router";
import {tokenNotExpired} from "angular2-jwt";
let Auth0Lock = require('auth0-lock').default;
@Injectable()
export class UserAuthenticationService {
// Configure Auth0
userProfile: Object;
lock = new Auth0Lock (tkConfig.clientID, tkConfig.domain, {
avatar: null,
theme: {
primaryColor: "#69BE28",
foregroundColor: "#000000"
},
languageDictionary: {
title: "GCO TeamKeeper"
}
}
);
constructor(
private router: Router) {
this.userProfile = JSON.parse(localStorage.getItem('profile'));
// Add callback for lock `authenticated` event
this.lock.on('authenticated', (authResult) => {
localStorage.setItem('id_token', authResult.idToken);
this.lock.getProfile(authResult.idToken, (error, profile) => {
if (error) {
alert(error);
return;
}
profile.user_metadata = profile.user_metadata || {};
localStorage.setItem('profile', JSON.stringify(profile));
this.userProfile = profile;
this.router.navigate(['/organization']);
});
})
}
public login() {
// Call the show method to display the widget.
this.lock.show();
};
public authenticated() {
// Check if there's an unexpired JWT
// It searches for an item in localStorage with key == 'id_token'
return tokenNotExpired();
};
public logout() {
// Remove token from localStorage
localStorage.removeItem('id_token');
localStorage.removeItem('profile');
this.userProfile = undefined;
this.router.navigate(['/start']);
};
}
덕분에, 완벽하게 일 :
그리고이 예를 완료하게 여기 내 인증 서비스 코드입니다! – Sam