AWS amplify + cognito 用户池:登录后无需第二次调用就无法获得经过身份验证的用户
AWS amplify + cognito user pool : not able to get authenticated user after login without second call
我正在使用 vue js 并通过放大连接到 aws 用户池,使用
Auth.federatedSignIn();
用户从 aws amplify hosted ui 成功登录后,他们将被重定向回我的主页(下方 App.vue)。
我尝试在页面创建时获取经过身份验证的用户,但它抛出 "not authenticated"。
我尝试再次调用该函数(或刷新页面),可以检索到用户。
我也尝试使用 setTimeout 来添加延迟,但似乎 vue 没有更新页面,我无法在屏幕上看到我的 currentUser。
回调后立即获得经过身份验证的用户的最佳做法是什么?
App.vue
<template>
<div id="app">
<v-btn @click="getAgain()">Get again</v-btn> #just a button to call again, and it able to get the user
<h1 v-if="currentUser">user id : {{currentUser}}</h1>
</div>
</template>
<script>
import { Auth } from "aws-amplify";
export default {
name: 'app',
components: {
},
data() {
return {
currentUser: null
}
},
created() {
Auth.currentAuthenticatedUser()
.then(user => {
this.currentUser = user;
console.log(user);
})
.catch(e => {});
},
methods: {
getAgain() {
Auth.currentAuthenticatedUser()
.then(user => {
this.currentUser = user;
})
.catch(e => {});
}
}
}
</script>
您可以监听登录事件并在事件触发后检索用户
Hub.listen('auth', ({ payload: { event, data } }) => {
if (event === 'signIn') {
Auth.currentAuthenticatedUser()
.then(user => {
this.currentUser = user;
console.log(user);
})
.catch(e => {console.log(e)});
}
});
我正在使用 vue js 并通过放大连接到 aws 用户池,使用
Auth.federatedSignIn();
用户从 aws amplify hosted ui 成功登录后,他们将被重定向回我的主页(下方 App.vue)。
我尝试在页面创建时获取经过身份验证的用户,但它抛出 "not authenticated"。
我尝试再次调用该函数(或刷新页面),可以检索到用户。
我也尝试使用 setTimeout 来添加延迟,但似乎 vue 没有更新页面,我无法在屏幕上看到我的 currentUser。
回调后立即获得经过身份验证的用户的最佳做法是什么?
App.vue
<template>
<div id="app">
<v-btn @click="getAgain()">Get again</v-btn> #just a button to call again, and it able to get the user
<h1 v-if="currentUser">user id : {{currentUser}}</h1>
</div>
</template>
<script>
import { Auth } from "aws-amplify";
export default {
name: 'app',
components: {
},
data() {
return {
currentUser: null
}
},
created() {
Auth.currentAuthenticatedUser()
.then(user => {
this.currentUser = user;
console.log(user);
})
.catch(e => {});
},
methods: {
getAgain() {
Auth.currentAuthenticatedUser()
.then(user => {
this.currentUser = user;
})
.catch(e => {});
}
}
}
</script>
您可以监听登录事件并在事件触发后检索用户
Hub.listen('auth', ({ payload: { event, data } }) => {
if (event === 'signIn') {
Auth.currentAuthenticatedUser()
.then(user => {
this.currentUser = user;
console.log(user);
})
.catch(e => {console.log(e)});
}
});