Organizationsテーブルのlogin_idとpasswordでログイン。
html
1<!-- ! メイン ============================== --> 2<main class="bg_yellow"> 3 <section id="login" class="page_login"> 4 <div class="container"> 5 <div class="area_cnt"> 6 <?= $this->Form->create() ?> 7 <ul class="list_login"> 8 <li> 9 <div class="head"> 10 <p class="ttl">ID</p> 11 </div> 12 <div class="cnt"> 13 <div class="wrap_input"> 14 <?= $this->Form->control('login_id', ['type' => 'text', 'label' => false, 'placeholder' => 'ログインID']); ?> 15 </div> 16 </div> 17 </li> 18 <li> 19 <div class="head"> 20 <p class="ttl">PASSWORD</p> 21 </div> 22 <div class="cnt"> 23 <div class="wrap_input"> 24 <?= $this->Form->control('password', ['label' => false, 'placeholder' => 'パスワード']) ?> 25 </div> 26 </div> 27 </li> 28 </ul> 29 <div class="btnarea_center"> 30 <?= $this->Form->button(__('LOGIN'), ['escape' => false, 'class' => 'btn_line_large']) ?> 31 </div> 32 <?= $this->Form->end() ?> 33 </div> 34 </div> 35 </section> 36</main>
php
1<?php 2namespace App\Controller; 3 4use Cake\Controller\Controller; 5use Cake\Event\Event; 6 7class AppController extends Controller 8{ 9 public function initialize() 10 { 11 parent::initialize(); 12 13 $this->loadModel('Organizations'); 14 15 $this->loadComponent('Common'); 16 17 $this->loadComponent('RequestHandler', [ 18 'enableBeforeRedirect' => false, 19 ]); 20 21 $this->loadComponent('Flash'); 22 23 //ログイン状態設定 24 $this->loadComponent('Auth', [ 25 'authenticate' => [ 26 'Form' => [ 27 'userModel' => 'Organizations', 28 'fields' => [ 29 'username' => 'login_id', 30 'password' => 'password' 31 ] 32 ] 33 ], 34 'loginAction' => [ 35 'controller' => 'Pages', 36 'action' => 'login' 37 ], 38 'loginRedirect' => [ 39 'controller' => 'Pages', 40 'action' => 'index' 41 ], 42 'unauthorizedRedirect' => $this->referer(), // 未認証時、元のページを返します。 43 'authError' => 'ログインが必要です。' 44 ]); 45 46 // ログインユーザーを変数に入れておく 47 $loginOrganization = $this->Auth->user(); 48 if($loginOrganization){ 49 $loginOrganization = $this->Organizations->get($loginOrganization['id']); 50 } 51 } 52}
php
1<?php 2namespace App\Controller; 3 4use Cake\Core\Configure; 5use Cake\Http\Exception\ForbiddenException; 6use Cake\Http\Exception\NotFoundException; 7use Cake\View\Exception\MissingTemplateException; 8 9class PagesController extends AppController 10{ 11 public function initialize() 12 { 13 parent::initialize(); 14 $this->Auth->allow(['index', 'login']); 15 $this->loadModels('Organizations'); 16 } 17 18 public function index() 19 { 20 21 } 22 23 // ログイン 24 public function login() 25 { 26 $this->set('title', 'ログイン'); 27 if ($this->request->is('post')) { 28 $data = $this->request->getData(); 29 $organization = $this->Auth->identify(); 30debug($organization); 31exit; 32 if ($organization) { 33 $this->Auth->setUser($organization); 34// $this->Flash->success(__('ログインしました')); 35 return $this->redirect(['controller' => 'Pages', 'action' => 'index']); 36 // クッキーの継続時間を2週間に設定 37 setcookie(LOGIN_PASSPORT_NAME, $passport, LOGIN_DURATION, '/'); 38 return $this->redirect($this->Auth->redirectUrl()); 39 } 40// $this->Flash->error(__('ログインIDもしくはパスワードが間違っています')); 41 return $this->redirect([]); 42 } 43 } 44} 45
falseが返ってきてしまいます...
回答1件
あなたの回答
tips
プレビュー