import { Component } from '@angular/core'; import { Authing } from '@authing/browser'; import type { UserInfo, LoginState, } from '@authing/browser/dist/types/global'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { title = 'website1'; loginState: LoginState | null = null; userInfo: UserInfo | null = null; private sdk = new Authing({ // 很重要,请仔细填写! // 如果应用开启 SSO,这儿就要写单点登录的“应用面板地址”;否则填写应用的“认证地址”。 domain: 'enccibbmkpbhiman.pre.authing.cn', // 应用 ID appId: '62c3b5bd8950b50610ecbef1', // 登录回调地址,需要在控制台『应用配置 - 登录回调 URL』中指定 redirectUri: 'https://localhost:8000', }); ngOnInit() { // 校验当前 url 是否是登录回调地址 if (this.sdk.isRedirectCallback()) { console.log('redirect'); /** * 以跳转方式打开 Authing 托管的登录页,认证成功后,需要配合 handleRedirectCallback, * 在回调端点处理 Authing 发送的授权码或 token,获取用户登录态 */ this.sdk.handleRedirectCallback().then((res) => { this.loginState = res; window.location.replace('/'); }); } else { this.getLoginState(); } } /** * 以弹窗方式打开 Authing 托管的登录页 */ async loginWithPopup() { const res = await this.sdk.loginWithPopup(); this.loginState = res; } /** * 以跳转方式打开 Authing 托管的登录页 */ loginWithRedirect() { this.sdk.loginWithRedirect(); } /** * 获取用户的登录状态 */ async getLoginState() { const state = await this.sdk.getLoginState(); this.loginState = state; } /** * 用 Access Token 获取用户身份信息 */ async getUserInfo() { if (!this.loginState) { alert('用户未登录'); return; } const userInfo = await this.sdk.getUserInfo({ accessToken: this.loginState.accessToken, }); this.userInfo = userInfo; } /** * 登出 */ logoutWithRedirect() { this.sdk.logoutWithRedirect(); } }