:::

17-1 使用新北市OpenID登入

  1. 先開啟\專案\resources\views\layouts\nav.blade.php,加入登入選項:
    <li class="nav-item">
        <form action="/auth/login/openid" method="post" style="display:inline">
            @csrf
            <button class="btn btn-link">OpenID 登入</button>
        </form>
    </li>
  2. 建立控制器:
    php artisan make:controller OpenIDController
  3. 編輯OpenID控制器\專案\app\Http\Controllers\OpenIDController.php
    <?php
    
    namespace App\Http\Controllers;
    
    use Backpack\Base\app\Models\BackpackUser;
    use Illuminate\Support\Facades\Auth;
    
    class OpenIDController extends Controller
    {
        public function ntpcopenid()
        {
            $openid = app('ntpcopenid');
            return redirect($openid->authUrl());
    
        }
    
        public function get_ntpcopenid()
        {
    
            $openid = app('ntpcopenid');
    
            switch ($openid->mode) {
                case 'cancel': // 取消授權
                    return 'User Canceled';
                    break;
    
                case 'id_res': // 同意授權
                    if (!$openid->validate()) {
                        // 驗證未過
                        // 導向至登入畫面
                        return redirect('auth/login');
                    }
    
                    // 驗證通過,檢查是否允許登入
                    if ($openid->canLogin()) {
                        // 允許登入
                        // 取得 user data 陣列
                        $data = $openid->getUserData('*');
    
                        //搜尋使用者是否存在,沒有就新增
                        $user           = BackpackUser::firstOrNew(['name' => $data['namePerson']]);
                        $user->email    = $data['contact/email'];
                        $user->password = password_hash($user->email, PASSWORD_DEFAULT);
    
                        //  註冊使⽤用者
                        if (!$user->exists) {
                            $user->save();
                            if ($data['pref/timezone'][0]['role']) {
                                $user->assignRole('學生');
                            }
                        }
    
                        //  登⼊入使⽤用者
                        Auth::login($user);
                        // 將取得的資料,轉成陣列存入session中
                        session($data);
                    }
                    // 不允許登入,例如導回登入頁面或顯示訊息
                    return redirect('/');
                    break;
    
                default: // 其他,如直接輸入網址瀏覽
                    return redirect('/');
                    break;
            }
    
        }
    }
    
  4. 編輯路由\專案\routes\web.php
    // 處理表單,導向至 NTPC OpenID 登入
    Route::post('auth/login/openid', 'OpenIDController@ntpcopenid')->name('ntpcopenid');
    
    // OpenID 導回
    Route::get('auth/login/openid', 'OpenIDController@get_ntpcopenid')->name('get_ntpcopenid');
  5.  

:::

書籍目錄

展開 | 闔起

https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1327%26tbsn%3D37

計數器

今天: 617617617
昨天: 2027202720272027
總計: 7971658797165879716587971658797165879716587971658