Laravel-admin 登入新增驗證碼

july1115發表於2019-08-19

Laravel-admin 登入新增驗證碼

最近在用laravel-amdin搭建後臺,記錄一下

一. 按照驗證碼庫

  1. 引入: composer require mews/captcha

  2. 修改config/app.php

    'providers' => [
        // ...
        Mews\Captcha\CaptchaServiceProvider::class,
    ]   
    
     'aliases' => [
        // ...
        'Captcha' => Mews\Captcha\Facades\Captcha::class,
    ]
  3. php artisan vendor:publish

  4. 修改config/captcha.php的default

return [
            'default'   => [
                'length'    => 5,
                'width'     => 120,
                'height'    => 36,
                'quality'   => 90,
            ],
            // ...
        ];

二. 修改登入方法

修改app/Admin/Controllers/AuthController.php,如沒有則複製vendor/encore/laravel-admin/src/Controllers/AuthController.php到app/Admin/Controllers/AuthController.php,修改程式碼如下:


<?php
namespace App\Admin\Controllers;
use Encore\Admin\Controllers\AuthController as BaseAuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
class AuthController extends BaseAuthController
{
    public function getLogin()
    {
        if (!Auth::guard('admin')->guest()) {
            return redirect(config('admin.route.prefix'));
        }
        return view('admin.login');
    }

    public function postLogin(Request $request)
    {
        $credentials = $request->only(['username', 'password','captcha']);
        $validator = Validator::make($credentials, [
            'username' => 'required',
            'password' => 'required',
            'captcha' => 'required|captcha'
        ]);
        if ($validator->fails()) {
            return Redirect::back()->withInput()->withErrors($validator);
        }
        unset($credentials['captcha']);
        if (Auth::guard('admin')->attempt($credentials)) {
            admin_toastr(trans('admin.login_successful'));
            return redirect()->intended(config('admin.route.prefix'));
        }
        return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
}

    protected function getFailedLoginMessage()
    {
        return Lang::has('auth.failed')
        ? trans('auth.failed')
        : 'These credentials do not match our records.';
        }
    }

四、修改頁面:

複製vendor/encore/laravel-admin/resources/views/login.blade.php到resources/views/admin/login.blade.php
在密碼與記住我程式碼塊中間新增程式碼

、、、

<div class="form-group has-feedback {!! !$errors->has('password') ?: 'has-error' !!}">
    @if($errors->has('password'))
        @foreach($errors->get('password') as $message)
            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label><br>
        @endforeach
    @endif
        <input type="password" class="form-control" placeholder="{{ trans('admin.password') }}" name="password">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>

<!-- 在這裡新增程式碼  start-->
<div class="row">
    <div class="form-group has-feedback {!! !$errors->has('captcha') ?: 'has-error' !!}">
        @if($errors->has('captcha'))
            @foreach($errors->get('captcha') as $message)
                <label class="control-label" for="inputError" style="margin-left: 15px"><i class="fa fa-times-circle-o">{{$message}}</i></label></br>
            @endforeach
        @endif
        <input type="text" class="form-control" style="display: inline;width: 55%; margin-left: 15px" placeholder="{{ trans('admin.captcha') }}" name="captcha">
        <span class="glyphicon glyphicon-refresh form-control-feedback captcha" style="right:39%;z-index: 100"></span>
        <img class="captcha" src="{{ captcha_src('admin') }}">
    </div>
</div>
<!-- 在這裡新增程式碼  end-->

<div class="row">
    <div class="col-xs-8">
    @if(config('admin.auth.remember'))

    、、、

完成

相關文章