Basic Auth Laravel4如何使用它?

时间:2014-07-03 10:39:33

标签: api laravel-4 basic-authentication

我尝试使用凭据保护我的restAPI并阅读basic-auth laravel我尝试实现基本身份验证系统

用户标签已存在并填充了数据

filter.php中的

我设置了

  

路由::过滤器(' auth.basic',function(){       return Auth :: basic(); });

比api Route

// =============================================
// API ROUTES ==================================
// =============================================
Route::group(array('prefix' => 'api', 'before' => 'auth.basic'), function() {

            Route::resource('products', 'ProductController', array('only' => array('index', 'store', 'destroy', 'update', 'show', 'edit')));
            Route::get('products/{id}', 'ProductController@get', array('only' => array('show')));
        });

控制器非常简单

<?php

use App\Models\Product;

class ProductController extends \BaseController {

    private $model;

    function __construct() {
        $this->model = new Product();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $model = new Product();
        $page           =      Input::get('pageNumber');
        $limit          =      Input::get('pageNumber');
        $ram            =      Input::get('limit');
        $cpu            =      Input::get('cpu');
        $price_range    =      Input::get('price_range');
        $keyword       =      Input::get('keyword');
        return Response::json($model->getProducts($page));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {

    }

    public function get($id) {
        $model = new Product();
        return Response::json($model->getProduct($id));
    }

    public function show($id) {       
        return Response::json($this->model->getProduct($id));
    }

    public function update($id) {       
        return Response::json($this->model->getProduct($id));
    }

    public function pause($id) {
        var_dump('pause');
    }

    public function create(){

    }

    public function edit(){
        var_dump('test_edit');
    }

}

调用domain.com/api/products弹出登录窗口。填充字段和提交数据无法登录

如何查看用户凭据?

对于后端我使用Sentry并且它正在工作

filter.php

Route::filter('auth.admin', function() {
    if (!Sentry::check()) {
        return Redirect::route('admin.login');
    }
});

路线

  

路由:: get(&#39; admin / login&#39;,数组(&#39; as&#39; =&gt;&#39; admin.login&#39;,&#39;使用&#39; ;   =&GT; &#39;应用软件\控制器\管理员\ AuthController @ getLogin&#39;));

控制器

<?php namespace App\Controllers\Admin;

use Auth, BaseController, Form, Input, Redirect, Sentry, View;

class AuthController extends BaseController {

    /**
     * Display the login page
     * @return View
     */
    public function getLogin()
    {
        return View::make('admin.auth.login');
    }

    /**
     * Login action
     * @return Redirect
     */
    public function postLogin()
    {
        $credentials = array(
            'email'    => Input::get('email'),
            'password' => Input::get('password')
        );

        try
        {
            $user = Sentry::authenticate($credentials, false);

            if ($user)
            {
                return Redirect::route('admin.pages.index');
            }
        }
        catch(\Exception $e)
        {
            return Redirect::route('admin.login')->withErrors(array('login' => $e->getMessage()));
        }
    }

    /**
     * Logout action
     * @return Redirect
     */
    public function getLogout()
    {
        Sentry::logout();

        return Redirect::route('admin.login');
    }

}

1 个答案:

答案 0 :(得分:0)

您似乎没有定义登录功能。

顺便说一下,你应该改变:

Route::group(array('prefix' => 'api', 'before' => 'auth.basic'), function() {

            Route::resource('products', 'ProductController', array('only' => array('index', 'store', 'destroy', 'update', 'show', 'edit')));
            Route::get('products/{id}', 'ProductController@get', array('only' => array('show')));
        });

为:

Route::group(array('prefix' => 'api', 'before' => 'auth.basic'), function(){

    Route::get('products/{id}', 'ProductController@get'));
    Route::resource('products', 'ProductController', array('except' => array('show')));
});