假設
我正在實作登錄功能nuxt/auth
。我想實作一個訪客登錄功能,但是當我按下按鈕以訪客身份登錄時,它沒有進入/search
,它回到了user/login
。我希望它不要進入登錄頁面。指定頁面顯示片刻,但user/login
立即顯示。
我們想要達到的目標
我想在按下訪客登錄按鈕后被重定向到指定頁面。
代碼
帶有訪客登錄按鈕的頁面
<script>
import * as url from '@/store/constants/url'
export default {
data ({ $config: { APP_NAME } }) {
return {
APP_NAME,
}
},
methods: {
guest () {
?
?
.then((response) => {
?
?
this.$auth.loginWith('local', {data: {
email: response.email,
password: "xxxxxx"
}})
})
this.$router.replace('/search') // I get back to the login page without going to /search.
}
}
}
</script>
nuxt.config.js
auth: {
token: {
global: true
},
redirect: {
login: '/user/login',
logout: '/user/login',
callback: false,
home: '/'
},
strategies: {
local: {
endpoints: {
login: { url: '/api/v1/auth/sign_in', method: 'post', propertyName: 'token' },
logout: { url: '/api/v1/auth/sign_out', method: 'delete' },
user: false
}
}
}
},
uj5u.com熱心網友回復:
正如這里和我之前關于nuxt/auth的許多答案所解釋的,你應該有這樣的東西。
<script>
export default {
methods: {
async logMeIn() {
const positiveResponseFromBackend = await this.$auth.loginWith('local', {
data: {
email: 'fancy email',
password: 'cool password',
},
})
if (positiveResponseFromBackend) {
await this.$auth.setUser({
email: 'fancy email', // can of course be dynamic or fetched from your backend as a response
password: 'cool password', // same here
})
}
},
},
}
</script>
這將通過 auth 模塊成功登錄,因此您將自動重定向到物件home
中的值redirects
(不需要 a router.push
)。
nuxt.config.js
auth: {
redirect: {
home: '/search'
},
},
uj5u.com熱心網友回復:
這可能是因為this.$auth.loginWith
是異步的,這意味著this.$router.replace('/search')
它將在 auth login 函式回傳之前執行。
你可以試試這個:
.then((response) => {
return this.$auth.loginWith('local', {data: {
email: response.email,
password: "xxxxxx"
}})
})
.then() => { this.$router.replace('/search') }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/468567.html
標籤:javascript Vue.js 验证 nuxt.js nuxt-auth
上一篇:Django中間件登錄檢查