程式CODE

2016年12月7日 星期三

laravel5.3 開啟內建auth使用者認證

一、建立資料庫
請參考之前文章,把資料庫及資料表users建立起來
users資料表在 ./database/migrations/2014_10_12_000000_create_users_table.php中設定

http://etplayinfo.blogspot.tw/2016/11/laravel53_29.html

自行在users表中再新一個 admin 的欄位 type 是 tinyint ,若為管理者,值為 1

二、切至laravel網站根目錄
php artisan make:auth
會產生多個 view 在 ./resources/views/auth


三、在 welcaom.blade.php中,多了以下


四、立即註冊登入






五、利用 middleware設計具管理身份

1.先建立 AdminMiddleware 切到 laravel 根目錄
php artisan make:middleware AdminMiddleware

2.修改 AdminMiddleware
在 ./app/Http/Middleware/ 下,把 RedirectfAuthenticated.php 內容複製到 AdminMiddleware.php 中,改類別名為 AdminMiddleware
略為修改為以下內容
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check() && Auth::user()->admin == 1) { 
//若users資料表內的admin欄為1,則下一個request,否則返回 / 
            return $next($request);
        }else{
            return redirect('/');
        }


    }
}

由以上得知,在往後要認證註冊:
Auth::guard($guard)->check()


要登入認證的controller,只要加入建構函式
     */
     public function __construct()
     {
         $this->middleware('auth');
     }
//
//except除此之外,都要用auth認證
//$this->middleware('auth')->except();
//or 只用
//$this->middleware('auth')->only();

要認證管理者登入:
if (Auth::guard($guard)->check() && Auth::user()->admin == 1) 


六、在需要管理者的頁面新增route
如:url/admin
Route::group(['middleware' => 'admin'],function(){
  Route::get('/admin', function () {
      echo "你是管理者";
  });
});


七、啟用 AdminMiddleware.php
在 ./app/Http/Kernel.php 中,增加以下 ...'admin'....這行,是copy 'guest' 這行再修改來的

protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'admin' => \App\Http\Middleware\AdminMiddleware::class,
    ];

八、改用username登錄,而不是email
https://kjamesy.london/work/laravel-53-auth-allow-username-and-email

1.先在user資料表多一個username欄位

2.到 ./resources/views/auth/login.blade.php中,修改原  email欄位:
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
    <label for="email" class="col-md-4 control-label">E-Mail Address</label>
    <div class="col-md-6">
        <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
             @if ($errors->has('email'))
               <span class="help-block">
                 <strong>{{ $errors->first('email') }}</strong>
             </span>
        @endif
    </div>
</div>
成:

<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
    <label for="username" class="col-md-4 control-label">Username or Email</label>
    <div class="col-md-6">
        <input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" autofocus>
        @if ($errors->has('username'))
            <span class="help-block">
                <strong>{{ $errors->first('username') }}</strong>
            </span>
        @endif
    </div>
</div>

3.在 ./app/Http/Controllers/Auth/LoginController.php
新增一個 function
public function username()
{
    return 'username';
}

沒有留言:

張貼留言