2016-10-14 1 views
0

Problem bezüglich Anmeldung mit Auth in cakephp3Einloggen mit anderen Bereichen mit Auth in cakephp3

$this->loadComponent('Auth', [ 
      'loginRedirect' => [ 
       'controller' => 'Articles', 
       'action' => 'index' 
      ], 
      'logoutRedirect' => [ 
       'controller' => 'Pages', 
       'action' => 'display', 
       'home' 
      ] 
     ]); 

es erlaubt mir nur Benutzername standardmäßig zu verwenden, okay, wenn ich wollte, mit E-Mail anmelden, dass durch Ich habe gesucht und das hat: UserController.php

public function login() 
    { 
     if ($this->request->is('post')) 
     { 
      $this->Auth->config('authenticate', [ 
        'Form' => [ 
         'fields' => ['username' => 'email'] 
        ] 
       ]); 
      $this->Auth->constructAuthenticate(); 
      $this->request->data['email'] = $this->request->data['username']; 
      unset($this->request->data['username']); 
      $user = $this->Auth->identify(); 
      if ($user) 
      { 
       $this->Auth->setUser($user); 
       return $this->redirect($this->Auth->redirectUrl()); 
      } 
      $this->Flash->error(__('Invalid username or password, try again')); 
     } 
    } 

I Felder von Benutzernamen ändern können per E-Mail und rekonstruieren sie aber was, wenn ich mit ID Feld anmelden wollten.

$this->Auth->config('authenticate', [ 
        'Form' => [ 
         'fields' => ['username' => 'id'] 
        ] 
       ]); 

Wenn ich von E-Mail zu ID ändere, kann ich mich nicht anmelden. Muss ich dann Abfragen verwenden?

Antwort

1

Ich hatte gerade das gleiche Problem und lösen es so CakePHP 3.3

Ihre AppController

$this->loadComponent('Auth', [ 
    'authenticate' => [ 
     'Form' => [ 
      'fields' => [ 
       'username' => 'email', 
      ] 
     ] 
    ], 
    'loginAction' => [ 
     'controller' => 'Users', 
     'action' => 'login' 
    ], 
    'unauthorizedRedirect' => $this->referer(), 
    'loginRedirect' => [ 
     'plugin' => false, 
     'controller' => 'Pages', 
     'action' => 'home', 
    ], 
]); 

Ihre Usercontroller (Login Aktion)

/** 
* @return Response|null 
*/ 
public function login() 
{ 
    if ($this->request->is('post')) { 
     $user = $this->Auth->identify(); 
     if ($user) { 
      $this->Auth->setUser($user); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } 
     $this->Flash->error('Your username or password is incorrect.'); 
    } 
} 

Ihre grundlegende Benutzer/Login verwenden. ctp Vorlage

<?= $this->Form->create() ?> 
<?= $this->Form->input('email') ?> 
<?= $this->Form->input('password') ?> 
<?= $this->Form->button('Login') ?> 
<?= $this->Form->end() ?> 
1

Ihr Problem ist, wenn Sie die E-Mail-'Feld ‚id‘ in dem unten stehenden Code geändert,

`('authenticate', ['Form' => ['fields' => ['username' => 'id']]]);` 

Sie das Eingabefeld Ihrer Ansicht ‚login.ctp‘ geändert haben sollten.

hieraus: <?= $this->Form->input('email') ?>

dazu: <?= $this->Form->input('id') ?>