Upgrade to Pro — share decks privately, control downloads, hide ads and more …

使用者認證

 使用者認證

Shengyou Fan

December 14, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. Laravel తೝᨽ޻۩ • Laravel ݪੜब༗㚎ݐೝᨽྨผ Authʀࡏሢ Ҋ啟ಈ࣌ɼबቮៃት༗ User Modelʀࣕࡏ artisan

    ࢦྩ㚎ɼ໵ቮៃఏڙ㗞ੜॏઃີᛰ ૬᮫ػ੍తࢦྩ • ෆաɼ App ຊ਎ཁዎኄመ࡞ೝᨽػ੍ɼଇ ཹڅ։ᚙਓһࣗߦܾఆ ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/security
  2. App తೝᨽػ੍ • Ḽ࡭/ாᥒ啟ಈػ੍ (Email) • ొೖ/ొग़ • ๨هີᛰ/ॏઃີᛰ •

    ॏدೝᨽ৴ P.S  ຊ࣍޻࡞๥ᷮࣔൣ೗Կመ࡞ొೖ/ొग़తޭೳ
  3. መ࡞㚎༰ • መ࡞Ұݸ users ࢿྉදɼཫ໘Ṷଘ࢖༻ऀ ாᥒɺີᛰٴ remember tokenɼฒ४උ UserTableSeederɼࢸগઃఆҰ૊ாີ •

    መ࡞ AuthController ႔ཧొೖ/ొग़ಈ࡞ • መ࡞ొೖทɺ֤ Views ্ొೖ/ొग़㐫ଶత ൑Ꮧػ੍ • ઃఆ Route
  4. 㗞ੜ users ࢿྉද • ༻ artisan ࢦྩݐཱҰݸ Migration ိ㗞ੜ users

    ࢿྉදɼࡏ users ࢿྉද㚎ɼࢸগཁ ༗ாᥒɺີᛰٴ remember token ࡾཝҐ //  app/databases/migrations/create_users_table.php Schema::create(‘users’,  function(Blueprint  $table) {        $table-­‐>increments(‘id’);        $table-­‐>string(‘username’)-­‐>unique();        $table-­‐>string(‘password’);        $table-­‐>rememberToken();        $table-­‐>timestamps(); }
  5. 㗞ੜாᥒ • ৽⃧ UserTableSeederɼฒࡏཫ໘ઃఆҰ૊ ாᥒ/ີᛰ P.S  هಘࡏ  DatabaseSeeder  ཫ։啟  UserTableSeederɼฒࣥߦ

     db:seed //  app/databases/seeds/UserTableSeeder.php public  function  run() {        DB::table(‘users’)-­‐>truncate();                User::create([                ‘username’  =>  ‘{username}’,                ‘password’  =>  Hash::make(‘{password}’),        ]); }
  6. ᰖࣔొೖදᄸ • ए࢖༻ऀؐ຤ొೖɼᰖࣔొೖදᄸరሜா ᥒٴີᛰ //  app/views/login/index.blade.php Form::open(['route'  =>  'auth.process',  'method'

     =>  'POST',   'class'  =>  'form-­‐signin',  'role'  =>  'form']) Form::text('username',  null,  ['class'  =>  'form-­‐control',   'placeholder'  =>  '੥༌ೖ䓟తாᥒ',  'required',  'autofocus']) Form::password('password',  ['class'  =>  'form-­‐control',   'placeholder'  =>  '੥༌ೖ䓟తີᛰ',  'required']) Form::submit('ొೖ',  ['class'  =>  'btn  btn-­‐lg  btn-­‐primary  btn-­‐ block']) Form::close()
  7. ᱛᨽొೖࢿ㘤 • ࡏ AuthController ཫ઀Ꮕొೖࢿ㘤ฒᱛ ᨽɼ၊೗੒ޭత࿩बಋճटทɼෆ੒ޭత ࿩बฦճొೖท //  app/controllers/AuthController.php if

     (Auth::attempt(['username'  =>  Input::get('username'),   'password'  =>  Input::get('password')],  Input::get('remember-­‐ me'))) {   return  Redirect::route('home.index')-­‐>with('success',  '੒ ޭొೖ'); }
  8. ႔ཧొग़ಈ࡞ • ሡొೖత࢖༻ऀొग़ɼಈ࡞׬੒ޙճ౸ट ทɼฒᰖࣔ੒ޭఏࣔ //  app/controllers/AuthController.php public  function  logout() {

      Auth::logout();     return  Redirect::route('home.index')                                      -­‐>with('success',  'ቮ੒ޭొग़'); }
  9. View ૬᮫൑Ꮧ • ݪຊࡏ View ্໘ሜత @if ൑Ꮧɼվ੒ਅ ਖ਼తొೖ൑Ꮧ //

     app/views/partials/sidebar.blade.php <h4>໢᜾؅ཧ</h4> @if  (Auth::check()) <p><span  class="label  label-­‐success">؅ཧத</span>  <a   href="{{  route('auth.logout')  }}">ొग़</a></p> @else <p><a  href="{{  route('auth.login')  }}">ొೖ</a></p> @endif
  10. Route ઃఆ • ሡ୞ఏڙڅ؅ཧऀ༻త Route ૊߹੒ groupɼฒሣ֘ group Ճ্ auth

    filter Ҏ֬ ೝṜࠣ route ᷮೳొೖޙ࢖༻ //  app/routes.php //  ⃧Ճొೖ႔ཧత  Route Route::post('login/process',  ['as'  =>  'auth.process'  ,   'uses'  =>  'AuthController@process']); Route::get('logout',  ['as'  =>  'auth.logout'  ,  'uses'  =>   'AuthController@logout']); //  ᷮ༗؅ཧऀొೖޙ࠽ՄҎ࢖༻త  Route  ์ࡏ  group  ཫ Route::group(array('before'  =>  'auth'),  function() { });