如何解决在安全页面上刷新页面后,使用Laravel护照的Nuxt js重定向到登录页面
我正在使用laravel护照进行NUXT身份验证。我正在关注此文档:
https://auth.nuxtjs.org/providers/laravel-passport.html#usage
我可以在我的nuxt应用程序上成功登录。登录后重定向到仪表板,其中仪表板具有middleware:'auth'
。问题是。刷新仪表板时,它将重定向回登录页面。在js控制台上,$auth.isAuthenticated
返回true,但在SSR上返回false。我想问题出在middleware:auth
上,我不确定。我已经在Google上搜索了,但没有解决方案。我已经尝试过了:
https://github.com/nuxt-community/auth-module/issues/478
我的代码如下:
Implementing Laravel 7 Passport authentification with Nuxt frontend
有人知道解决方案吗?
解决方法
http_only
cookie不适用于SSR。因此,您需要制作一个nuxt插件。然后您应该检查Cookie中是否不存在access_token
和user
是否存在(当用户登录时,您应该使用用户信息或用户ID制作Cookie,我个人将使用Vuex vuex-persistedstate)。并且,如果用户存在于Cookie中,而access_token
不存在,则应刷新令牌。
plugins/refresh-token.js
import clientCookies from 'js-cookie';
/**
* For refreshing the token on page reload.
*
* @param {Object} context.store
* @return {Void}
*/
export default function ({store}) {
window.onNuxtReady(() => {
let token = clientCookies.get('x-access-token');
let user = store.getters['user/currentUser'];
if (user && ! token) {
store.dispatch('auth/refreshToken')
.catch(errors => {
store.dispatch('auth/signUserOut');
});
}
});
}
有关更多信息,请阅读此post
,我最近遇到了这个问题,虽然这个答案可能对操作员没有帮助,但我相信将来可能对其他人有用。
这是我们下面所做的,在您的api路由下(可在routes / api.php中找到),添加访问laravel应用程序api的nuxt js域,使用下面的代码-确保您格式化为自己使用情况
$landingRoutes = function () {
//your routes
};
Route::group(['domain' => config('app.DASHBOARD_DOMAIN1')],$landingRoutes);
Route::group(['domain' => config('app._DASHBOARD_DOMAIN2')],$landingRoutes);
我也在自己的博客上{@ 3}写道
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。