首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带Vue和Passport的Laravel

带Vue和Passport的Laravel
EN

Stack Overflow用户
提问于 2020-04-19 16:26:34
回答 1查看 590关注 0票数 0

我有一个项目,我在那里安装了护照和雨果。我有自己的端点,但是现在我很难让这些端点与项目的前端一起工作。

因此,当我安装Laravel时,我使用赞扬的php手工ui vue -auth来生成登录/注册脚手架。

api端点位于文件api.php中。

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

代码语言:javascript
复制
    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模板的代码:

代码语言:javascript
复制
<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>

代码语言:javascript
复制
<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);
            })
        }
    } 

}

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

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

代码语言:javascript
复制
 <form class="pb-8" @submit.prevent="postNow" ref="LoginForm" method="post">

将其更改为

代码语言:javascript
复制
 <form class="pb-8" @submit="postNow" ref="LoginForm" method="post">

正如您注意到的,我的端点突然也不正确。

对这一切都很困惑..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-19 17:00:35

您注意到端点是错误的。

因此,相反:

代码语言:javascript
复制
axios.post('/login' , this.credentials)

更改为

代码语言:javascript
复制
axios.post('/api/login' , this.credentials)

错误可能是由于路由"/login“中缺少csrf令牌字段所致。

使用api路由,您不需要csrf。

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

代码语言:javascript
复制
.catch( error => {
   console.log(error.response);
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61307789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档