lovezsl2

165 经验值

@justtest 这个问题已解决,参照原有users的Model,把对应的继承和implements就ok了

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
/**
 * Class Users
 * @package App\Geo\Model
 */
class Users extends Model implements AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    /**

@justtest 半路出家,对oop确实不了解,现在是工作需要,我会好好了解下oop的

@justtest Users是要继承Model的,我加了use Illuminate\Contracts\Auth\Authenticatable;没有什么变化

@justtest users是要继承model的,use Illuminate\Contracts\Auth\Authenticatable;之后也没有变化 @JellyBool @惜君 i

ErrorException in Guard.php line 430:
Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Geo\Model\Users given

根据注册登录视频的讲解,使用auth模块能够实现登陆注册
然后自己就做了个新的注册和登录
登陆没问题,注册的入库也没有问题,但是在入库成功后使用Auth::login($user)就报这个错误,看到laravel的中文社区里也有这样的一个问题,回复的是使用Author::attempt来进行登陆,我做了测试,这样是可以登陆的,而且我的登陆就是使用的这种方法,现在就在纠结Auth::login为什么会报这样的错误

代码就是这样,登录和非登录访问权限,我放到了全局middleware里了
过滤了注册,登陆,退出

middleware文件

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\Auth\Guard;
class LoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $guestUrl = ['auth/login','auth/logout','auth/register'];
        //dd($request->path(),in_array($request->url(),$guestUrl));
        if (in_array($request->path(),$guestUrl)){
            return $next($request);
        }
        if (!Auth::check()) {
            return redirect('auth/login');
        }
        return $next($request);
    }
}

UserController文件

<?php

namespace App\Http\Controllers\User;


use App\Geo\Model\Users;
use App\Geo\Model\UserLoginRetry;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    /**
     * 获取登陆页面,判断是否已经登陆
     */
    public function getLogin()
    {
        session::flash('aaa','aaaa');
        return view('auth.login');
    }

    /**
     * 登录过滤条件
     * @param array $data
     */
    protected function loginValidator(array $data)
    {
        return Validator::make($data, [
            'username' => 'required|max:255|min:3|alpha_num',
            'password' => 'required|min:3|max:16|alpha_num',
        ]);
    }
    /**
     * 登录
     * @param Request $request
     * @return Ambigous <\Illuminate\Routing\Redirector, \Illuminate\Http\RedirectResponse, mixed, \Illuminate\Foundation\Application, \Illuminate\Container\static>
     */
    protected function postLogin(Request $request)
    {
        //过滤
        //查看用户是否存在
        //存在判断5分内密码是否3次错误
        //3次内执行登录
        //登录失败,记录密码错误
        $validator = $this->loginValidator($request->all());
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }
        $user = Users::username($request->input('username'))->first();
        
        if(isset($user)){
            if(count($user->retry()->where('created_at','>=',Carbon::now()->subMinutes(5))->get()) >=3){
                return '密码错误3次,5分钟后登录';
            }
            if (Auth::attempt(['username' => $request->input('username'), 'password' => $request->input('password')]))
            {
                return redirect('/home');
            }
            UserLoginRetry::create(['users_id'=>$user->id]);
        }
        return redirect('/auth/login');
    }
    /**
     * 注册过滤条件
     * @param array $data
     */
    protected function registerValidator(array $data){
        return Validator::make($data, [
            'username' => 'required|max:255|min:3|alpha_num|unique:users',
            'password' => 'required|confirmed|min:8|max:16|alpha_num',
            'status' => 'required',
            'permission' => 'required'
        ]);
    }
    /**
     * 获取注册页面,判断是否已经登录
     * @return Ambigous <\Illuminate\Routing\Redirector, \Illuminate\Http\RedirectResponse, mixed, \Illuminate\Foundation\Application, \Illuminate\Container\static>|Ambigous <\Illuminate\View\View, \Illuminate\Contracts\View\Factory, mixed, \Illuminate\Foundation\Application, \Illuminate\Container\static>
     */
    protected function getRegister(){
        Session::put('bbb','bbb');
        //return view('auth.register');
    }
    /**
     * 注册,成功后登录
     * @param Request $request
     * @return Ambigous <\Illuminate\Routing\Redirector, \Illuminate\Http\RedirectResponse, mixed, \Illuminate\Foundation\Application, \Illuminate\Container\static>
     */
    protected function postRegister(Request $request){
        $validator = $this->registerValidator($request->all());
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }
        $this->create($request->all());
        if(Auth::attempt(['username' => $request->input('username'), 'password' => $request->input('password')])){
            return redirect('/home');
        }
        //Auth::login($this->create($request->all()));
        return redirect('/auth/login');
    }
    
    /**
     * 退出
     * @return Ambigous <\Illuminate\Routing\Redirector, \Illuminate\Http\RedirectResponse, mixed, \Illuminate\Foundation\Application, \Illuminate\Container\static>
     */
    protected function getLogout(){
        Auth::logout();
        return redirect('/auth/login');
    }
    
    /**
     * 保存注册数据,加密密码
     * @param array $data
     * @return Ambigous <\Illuminate\Database\Eloquent\static, \App\Geo\Model\Users>
     */
    protected function create(array $data){
        return Users::create([
            'username' => $data['username'],
            'password' => bcrypt($data['password']),
            'status' => $data['status'],
            'permission' => $data['permission'],
        ]);
    }
}