从单独的前端应用程序为Laravel应用程序创建注册令牌 [英] Creating a sign up token for Laravel app from separate frontend app

查看:45
本文介绍了从单独的前端应用程序为Laravel应用程序创建注册令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有独立Laravel后端的Angular应用.我要进行用户身份验证,特别是用户注册.

I'm creating an Angular app with a separate Laravel backend. I want to User auth, specifically User registration.

我遇到以下错误:

前端应用出现错误:

请求的请求上没有'Access-Control-Allow-Origin'标头资源.

No 'Access-Control-Allow-Origin' header is present on the requested resource.

尝试从DHC创建新用户时出错:

端点:

my_apps_api.localhost/api/auth/register

发布数据:

{
  "username" : "myusername"
  "password" : "12345"
}

VerifyCsrfToken.php 中的 TokenMismatchException

TokenMismatchException in VerifyCsrfToken.php


我认为这是因为我需要从我的有角度的应用程序生成令牌以传递回Laravel,但不确定如何做到这一点,或者甚至是问题所在.


I think this is because I need to generate a token from my angular app to pass back to Laravel, but am not sure how to do that, or if that is even the problem.

如何生成令牌以创建新用户?

How can I generate a token such that I can create a new user?

前端Angular应用:

注册控制器:

    $scope.submit = function() {
        Auth.signup({
            username: $scope.user.username,
            password: $scope.user.password
        }).success(function(res) {

身份验证服务:

        signup: function(params) {

            return $http({
                method: 'POST',
                url: authurl + 'auth/register',
                data: params,
                cache: true
            });
        }

Laravel API:

Route::group(['middleware' => 'cors'], function(\Illuminate\Routing\Router $router) {
    ...
    $router->controllers([
        'auth' => 'Auth\AuthController',
        'password' => 'Auth\PasswordController'
    ]);


注意:浏览 Laravel路由文档,我知道您可以像这样生成令牌:


Note: Looking through the Laravel Routing docs, I know you can generate the token as such:

<输入类型="hidden" name ="_ token" value =<?php echo csrf_token();?>">

但是不确定从外部前端应用发布到Laravel的方式.

But am not sure how to do that from an external frontend app posting to Laravel.

推荐答案

基于您的错误:VerifyCsrfToken.php中的TokenMismatchException您必须在中间件中进行Csrf检查,或在请求中发送Csrf令牌.

Based on you error:TokenMismatchException in VerifyCsrfToken.php You must turn of Csrf checks in middleware or send Csrf token in request.

如果要在重拍中使用crsf令牌,则应使用ajax从服务器获取它,如果要从服务器获取html,则应该使用html.在服务器端,crsf令牌是通过辅助函数生成的:csrf_token()

If you want crsf in token in requet you should get it from server with ajax, or in html if you are fetching html from server. On server side crsf token is generated with helper function: csrf_token()

这篇关于从单独的前端应用程序为Laravel应用程序创建注册令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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