如何从授权客户端获取我的user_id

时间:2017-05-24 02:48:13

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

我想检索当前在线用户的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
      }
    }
  })
}

1 个答案:

答案 0 :(得分:0)

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

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

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

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