diff --git a/classes/RedisModel.php b/classes/RedisModel.php index 4182061..31668e0 100644 --- a/classes/RedisModel.php +++ b/classes/RedisModel.php @@ -24,14 +24,24 @@ class RedisModel extends Object return strtolower(implode(':', $parts)); } + public function mappingKey($variable, $value) + { + return $this->key($variable, $value, 'uid'); + } + public function nextUID() { return $this->redis->incr($this->key('uid')); } - public function getUID($variable, $value) + public function setMapping($variable, $value, $uid) { - return $this->redis->get($this->key($variable, $value, 'uid')); + $this->redis->set($this->mappingKey($variable, $value), $uid); + } + + public function getMapping($variable, $value) + { + return $this->redis->get($this->mappingKey($variable, $value)); } public function __call($name, $arguments) diff --git a/modules/user/create.php b/modules/user/create.php index 59c0823..e308797 100644 --- a/modules/user/create.php +++ b/modules/user/create.php @@ -23,19 +23,12 @@ class user_create extends CustomModule { try { - $_POST = array( - 'email' => 'foo@bar.com', - 'username' => 'fubar', - 'password' => 'insecure123', - ); - - $user = new User(); // Checks if the email or username is already in use foreach (array('email', 'username') as $field) { - if ($user->getUID($field, $_POST[$field])) + if ($user->getMapping($field, $_POST[$field])) { return array('error' => 'The ' . $field . ' is already in use.'); } @@ -44,15 +37,25 @@ class user_create extends CustomModule // Grabs the next UID $uid = $user->nextUID(); + // Generates the auth token + $auth_token = sha1(mt_rand() . microtime()); + // Writes the user data - $user->set('1001', array( + $user->set($uid, array( 'username' => $_POST['username'], 'email' => $_POST['email'], 'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'), + 'auth' => $auth_token, )); - // Sets up the authentication cookie - //$user->setAuth() + // Sets the UID mappings + $user->setMapping('username', $_POST['username'], $uid); + $user->setMapping('email', $_POST['email'], $uid); + + // Sets a cookie with the UID and auth token + setcookie('auth', $uid . '|' . $auth_token, Time::YEAR); + + return array('status' => 'success', 'url' => '/'); } catch (RedisException $e) {