Skip to content

Instantly share code, notes, and snippets.

@jdp
Created June 27, 2009 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdp/136935 to your computer and use it in GitHub Desktop.
Save jdp/136935 to your computer and use it in GitHub Desktop.
<?php
if ($session->check('Message.auth')) {
$session->flash('auth');
}
echo $form->create('User', array('action'=>'login'));
echo $form->input('email');
echo $form->input('password');
echo $form->end('Login');
?>
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = 'Playlist';
var $validate = array(
'id' => array(
'rule' => 'blank',
'on' => 'create'
),
'email' => array(
'cuz-its-required' => array(
'rule' => 'email',
'required' => true,
'message' => 'Please enter a valid email'
),
'steal-an-email' => array(
'rule' => 'isUnique',
'message' => 'Somebody is already using that email',
'on' => 'create'
)
),
// passwd is the name of the form element
// it gets hashed and put into password db field
'passwd' => array(
'rule' => array('confirmPassword', 'password'),
'message' => 'Passwords do not match',
'on' => 'create'
),
'password_confirm' => array(
'rule' => 'alphanumeric',
'required' => true
)
);
/* Makes more sense than doing it in controller */
function confirmPassword($data) {
return ($data['passwd'] == $this->data['User']['password_confirm']);
}
/* Override for Auth's hashPassword function, to work w/ old users */
function hashPasswords($data) {
$data[$this->alias]['password'] = md5($data[$this->alias]['password']);
return $data;
}
}
?>
<?php
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form');
var $components = array('Auth');
function beforeFilter() {
//$this->Auth->authenticate = ClassRegistry::init('User');
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
$this->Auth->allow('signup');
}
function dashboard() {
}
function login() {
}
function signup() {
if (!empty($this->data)) {
$this->data['User']['password'] = md5($this->data['User']['passwd']);
$this->User->create($this->data);
if ($this->User->save()) {
// Auth::login doesn't pay attention to Auth->fields
$this->data['User']['username'] = $this->data['User']['email'];
$login_result = $this->Auth->login($this->data);
$this->Session->del('Message.auth');
//$this->Session->setFlash("Signup successful!");
//$this->redirect($this->Auth->redirect());
}
}
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment