【问题标题】:Laravel with Vue and PassportLaravel 与 Vue 和 Passport
【发布时间】:2020-08-02 02:08:52
【问题描述】:

我有这个项目,我用 laravel 安装了护照和 vue。我有我的身份验证端点,但现在我正在努力让这些端点与我的项目前端一起工作。

所以当我安装 Laravel 时,我使用推荐 php artisan ui vue --auth 来生成我的登录/注册脚手架。

api端点在文件api.php中

这是我来自 AuthController.php 的代码的一部分:

    public function login(ApiLoginRequest $request){
    //debug
        //return $request;
    $credentials = $request->only(['email', 'password']);
    $authAttempt = auth()->attempt($credentials);
    if(!$authAttempt){
        return response([
            'error'   => 'Access forbidden',
            'message' => 'Please check your email and password'
        ], 403 );
    }
    //debug result needs to be true
        // return response([
        //     'debug response' => $authAttempt
        // ]);
    $http = new Client();
    $response = $http->post( 'http://laravel-xyzhub.test/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => $this->CLIENT_ID,
            'client_secret' => $this->CLIENT_SECRET,
            'username' => $request->email,
            'password' => $request->password,
            'scope' => ''
        ]
    ]);
    return json_decode((string) $response->getBody(), true );
}

vue 模板代码:

<template>

<div class="mx-aut h-full flex justify-center items-center bg-gray-800">
    <div class="w-96 bg-green-400 rounded-lg shadow-xl p-6" >

        <div class="text-center text-white uppercase">
            <h1 class="font-extrabold text-6xl">XYZ HUB</h1>

            <h1 class="text-3xl pt-8">Welcome Back</h1>
            <h2 class="pb-8 text-xs">Enter your credentials below</h2>
        </div>

        <!-- form -->
        <form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">

            <div class="relative">
                <label for="email" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Email</label>

                <div class="">
                    <input id="email" type="email" class="pt-8 w-full rounded p-3 text-green-700" name="email" v-model="credentials.email" autocomplete="email" autofocus placeholder="your@email.com">
                </div>
            </div>

            <div class="relative pt-6">
                <label for="password" class="uppercase text-green-400 font-bold absolute pl-3 pt-2">Password</label>

                <div class="">
                    <input id="password" type="password" class="pt-8 w-full rounded p-3 text-green-700" name="password" v-model="credentials.password" placeholder="password">
                </div>
            </div>

            <div class="pt-8">
                <button type="submit" class="uppercase font-bold rounded w-full bg-white text-green-400 py-2 px-3 text-2xl" v-on:click="loginUser">Login</button>
            </div>

            <div class="flex justify-center pt-8 text-white uppercase font-semibold text-sm">
                <a :href="recover"> Forget Password </a>
            </div>

            <div class="flex justify-center pt-2 text-white uppercase font-semibold text-sm">
                <a :href="register"> Register </a>
            </div>

        </form>

    </div>
</div>

<script>
export default {

    data(){
        return { 
            credentials:{
                email: '',
                password: ''
            }
        }
    },

    methods:{

        postNow(event){
            console.log("event" , {event , $form: this.$refs.LoginForm});

            axios.post('/api/login' , this.credentials)
            .then( response => {
                console.log ('response' , response.data); router.push("/register");
            })
            .catch( error => {
                console.log("error", error.response);
            })
        }
    } 

}

我知道我在模板中的代码不正确只是不确定我做错了什么......任何帮助将不胜感激。提前致谢

更新: 因此,如果我删除表单中的阻止并在响应通过后添加重定向,那么突然间我的提交不会通过

 <form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">

改成

 <form class="pb-8" @submit="postNow" ref="LoginForm" method="post">

你注意到我的端点也突然不正确了

真的对这一切感到困惑......

【问题讨论】:

  • 您将credentials 对象发送到您的login 路由,但似乎没有在您的Vue 模板中使用credentials
  • @DelenaMalan 嗯,你是对的。如何在我的 vue 模板中使用凭据(vue 新手,所以我正在学习)...
  • v-model 属性中,您可以使用credentials.emailcredentials.password 而不仅仅是emailpassword
  • @DelenaMalan 谢谢。让它工作。最后这么傻

标签: laravel vue.js axios laravel-passport


【解决方案1】:

您注意到端点错误。

所以改为:

axios.post('/login' , this.credentials)

改成

axios.post('/api/login' , this.credentials)

错误可能是由于路由“/login”中缺少 csrf 令牌字段。
有了 api 路由,就不需要 csrf。

另外,记录错误响应更容易,例如...

.catch( error => {
   console.log(error.response);
})

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 2018-07-16
    • 2018-03-28
    • 2020-05-28
    • 2020-11-22
    • 2021-03-29
    • 2020-06-14
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多