Hey guys!
I'm new to CakePHP, I've just tried it out for a few weeks. I have
encountered a problem going through the Authentication tutorial
(http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-
example/auth.html). I have done the blog tutorial and then continued
with this.
Creating users seem to work fine, at least they show up in the
database. But when I try to log in with my newly created user, nothing
happens. The login page just reloads. No flash message, no errors.
(And I can't use the posts/add/ function, so I am not logged in.)
What could be the problem? I have searched for others with the same
problem but I have not found a solution that worked. I don't know how
much code you need to help me, please tell me and I will post.
Here is my AppController
<?php
// app/Controller/AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action'
=> 'index'),
'logoutRedirect' => array('controller' => 'users',
'action' => 'login'),
'authorize' => array('Controller')
)
);
function beforeFilter() {
$this->Auth->allow('index', 'view');
}
public function isAuthorized($user) {
if (isset($user['role']) && $user['role'] === 'admin') {
return true; //Admin can access every action
}
return false; // The rest don't
}
}
And my UsersController
<?php
// app/Controller/UsersController.php
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
//Actions allowed to use without logging in
$this->Auth->allow('add', 'logout');
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Ogiltig användare'));
}
$this->set('user', $this->User->read(null, $id));
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Användaren har
sparats'));
$this->redirect(array('controller' => 'posts',
'action' => 'index'));
} else {
$this->Session->setFlash(__('Användaren kunde inte
sparas. Försök igen.'));
}
}
}
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Användaren har
sparats'));
$this->redirect(array('controller' => 'posts',
'action' => 'index'));
} else {
$this->Session->setFlash(__('Användaren kunde inte
sparas. Försök igen.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->User->delete()) {
$this->Session->setFlash(__('Användaren raderad'));
$this->redirect(array('controller' => 'posts', 'action' =>
'index'));
}
$this->Session->setFlash(__('Användaren kunde inte raderas'));
$this->redirect(array('controller' => 'posts', 'action' =>
'index'));
}
public function login() {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Användarnamnet eller lösenordet
var felaktigt. Försök igen.'));
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
I really don't think I changed anything from the tutorial, except the
flash messages which I translated to Swedish.
I would really appreciate the help!
/Ella Källman, Sweden
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php