logout - 如何使用 Amazon Cognito 注销终端节点?
问题描述
我在我的应用程序中使用 AWS Cognito。
在注销时,我正在调用Logout Endpoint。
但是在注销后,我仍然可以使用旧的刷新令牌生成 id-tokens。
这意味着我的注销端点不再工作。我将令牌保存在我的本地存储中,并且在注销时我正在手动清除存储。
我的问题是:如何正确使用 AWS Cognito 的注销机制?
解决方案
我不确定您使用的是哪个框架,但我使用的是 Angular。不幸的是,有多种使用 AWS Cognito 的方法,并且文档不清楚。这是我的身份验证服务的实现(使用 Angular):
- 注 1 - 使用此登录方法 - 一旦您将用户重定向到注销 url - 本地主机会自动刷新并且令牌被删除。
- 注 2 - 您也可以通过调用手动执行此操作:this.userPool.getCurrentUser().signOut()
import { Injectable } from '@angular/core'
import { CognitoUserPool, ICognitoUserPoolData, CognitoUser } from 'amazon-cognito-identity-js'
import { CognitoAuth } from 'amazon-cognito-auth-js'
import { Router } from '@angular/router'
const COGNITO_CONFIGS: ICognitoUserPoolData = {
UserPoolId: '{INSERT YOUR USER POOL ID}',
ClientId: '{INSERT YOUR CLIENT ID}',
}
@Injectable()
export class CognitoService {
userPool: CognitoUserPool
constructor(
private router: Router
) {
this.createAuth()
}
createAuth(): void {
// Configuration for Auth instance.
const authData = {
UserPoolId: COGNITO_CONFIGS.UserPoolId,
ClientId: COGNITO_CONFIGS.ClientId,
RedirectUriSignIn : '{INSERT YOUR COGNITO REDIRECT URI}',
RedirectUriSignOut : '{INSERT YOUR COGNITO SIGNOUT URI}',
AppWebDomain : '{INSERT YOUR AMAZON COGNITO DOMAIN}',
TokenScopesArray: ['email']
}
const auth: CognitoAuth = new CognitoAuth(authData)
// Callbacks, you must declare, but can be empty.
auth.userhandler = {
onSuccess: function(result) {
},
onFailure: function(err) {
}
}
// Provide the url and parseCognitoWebResponse handles parsing it for us.
const curUrl = window.location.href
auth.parseCognitoWebResponse(curUrl)
}
/**
* Check's if the user is authenticated - used by the Guard.
*/
authenticated(): CognitoUser | null {
this.userPool = new CognitoUserPool(COGNITO_CONFIGS)
// behind the scene getCurrentUser looks for the user on the local storage.
return this.userPool.getCurrentUser()
}
logout(): void {
this.router.navigate(['/logout'])
}
}
推荐阅读
- javascript - Gatsby/React: how to create clean fallback for non JS users?
- javascript - 反应:在新标签页中打开链接
- amazon-web-services - 在 GCP 中指向并使用子域
- apache-kafka - Kafka:消费者可以在所有副本同步之前读取消息吗?
- mysql - 在不使用 SQL 字符串的情况下使用 Python 写入 MySQL
- css - 指定宽度时将高度设置为自动有什么意义?
- python - 动画中的networkx图形变化形式
- c# - 按名称查找 C# 线程
- javascript - setInterval() 在代码执行时暂停
- python-3.x - 从网站的 ap 标签内的多个同名 span 元素中抓取 span 文本