【问题标题】:How do I get my user_id from the authorised client如何从授权客户端获取我的 user_id
【发布时间】:2017-10-24 04:36:19
【问题描述】:

我想检索当前在线用户的 ID。但是我不能用下面的代码来做:

Route::middleware('auth:api')->post('/optionelections', function (Request $request) {
        return $request->user();
    });

原因是我不断从 Laravel 收到相同的未经授权的错误。几天来我一直在尝试修复此错误,但似乎找不到解决方案。所以我试图以不同的方式来做,但我不知道怎么做。我目前正在使用 Passport 将我的令牌和我的 client_id 存储在本地存储中。

这是我的apply_election.vue

    import {apiDomain} from '../../config'
  export default {
    name: 'applyForElection',
    data () {
      return {
        election: {},
        newOption: {'election_id': ''},
        //this is where the user_id should come
        newOption: {'user_id': ''}

      }
    },
    methods: {
      createOption: function () {
        var itemId = this.$route.params.id
        this.newOption.election_id = itemId
        this.$http.post(apiDomain + 'optionelections', this.newOption)
          .then((response) => {
            this.newOption = {'election_id': itemId}
            alert('you applied!')
            this.$router.push('/electionsnotstarted')
          }).catch(e => {
            console.log(e)
            alert('there was an error')
            this.$router.push('/electionsnotstarted')
          })
      }
    },
    created: function () {
      var itemId = this.$route.params.id
      this.$http.get('http://www.nmdad2-05-elector.local/api/v1/elections/' + itemId)
        .then(function (response) {
          this.election = response.data
        })
    }
  }

这在我的OptionElectionsController.php

public function store(Request $request)
    {


        $optionElection = new OptionElection();
        $optionElection->user_id = $request['user_id'];
        $optionElection->option = "something";
        $optionElection->votes = 0;
        $optionElection->election_id = $request['election_id'];
        $optionElection->accepted = 0;

        if ($optionElection->save()) {


            return response()
                ->json($optionElection);

        }

    }

这是我的Auth.js

export default function (Vue) {
  Vue.auth = {
    setToken (token, expiration) {
      localStorage.setItem('token', token)
      localStorage.setItem('expiration', expiration)
    },
    getToken () {
      var token = localStorage.getItem('token')
      var expiration = localStorage.getItem('expiration')

      if (!token || !expiration) {
        return null
      }
      if (Date.now() > parseInt(expiration)) {
        this.destroyToken()
        return null
      } else {
        return token
      }
    },
    destroyToken () {
      localStorage.removeItem('token')
      localStorage.removeItem('expiration')
    },
    isAuthenticated () {
      if (this.getToken()) {
        return true
      } else {
        return false
      }
    }
  }

  Object.defineProperties(Vue.prototype, {
    $auth: {
      get: () => {
        return Vue.auth
      }
    }
  })
}

【问题讨论】:

    标签: php laravel authentication vue.js passport.js


    【解决方案1】:

    你使用的是Laravel的TokenGuard,有很多方法可以让守卫识别认证,最好的方法:

    1. 在请求的查询中以api_token 属性发送令牌。

      this.newOption.api_token = token;
      
    2. 在带有Bearer 前缀的Authorization 标头中发送令牌。

      { 
          headers: { 
              Authorization: 'Bearer THE_TOKEN'
          }
      }
      

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 2012-02-28
      • 2016-05-19
      • 2020-07-28
      • 2017-07-16
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多