Skip to content

Instantly share code, notes, and snippets.

@sasaken555
Created December 1, 2019 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasaken555/eb0e9be9196903d48bff66afa737030c to your computer and use it in GitHub Desktop.
Save sasaken555/eb0e9be9196903d48bff66afa737030c to your computer and use it in GitHub Desktop.
Using IBM Cloud AppID in Angular Components
<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>
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