保持登录的用户信息 [英] Keeping logged in user information

查看:104
本文介绍了保持登录的用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法用laravel护照登录.我有记号,太好了.但我想保留登录的用户信息,名称,头像等.

I managed to log in with laravel passport. I have the token, great. But I want to keep logged in user information, name, avatar and so on.

我的登录过程获取了oauth令牌.在仪表板组件中,我对用户数据进行了api调用.

My login procedure gets oauth token. In dashboard component I make api call for user data.

我应该将它们保留在全局Vue对象中还是使用Vuex?安全吗?

Should I keep them in global Vue object or use Vuex? Is it safe?

推荐答案

您可能会考虑的某些选项

Some options you might consider

  • 将数据存储在cookie中
  • 使用localStorage
  • 将所有内容保留在根vue实例中
  • 将所有内容保留在包装的vue组件中

我的建议是将auth令牌(成功调用您的后端实际上需要的令牌)存储在cookie中.这样一来,您发送的每个请求都将非常易于访问.

My suggestion would be to store the auth token - that is actually required to successfully call your backend - in a cookie. This will make it super easy to access it with each and every request you send.

要存储用户信息,我建议创建一个包装组件或使用root vue实例.下面的示例应澄清这一点.

To store the user information I'd suggest to either create a wrapping component or use the root vue instance. The following example should clearify this.

包装主页组件(内联模板)

data: function() {
  return { userinfo: {} }
},

created: function() {
  // load your user info
}

然后在您的index.html/主视图中使用它

Then use it in your index.html / main view

<body>
  <home inline-template>
    <!-- any stuff here -->

    <!-- pass the user prop to every component that is shown in the userinfo -->
    <router-view :user="userinfo"></router-view>
  </home>
</body>  

您在路由器视图中显示的组件随后可以访问用户道具

Your components that are shown in the router-view can then access the user prop

示例组件

...

props: ['user'],

...

<template>
   <span>{{ user.name }}</span>
</template>

...

重要提示:要执行此操作,您还需要在路线的定义中添加 props:true .此处详细说明了所有内容: https://router.vuejs.org/en /essentials/passing-props.html

IMPORTANT: to make this work you will also need to add props: true to the definition of your route. Everything is explained here in detail: https://router.vuejs.org/en/essentials/passing-props.html

备注:如果您不想在包装组件中加载用户数据,则可以在其他任何地方加载它,并使用事件总线将结果传输到包装组件.但是,关于用户信息,您始终始终只有一个事实来源.只能将其存储在单个位置.

Remark: If you don't want to load userdata in your wrapping component you can load it anywhere else and use an event bus to transfer the results to the wrapping component. However, you should always have only ONE source of truth regarding the user info. Store it only at a single place.

这篇关于保持登录的用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆