dongpang1898 2018-10-10 14:32
浏览 264
已采纳

Laravel + React,使用Laravel身份验证使用api

we have a Laravel project with react as the front-end. Basically react is inside the laravel project, we used php artisan preset react to add it.

As this application needs authentication, we used the custom laravel auth to give access to the users. Then when the authentication is correct, we redirect the user to a route that will be managed by react and react router. The problem is that we need to consume our API endpoints from the same app, and those endpoints MUST be protected. The laravel Auth is not working there, the sessión information is not being sent on each request. I’ve tried https://laravel.com/docs/5.7/authentication#stateless-http-basic-authentication that although it solves the problem is not convenient to log in and then when want to consume another resource show a prompt to log in again. Also change the api routes to a web middleware is not an option.

Does someone knows how to protect the laravel API routes with the normal laravel authentication

  • 写回答

2条回答 默认 最新

  • dongsechuan0535 2019-04-16 01:51
    关注

    The solution was simple, even that is on the documentation, the necessary steps should be clarified.

    We need to:

    1. Add passport composer require laravel/passport
    2. Make the migrations php artisan migrate
    3. Install passport php artisan passport:install

    The fourth step is more complex. We need to open our User.php model file. And first we need to import the HasApiTokens and tell the model to use it.

    use Laravel\Passport\HasApiTokens;
    
    class User extends Authenticatable
    
    {
    
        use HasApiTokens, Notifiable;
    
        .......
    
    }
    

    Then on our config/auth.php we need to modify the api array and change the driver to passport

    'api' => [
    
        //for API authentication with Passport
    
        'driver' => 'passport',
    
        'provider' => 'users',
    
    ],
    

    Then on our app/Http/Kernel.php we need to add a middleware to the $middlewareGroups array in the key web.

    protected $middlewareGroups = [
    
        'web' => [
    
            ................
    
            //for API authentication with Passport
    
            \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
    
        ],
    

    Now we can use the auth:api middleware on our api routes.

    Route::middleware('auth:api')->group( function(){
        ...your routes here
    });
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥40 selenium访问信用中国
  • ¥15 电视大赛投票系统的c语言代码怎么做
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?
  • ¥15 ctrl win alt 键一直触发
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部