Created
December 1, 2019 08:23
-
-
Save sasaken555/59a3b5c759db89284000086e3bd86dbf to your computer and use it in GitHub Desktop.
IBM Cloud AppID service in Angular
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 {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