## 現状
現在、CakePHPの認証機能を使用して、ログイン機能を作成しました。
また、新規登録機能も作成しました。
下記がそのコードになります。
LoginController
1class LoginController extends AppController 2{ 3 public function initialize() 4 { 5 parent::initialize(); 6 } 7 8 public function login() 9 { 10 if ($this->request->is('post')) { 11 // POSTデータの場合、リクエスト情報を使用してユーザーの識別 12 $user = $this->Auth->identify(); 13 14 if ($user) { 15 // 認証した場合、ユーザー情報を保存 16 $this->Auth->setUser($user); 17 return $this->redirect($this->Auth->redirectUrl()); 18 } else { 19 // 認証できなかった場合、エラーメッセージ表示 20 $this->Flash->error('メールアドレス、またはパスワードが不正です。'); 21 } 22 } 23 24 $this->render($this->request->action, 'default'); 25 } 26 27 public function logout() 28 { 29 // sessionの削除 30 $this->request->session()->destroy(); 31 } 32}
AppController
1class AppController extends Controller 2{ 3 4 /** 5 * Initialization hook method. 6 * 7 * Use this method to add common initialization code like loading components. 8 * 9 * e.g. `$this->loadComponent('Security');` 10 * 11 * @return void 12 */ 13 public function initialize() 14 { 15 parent::initialize(); 16 17 $this->loadComponent('RequestHandler'); 18 $this->loadComponent('Flash'); 19 20 // 認証 21 $this->loadComponent('Auth', [ 22 // ログイン時のアクション 23 'loginAction' => [ 24 'controller' => 'Login', 25 'action' => 'login' 26 ], 27 // エラーメッセージ 28 'authError' => '入力内容が間違っています', 29 'authenticate' => [ 30 'Form' => [ 31 'fields' => [ 32 'username' => 'email', 33 'password' => 'password_hash' 34 ] 35 ], 36 ], 37 // セッションに格納 38 'storage' => 'Session' 39 ]); 40 41 $authenticateUser = ''; 42 // ログインユーザーか常に確認する 43 if (!empty($this->Auth->user())) { 44 $authenticateUser = $this->Auth->user(); 45 } 46 $this->set(compact('authenticateUser')); 47 } 48 49 /** 50 * Before render callback. 51 * 52 * @param \Cake\Event\Event $event The beforeRender event. 53 * @return void 54 */ 55 public function beforeRender(Event $event) 56 { 57 if (!array_key_exists('_serialize', $this->viewVars) && 58 in_array($this->response->type(), ['application/json', 'application/xml']) 59 ) { 60 $this->set('_serialize', true); 61 } 62 } 63} 64
UsersController
1class UsersController extends AppController 2{ 3 public function initialize() 4 { 5 parent::initialize(); 6 } 7 8 public function index() 9 { 10 $user = $this->Users->newEntity(); 11 $this->set(compact('user')); 12 13 if ($this->request->is('post')) { 14 // POSTされたらバリデーションを実施 15 // Entity作成 16 $validateUser = $this->Users->newEntity($this->request->data, [ 17 'validate' => 'register' 18 ]); 19 20 if ($validateUser->errors()) { 21 // バリデーションに引っかかった場合 22 $this->Flash->error('入力内容に不備があります'); 23 $this->set('user', $validateUser); 24 return $this->render($this->request->action, 'default'); 25 } 26 27 // 入力内容をセッションに格納 28 $this->request->session()->write('registerUser', $validateUser); 29 30 // 確認画面へ 31 return $this->redirect('/Users/confirm'); 32 } 33 34 if ($this->request->session()->check('registerUser')) { 35 // セッションに値が存在する場合は読み込む 36 $user = $this->request->session()->read('registerUser'); 37 } 38 39 // セッションに格納 40 $this->request->session()->write('registerUser', $user); 41 42 // GETされた時は入力画面へ 43 $this->render($this->request->action, 'default'); 44 return; 45 } 46 47 public function confirm() 48 { 49 // 入力内容をセッションから取得 50 $user = $this->request->session()->read('registerUser'); 51 52 if (!$user) { 53 // セッションから情報を取得できなかった場合 54 throw new BadRequestException('セッションの情報を取得できませんでした'); 55 return; 56 } 57 58 $this->set(compact('user')); 59 $this->render($this->request->action, 'default'); 60 } 61 62 public function save() 63 { 64 // 入力内容をセッションから取得 65 $user = $this->request->session()->consume('registerUser'); 66 67 if (!$user) { 68 // セッションから情報を取得できなかった場合 69 throw new BadRequestException('セッションの情報を取得できませんでした'); 70 return; 71 } 72 73 $user->password_hash = $user['password']; 74 75 if (!$this->Users->save($user)) { 76 // DBに格納できなかった場合 77 return $this->Flash->error('入力内容を保存できませんでした'); 78 } 79 80 // 登録内容でそのままログイン 81 $this->Auth->setUser($user->toArray()); 82 $this->redirect('/Users/complete'); 83 } 84 85 public function complete() 86 { 87 // ログインユーザー情報の取得 88 $authUser = $this->Auth->user(); 89 90 if (empty($authUser)) { 91 // ログインユーザーの取得ができなかった場合 92 throw new BadRequestException('登録情報の取得ができませんでした'); 93 return; 94 } 95 96 $this->render($this->request->action, 'default'); 97 } 98}
UsersTable
1class UsersTable extends Table 2{ 3 public function validationRegister(Validator $validation) 4 { 5 // validation of name column 6 $validation->notEmpty('name', '名前は必須項目です'); 7 8 // validation of password_hash column 9 $validation 10 ->notEmpty('password_hash', 'パスワードは必須項目です') 11 ->add('password_hash', [ 12 'length' => [ 13 'rule' => ['minlength', 7], 14 'message' => 'パスワードは7文字以上で入力して下さい' 15 ], 16 'alphaNumeric' => [ 17 'rule' => function ($value, $context) { 18 return (bool) preg_match('/^[a-zA-Z0-9]+$/', $value); 19 }, 20 'message' => 'パスワードは半角英数字で入力して下さい' 21 ] 22 ]); 23 24 // validation of tell column 25 $validation 26 ->notEmpty('tell', '電話番号は必須項目です') 27 ->add('tell', [ 28 'alphaNumeric' => [ 29 'rule' => function ($value, $context) { 30 return (bool) preg_match('/^[0-9]+$/', $value); 31 }, 32 'message' => '数字のみで入力して下さい' 33 ] 34 ]); 35 36 // validation of email column 37 $validation 38 ->notEmpty('email', 'メールアドレスは必須項目です') 39 ->add('email', [ 40 'validFormat' => [ 41 'rule' => 'email', 42 'message' => 'メールアドレスが正しくありません' 43 ], 44 'isUnique' => [ 45 'rule' => 'email', 46 'message' => '既に登録されているメールアドレスです' 47 ] 48 ]); 49 50 // validation of postal_code column 51 $validation 52 ->notEmpty('postal_code', '郵便番号は必須項目です') 53 ->add('postal_code', 'custom', [ 54 'rule' => function ($value, $context) { 55 return (bool) preg_match('/^[0-9]{3}[0-9]{4}+$/', $value); 56 }, 57 'message' => '郵便番号が正しくありません' 58 ]); 59 60 // validation of address column 61 $validation 62 ->notEmpty('address', '住所は必須項目です'); 63 64 return $validation; 65 } 66}
※文字数の関係からビューファイルは掲載しておりません。
現在の挙動として、ログインしていないと、別ページに遷移できず、ログインページにリダイレクトする形になっています。
やりたい事
やりたい事としては、ログインしていなくても、新規登録ページには遷移できる
ようにしたいです。
そもそもなのですが、現在の挙動として、なぜログインしていないと別ページに遷移しようとした時に、リダイレクトするのか、がわかりません。
AppControllerに記載した、Authのデフォルトの設定がそうなっているのでしょうか?
疑問は色々ありますが、ログインしていなくても新規登録ページには遷移できる
ようになればとりあえずの解決となるので、教えて頂きたいです。
お願い致します。
回答1件
あなたの回答
tips
プレビュー