![](/img/trans.png)
[英]how to handle axios error unifiedly in asyncData() in SSR nuxt.js
[英]Getting "not authenticated" Error in Nuxt.js middleware with SSR and Amplify
我想創建一個 Nuxt.js 中間件來檢查用戶是否經過身份驗證。 在應用程序內部導航時效果很好。 但是當我刷新頁面時,我總是得到:
[ERROR] 43:48.846 foo - SERVER { 15:43:48 error: 'not authenticated' } ERROR [ERROR] 43:48.844 AuthError - 15:43:48 Error: Amplify has not been configured correctly. The configuration object is missing required auth properties. Did you run `amplify push` after adding auth via `amplify add auth`? See https://aws-amplify.github.io/docs/js/authentication#amplify-project-setup for more information
AWS Amplify 在這里有一篇關於 SSR 支持的文章: AWS Amplify的 SSR 支持在那里您可以找到啟用 SSR 和檢查用戶身份驗證的代碼
在 Nuxt.js 文檔中,您可以找到:
在通用模式下,中間件將在服務器端(對 Nuxt 應用程序的第一次請求,例如直接訪問應用程序或刷新頁面時)調用一次,在導航到更多路由時在客戶端調用一次。
好的,所以我在我的放大插件中啟用了 SSR 並調整了中間件:
if (process.server) {
const { Auth } = withSSRContext(context.req)
try {
await Auth.currentAuthenticatedUser()
} catch (error) {
return context.redirect(302, '/auth/signin')
}
}
當我刷新頁面時,代碼被執行,但我總是收到“未通過身份驗證”錯誤和“未正確配置 Amplify”。 另一方面,在客戶端(登錄、注冊等)一切正常。
有什么想法嗎? 我找不到解決辦法...
親切的問候
我遇到了同樣的問題,結果我不得不調整我的配置。 我在 NextJS 但它看起來像同樣的問題。 改變了我在組件中配置 Amplify 的方式:
Amplify.configure({ ...config })
至
Amplify.configure({ ...config, ssr: true })
如果不設置ssr
屬性,我的服務器端代碼無法獲取當前經過身份驗證的用戶,我得到的只是服務器日志中的“用戶未通過身份驗證”。
我正在使用 aws-amplify@3.3.4
只需嘗試配置身份驗證。 無需提供ssr: true
。 至少那對我有用..
if (process.server) {
const { Auth } = withSSRContext(context.req)
Auth.configure({...config})
try {
await Auth.currentAuthenticatedUser()
} catch (error) {
return context.redirect(302, '/auth/signin')
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.