実現したいこと:
管理者が新規登録後、ログアウトして
再度、ログインできるようにしたいです。
コード
config/auth.php
return [ /* |-------------------------------------------------------------------------- | Authentication Defaults |-------------------------------------------------------------------------- | | This option controls the default authentication "guard" and password | reset options for your application. You may change these defaults | as required, but they're a perfect start for most applications. | */ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], /* |-------------------------------------------------------------------------- | Authentication Guards |-------------------------------------------------------------------------- | | Next, you may define every authentication guard for your application. | Of course, a great default configuration has been defined for you | here which uses session storage and the Eloquent user provider. | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | Supported: "session", "token" | */ 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'user' => [ 'driver' => 'session', 'provider' => 'users', ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' =>'eloquent', 'model' =>App\Admin::class, ], 'admins' => [ 'driver' => 'database', 'table' => 'admins', ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], /* |-------------------------------------------------------------------------- | Resetting Passwords |-------------------------------------------------------------------------- | | Here you may set the options for resetting passwords including the view | that is your password reset e-mail. You may also set the name of the | table that maintains all of the reset tokens for your application. | | You may specify multiple password reset configurations if you have more | than one user table or model in the application and you want to have | separate password reset settings based on the specific user types. | | The expire time is the number of minutes that the reset token should be | considered valid. This security feature keeps tokens short-lived so | they have less time to be guessed. You may change this as needed. | */ 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], 'admins' => [ 'provider' => 'admins', 'email' => 'adminAuth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ], ];
AdminHomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Admin; use App\Http\Requests; class AdminHomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth:admin'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ /** * Display a listing of the resource. * * @return Response */ public function index() { // $lends = $this->lend->all(); // return view('adminAuth.lend',compact('lends')); } public function create() { return view('adminAuth.create'); } /** * 新しいブログポストの保存 * * @param Request $request * @return Response */ public function store(Request $request) { $input = $request->all(); $this->lend->fill($input); $this->lend->save(); return redirect()->to('adminAuth.lend'); } public function edit($id) { $lend = $this->lend->find($id); return view('edit')->with(compact('lend')); } public function update(Request $request, $id) { $input = $request->all(); $this->lend->where('id',$id)->update(['title' => $input['title']]); return redirect()->to('lend'); } public function destroy($id) { $data = $this->lend->find($id); $data->delete(); return redirect()->to('adminAuth.lend'); } }
middleware/Authenticate
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class Authenticate { /** * 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)->guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { if($guard === 'admin'){ return redirect()->guest('admin/login'); }else{ return redirect()->guest('login'); } } } return $next($request); } }
route.php
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('/', function () { return view('welcome'); }); Route::auth(); //追加 // Authentication Routes... $this->get('admin/login', 'AdminAuth\AuthController@showLoginForm'); $this->post('admin/login', 'AdminAuth\AuthController@login'); $this->get('admin/logout', 'AdminAuth\AuthController@logout'); // Registration Routes... $this->get('admin/register', 'AdminAuth\AuthController@showRegistrationForm'); $this->post('admin/register', 'AdminAuth\AuthController@register'); // Password Reset Routes... $this->get('admin/password/reset/{token?}', 'AdminAuth\PasswordController@showResetForm'); $this->post('admin/password/email', 'AdminAuth\PasswordController@sendResetLinkEmail'); $this->post('admin/password/reset', 'AdminAuth\PasswordController@reset'); //追加 Route::get('/home', 'HomeController@index'); Route::get('admin/home','AdminHomeController@index'); Route::delete('lend/destroy',[ 'as' => 'lend.destroy', 'uses' => 'AdminHomeController@destroy', ]); // Route::post('lend/store'); Route::post('lend/store',[ 'as' => 'lend.store', 'uses' => 'AdminHomeController@store', ]); Route::get('/lend/create','AdminHomeController@create');
Admin.php
<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; class Admin extends Authenticatable { /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
ここに質問したいことを詳細に書いてください
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
###発生している問題・エラーメッセージ
新規登録後、見せたいページに飛ばせますが、 ログインができません。 *データベースの管理者テーブルにはadminのアカウントが しっかり記録されています。 ちなみにエラーメッセージは表示されていません。
###該当のソースコード
ここにご自身が実行したソースコードを書いてください
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
あなたの回答
tips
プレビュー