設定
我正在開發一個使用 AWS Amplify 作為后端的離子應用程式。我們希望通過兩種身份驗證型別“AMAZON_COGNITO_USER_POOLS”和“API_KEY”來使用以下服務:
- 放大 API 以與 API Gateway 互動
- 帶有代碼生成的 GraphQL API
對于身份驗證,我們使用帶有 Cognito 托管 UI的Ionic Auth Connect 。按照檔案,我們能夠進行登錄和注冊,并能夠檢索 Id、Refresh 和 Access 令牌。
問題
此設定的問題在于,amplify 無法識別登錄用戶,并且在嘗試與 Amplify API 和 GraphQL 互動時,我們沒有收到當前用戶錯誤。
檢查放大和身份驗證連接檔案我發現沒有明確的方法來手動設定當前用戶。
我嘗試使用Amplify.configure手動設定令牌并使用身份驗證攔截器,但它不起作用,因為 Amplify 在 SDK 中有一些阻止請求的驗證邏輯。
有沒有辦法為放大設定用戶會話而無需手動構建 API 和 GraphQL 呼叫?
uj5u.com熱心網友回復:
假設您正在為您的身份驗證類使用以下設定:來自官方檔案
import { Injectable, NgZone } from '@angular/core';
import { IonicAuth } from '@ionic-enterprise/auth';
import { Platform } from '@ionic/angular';
import { BehaviorSubject, Observable } from 'rxjs';
import { nativeIonicAuthOptions, webIonicAuthOptions } from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService extends IonicAuth {
private authenticationChange: BehaviorSubject<boolean> = new BehaviorSubject(false);
public authenticationChange$: Observable<boolean>;
constructor(platform: Platform, private ngZone: NgZone) {
super(platform.is('hybrid') ? nativeIonicAuthOptions : webIonicAuthOptions);
this.authenticationChange$ = this.authenticationChange.asObservable();
this.isAuthenticated().then((authenticated) => { this.onAuthChange(authenticated); });
}
public async onLoginSuccess(): Promise<void> {
this.onAuthChange(true);
}
public async onLogout(): Promise<void> {
this.onAuthChange(false);
}
private async onAuthChange(isAuthenticated: boolean): Promise<void> {
this.ngZone.run(() => {
this.authenticationChange.next(isAuthenticated);
});
}
}
您可以使用amazon-cognito-identity-js并手動設定CognitoUserSession如下:
安裝amazon-cognito-identity-js
創建一個新方法來處理設定用戶會話:
private async setAmplifyUser(): Promise<void> {
const idToken = await this.getIdToken();
const accessToken = await this.getAccessToken();
const authResponse = await this.getAuthResponse();
const refreshToken = await this.getRefreshToken();
const userPool = new CognitoUserPool({
UserPoolId: `YOUR_USER_POOL_ID `,
ClientId: `YOUR_APP_CLIENT_ID `,
});
const cognitoIdToken = new CognitoIdToken({
IdToken: authResponse.id_token,
});
const cognitoAccessToken = new CognitoAccessToken({
AccessToken: accessToken,
});
const cognitoRefreshToken = new CognitoRefreshToken({
RefreshToken: refreshToken,
});
const username = idToken['cognito:username'];
const user = new CognitoUser({
Username: username,
Pool: userPool,
});
user.setSignInUserSession(
new CognitoUserSession({
AccessToken: cognitoAccessToken,
IdToken: cognitoIdToken,
RefreshToken: cognitoRefreshToken,
})
);
}
要使 Cognito 會話與 auth connect 的會話保持同步,您需要在setAmplifyUser內部呼叫onAuthChange
要確認您的會話已設定,您可以嘗試呼叫這些方法:
Auth.currentAuthenticatedUser().then((res) => {
console.log('currentAuthenticatedUser', res);
});
Auth.currentSession().then((res) => {
console.log('currentSession', res);
});
關于使用多種身份驗證模式的附加說明:在 AppModule 的建構式中,您可以根據身份驗證狀態訂閱onAuthChange和切換aws_appsync_authenticationType 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/504171.html
標籤:有角度的 离子框架 亚马逊认知 aws-放大 离子企业认证
上一篇:如何作用于離子滑梯上的單個專案?
下一篇:注冊電容器推送通知
