Good idea resetting the cookie when users log in, I think i'll use that, ta :)
On May 13, 11:31 pm, "dr. Hannibal Lecter" <[EMAIL PROTECTED]> wrote: > Here is my implementation in a form of a component: > > http://dsi.vozibrale.com/articles/view/rememberme-component-for-cakephp > > Basically the same functionality as yours (+ the extra "continuous > remembering"), but I don't have a working example with all the views > etc. > > HTH someone too ;-) > > On May 13, 12:42 pm, Tim W <[EMAIL PROTECTED]> wrote: > > > Cake 1.2 Auth is still pretty badly documented, but between a half > > dozen articles I managed to get it all working. None tell the full > > story though, and I don't fully understand it, but I have working code > > I thought i'd share. I may post a bakery article if I get time. For > > now this should help some people. My biggest tip is to get a decent > > IDE and step through the Cake core code if you want to know what's > > going on, I use Komodo, but i'm sure there are others too. Note that I > > don't use groups, as when I first started I couldn't get them working, > > and I don't need them any now. > > > NB: this code was taken from all over the place; I copied, pasted, and > > tweaked. If I could find the original articles i'd credit the authors, > > but they were found all over the place and I didn't save them. My > > apologies to the people I haven't credited. > > > I've removed some of the guff from my own app, so this should work, > > but you might need to tweak minor stuff. > > > Database table > > CREATE TABLE `users` ( > > `id` int(11) NOT NULL auto_increment, > > `username` varchar(40) NOT NULL, > > `password` varchar(60) NOT NULL, > > `state_id` int(11) NOT NULL default '11', > > `email` varchar(40) NOT NULL, > > `created` datetime NOT NULL, > > `modified` datetime NOT NULL, > > PRIMARY KEY (`id`), > > UNIQUE KEY `EMAIL` (`email`), > > UNIQUE KEY `activation_code` (`activation_code`), > > KEY `email_index` (`email`) > > ) > > > *** /views/users/login.ctp (seehttp://www.dynamicdrive.comforthe > > rollover button javascript code, article "OO Dom Image Rollover") > > > <h1>User Login</h1> > > <table class="whatever"> > > <tr> > > <th>Username</th> > > <td><?php echo $form->input('username'); ?></td> > > </tr> > > <tr> > > <th>Password</th> > > <td><?php echo $form->input('password'); ?></td> > > </tr> > > <tr> > > <th>Remember Me</th> > > <td><?php echo $form->input('remember_me', array('label' => > > 'Remember Me', 'type' => 'checkbox')); ?></td> > > </tr> > > <tr> > > <th> </th> > > <th align="right"><?php echo > > $form->submit('/images/login.png', > > array('srcover' => $this->base . '/images/login-over.png', 'srcdown' > > => $this->base . '/images/login-down.png', 'border' => '0'));?></th> > > </tr> > > </table> > > > <input type="hidden" name="XDEBUG_SESSION_START" value="tim" /> > > <?php echo $form->end(); ?> > > > *** /views/users/register.ctp > > > <h1>User Registration</h1> > > > <?php echo $form->create('User', array('action' => 'register'));?> > > > <table class="whatever"> > > <tr> > > <th>Username</th> > > <td><?php echo $form->input('username', > > array_merge($errorMessages, array('size'=>'20', 'div' => null))); > > ?> <span class="required_field">*</span></td> > > > </tr> > > <tr> > > <th>Email Address</th> > > <td><?php echo $form->input('email', array_merge($errorMessages, > > array('size'=>'30', 'div' => null))); ?> <span > > class="required_field">*</span><br /> > > Please note your confirmation email will be sent to this address</ > > td> > > </tr> > > <tr> > > <th>Password</th> > > <td><?php echo $form->input('password', array('type'=>'password', > > 'size'=>'15', 'div' => null)); ?> <span > > class="required_field">*</span></td> > > </tr> > > <tr> > > <th>Confirm Password</th> > > <td><?php echo $form->input('password2', array('type'=>'password', > > 'size'=>'15', 'div' => null)); ?> <span > > class="required_field">*</span></td> > > </tr> > > <tr> > > <th> </th> > > <th align="right"><?php echo $form->submit('/images/register.png', > > array('srcover' => $this->base . '/images/register-over.png', > > 'srcdown' => $this->base . '/images/register-down.png', 'border' => > > '0'));?></th> > > </tr> > > </table> > > > <input type="hidden" name="XDEBUG_SESSION_START" value="tim" /> > > <?php echo $form->end(); ?> > > > *** /controllers/login.php > > > <?php > > /** > > * User Controller class > > */ > > class UsersController extends AppController { > > > var $name = 'Users'; > > var $helpers = array('Html', 'Form', 'Session', 'Misc'); > > var $components = array('Auth', 'Email', 'Cookie'); > > var $uses = array('User', 'UserView'); > > > function index() { > > $this->redirect('/home'); > > } > > > /** > > * Login the user > > */ > > function login() { > > //-- code inside this function will execute only when autoRedirect > > was set to false (i.e. in a beforeFilter). > > > if ($this->RequestHandler->isPost()) { > > if ($this->Auth->user()) { // Does user/password checking > > if (!empty($this->data) && $this->data['User']['remember_me'] > > == '1') { > > // Save cookie only if checkbox ticked > > $cookie = array(); > > $cookie['username'] = $this->data['User']['username']; > > $cookie['password'] = $this->data['User']['password']; > > $this->Cookie->write('Auth.User', $cookie, true, '+2 > > weeks'); > > unset($this->data['User']['remember_me']); > > } > > > $this->redirect($this->Auth->redirect()); > > } else { > > $this->Session->setFlash('Invalid user or password'); > > } > > } > > } > > > /** > > * Log out user > > * > > */ > > function logout(){ > > $cookie = $this->Cookie->read('Auth.User'); > > $this->Cookie->del('Auth.User'); > > > $this->redirect($this->Auth->logout()); > > } > > > function register() { > > // Check if user already logged in > > $i = $this->getUserID(); > > if ($i != null && $i > 0) { > > $this->Session->setFlash('You\'re already logged in!'); > > $this->redirect('/home/index'); > > } > > > // Setup data for page > > $this->set('title', $this->appName .'User Registration'); > > $this->set('errorMessages', array('error' => > > array('username_size' => 'Your username must > > be between 3 and 10 characters long', > > 'password' => 'Your password must be > > between 4 and 10 characters long', > > 'email' => 'Please enter a valid email > > address', > > 'first_name' => 'name size is too > > short' > > ))); > > > if (empty($this->data)) { > > // Just show the form > > } else { > > // Need to encode password2 so it can be matched with password > > by validation, but need to keep the length > > // around to check as encoded password is always the same length > > $this->data['User']['password_length'] = > > strlen($this->data['User']['password2']); > > > $this->data['User']['password2'] = $this->Auth->password($this- > > > >data['User']['password2']); > > > // Attempt to register the user > > if ($this->User->save($this->data)) { > > $this->set('title', $this->appName .'User Registration > > Succesful'); > > $this->set('user', $this->data); > > $this->render('register_email_sent'); > > return; > > } else { > > $this->Session->setFlash('Please correct the errors > > highlighted below'); > > $this->data['User']['password'] = $this->data['User'] > > ['password2'] = ''; > > } > > } > > } > > > function beforeFilter() { > > $this->Auth->userScope = array('User.state_id' => '12'); > > $this->set('title', $this->appName .'User Login'); > > > parent::beforeFilter(); > > }} > > > ?> > > > /controllers/app_controller.php > > > <?php > > /** > > * Main App Controller File > > */ > > class AppController extends Controller { > > var $components = array('Auth','Cookie', 'Email'); > > var $helpers = array('Html', 'Javascript', 'Ajax'); > > > var $home = '/home/view/'; > > > /** > > * Load the Authentication > > * > > * @access public > > */ > > function beforeFilter(){ > > $this->Auth->loginAction = array('controller' => 'users', 'action' > > => 'login'); > > $this->Auth->loginRedirect = array('controller' => 'home'); > > $this->Auth->allow('index'); > > $this->Auth->authorize = 'controller'; > > > $this->Auth->autoRedirect = false; > > > if ($this->getUserID() == -1) { > > $this->loginFromCookie(); > > } > > } > > > function loginFromCookie() { > > $cookie = $this->Cookie->read('Auth.User'); > > if (!is_null($cookie)) { > > if ($this->Auth->login($cookie)) { > > // Clear auth message, just in case we use it. > > $this->Session->del('Message.auth'); > > //$this->redirect($this->Auth->redirect()); > > } else { // Delete invalid Cookie > > $this->Cookie->del('Auth.User'); > > } > > } > > } > > > function getUserID() { > > $usr = $this->Auth->user(); > > if (!empty($usr)) { > > return $usr['User']['id']; > > } else { > > return -1; > > } > > } > > > } > > > I hope it helps someone out :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
