配置和配置laravel auth

时间:2016-06-16 05:40:08

标签: php laravel authentication laravel-5.2

我正在尝试在正确验证发送的数据后进行登录。 验证工作正常,但是当我尝试登录时,总是返回false。可能有错吗?

非常感谢你。

UserController中:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Modals\Users as user;


class UserController extends Controller
{
    public function index()
    {
        return view('home');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function NewUser(Request $request)
    {
        $validator= Validator::make($request->all(),[

            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:1',
            'cpassword'=> 'required||same:password',

        ]);

        if($validator->fails()){
            return response()->json([
                'success' => false,
                'error' => $validator->errors()->toArray()
            ]);


        }else{

           return $this->CreateUser($request);


        }

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    private function CreateUser(Request $request){

        $user = new User; // was $user = new NewUser();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = $request->password; // was $user->passwosrd = $requestt->password
        $user->remember_token = $request->_token;
        $user->save();

        if (Auth::attempt(['email' => $request->email, 'password' => $request->password]))
        {

            return response()->json([
                'success' => true
            ]);
        }else

        {
            return response()->json([

                'success' => false,
                'error' => 'not login'

            ]);
        }



    }

auth.php

<?php

return [

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Modals\Users::class,
        ],

         'users' => [
             'driver' => 'database',
             'table' => 'users',
         ],
    ],



];

模态:

<?php

namespace App\Modals;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Users extends Model implements AuthenticatableContract, CanResetPasswordContract
{

    use Authenticatable, CanResetPassword;

    protected $table = 'users';

}

1 个答案:

答案 0 :(得分:1)

试试这个:

$user = new User; // was $user = new NewUser();
$user->name = $request->get('name');
$user->email = $request->get('email');
$user->password = \Hash::make($request->get('password'));
$user->remember_token = $request->get('_token'); //but why are you storing this?
$user->save();



if (Auth::attempt(['email' => $request->get('email'), 'password' => $request->get('password')]))
{
    return response()->json([
       'success' => true
    ]);
}else{
   return response()->json([
      'success' => false,
      'error' => 'not login'
   ]);
}