Php 如何从授权客户处获取我的用户id

Php 如何从授权客户处获取我的用户id,php,laravel,authentication,vue.js,passport.js,Php,Laravel,Authentication,Vue.js,Passport.js,我想检索当前联机用户的id。但我无法使用以下代码执行此操作: Route::middleware('auth:api')->post('/optionelections', function (Request $request) { return $request->user(); }); 原因是我一直从拉威尔那里得到同样未经授权的错误。几天来我一直在试图修复这个错误,但似乎找不到解决方案。所以我试着用另一种方式去做,但我不知道怎么做。我目前正在使用Pass

我想检索当前联机用户的id。但我无法使用以下代码执行此操作:

Route::middleware('auth:api')->post('/optionelections', function (Request $request) {
        return $request->user();
    });
原因是我一直从拉威尔那里得到同样未经授权的错误。几天来我一直在试图修复这个错误,但似乎找不到解决方案。所以我试着用另一种方式去做,但我不知道怎么做。我目前正在使用Passport将我的令牌和我的
客户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
        })
    }
  }
这在我的
optionSelectionsController.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
      }
    }
  })
}

您正在使用Laravel的
TokenGuard
,有很多方法可以让guard识别身份验证,最好的方法是:

  • 在请求查询的
    api\u token
    属性中发送令牌

    this.newOption.api_token = token;
    
  • Authorization
    头中使用
    Bearer
    前缀发送令牌

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