Skip to content

Instantly share code, notes, and snippets.

@sasaken555
Created December 1, 2019 08:23
Show Gist options
  • Save sasaken555/59a3b5c759db89284000086e3bd86dbf to your computer and use it in GitHub Desktop.
Save sasaken555/59a3b5c759db89284000086e3bd86dbf to your computer and use it in GitHub Desktop.
IBM Cloud AppID service in Angular
import {Injectable} from '@angular/core';
import AppID from 'ibmcloud-appid-js';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private appId;
private accessToken: string;
constructor() {
this.appId = new AppID();
this.initClient().then(() => console.log('initialized AuthService.'));
}
private async initClient() {
// AppIDの資格情報に記載された値をそれぞれ入れる
await this.appId.init({
clientId: 'YOUR_CLIENT_ID',
discoveryEndpoint: 'YOUR_DISCOVERY_ENDPOINT'
});
}
get isAuthenticated(): boolean {
return !!this.accessToken;
}
async signIn() {
const { accessToken } = await this.appId.signin();
this.accessToken = accessToken;
}
async getUserInfo() {
return await this.appId.getUserInfo(this.accessToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment