Created
December 1, 2019 08:34
-
-
Save sasaken555/eb0e9be9196903d48bff66afa737030c to your computer and use it in GitHub Desktop.
Using IBM Cloud AppID in Angular Components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button | |
mat-raised-button | |
disabled="{{!isAuthenticated}}" | |
(click)="getUserInfo()" | |
> | |
<mat-icon>account_circle</mat-icon> Profile | |
</button> | |
<button | |
mat-raised-button | |
color="primary" | |
*ngIf="!isAuthenticated" | |
(click)="onLoginPressed()" | |
> | |
<mat-icon>lock_open</mat-icon> Login with AppID | |
</button> | |
<app-articles *ngIf="isAuthenticated"></app-articles> | |
<p *ngIf="!isAuthenticated">ログインしてタイムラインを見よう!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnInit} from '@angular/core'; | |
import {AuthService} from '../../services/auth.service'; | |
@Component({ | |
selector: 'app-article-timeline', | |
templateUrl: './article-timeline.component.html', | |
styleUrls: ['./article-timeline.component.css'] | |
}) | |
export class ArticleTimelineComponent implements OnInit { | |
isAuthenticated: boolean; | |
constructor(private authService: AuthService) { | |
} | |
ngOnInit() { | |
this.isAuthenticated = this.authService.isAuthenticated; | |
} | |
async onLoginPressed() { | |
await this.authService.signIn() | |
.then(() => { | |
this.isAuthenticated = this.authService.isAuthenticated; | |
alert('Login succeeded!'); | |
}); | |
} | |
async getUserInfo() { | |
const userInfo = await this.authService.getUserInfo(); | |
alert('Name: ' + userInfo.name + '\nEmail: ' + userInfo.email); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment