Laravel - 在标题中发送 api_token [英] Laravel - Send api_token within the header

查看:40
本文介绍了Laravel - 在标题中发送 api_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Laravel 构建 API,我想在标题中发送 api_token 而不是表单帖子.这是已经内置的东西还是我必须去弄清楚如何创建我自己的身份验证驱动程序?

I'm building an API for Laravel and I want to send the api_token within the header instead of the form post. Is this something that is already built in or would I have to go the route of figuring out how to create my own auth driver?

推荐答案

在我自己为此苦苦挣扎之后,我让它工作了.您需要首先遵循这个关于如何为 Laravel API 使用 api_token 的小教程:https://gistlog.co/JacobBennett/090369fbab0b31130b51

After struggling a little with this myself I got it working. You need to first follow this little tutorial on how to use the api_token for your laravel API: https://gistlog.co/JacobBennett/090369fbab0b31130b51

然后,一旦您在 users 表等中拥有 api_token,您现在就可以在每个请求的标头中传递它.

Then once you have the api_token in the users table etc you can now pass this in the header of each request.

我的 Laravel 正在使用 Vueify 模板,即我在/components/Comment.vue 等文件下.

My laravel is using the Vueify templates, ie I have under /components/Comment.vue etc files.

第一步是通过刀片模板中的组件定义传递一个属性,将用户 api_token 传递给 Vue 模板:

First step is to pass the users api_token to the Vue Template by passing a property through the component definition in your blade template:

<comments id_token="{{ access()->user()->api_token }}"></comments>

然后确保在您的 .vue 文件中通过将其添加到props"中来接受​​该属性:

Then ensure in your .vue file that you accept the property by adding it to the "props":

export default {
    data: function() {
        return {
            edit: false,
            list: [],
            comment: {
                id: '',
                name: '',
                body: ''
            }
        };
    },

    props: ['id_token'],

    created: function() {
        Vue.http.headers.common['Authorization'] = 'Bearer ' + this.id_token;

        this.fetchCommentList();
    },

请注意,上面我还将令牌添加到公共标头中,以便让它通过所有方法中使用的每个请求进一步向下.

Vue.http.headers.common['Authorization'] = 'Bearer ' + this.id_token;

这篇关于Laravel - 在标题中发送 api_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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