[Ivan Diaz] - Add working login response

This commit is contained in:
Ivan Diaz 2015-11-01 18:43:35 -03:00
parent 001427008b
commit 504c4227b8
4 changed files with 98 additions and 6 deletions

View File

@ -5,9 +5,31 @@ $app->group('/user', function () use ($app) {
echo "Returns the user with $by = $value as a json";
});
$app->post('/login', function () use ($app) {
$app->get('/add/:user/:pass', function ($user, $pass) use ($app) {
// $app->response()->setStatus(400);
$app->response()->setBody('{ "response": true }');
$userInstance = new User();
$userInstance->setProperties(array(
'user' => $user,
'password' => $pass,
'admin' => 0
));
$id = $userInstance->store();
$app->response()->setBody("{ \"id\": $id }");
});
$app->post('/login', function () use ($app) {
$user = $app->request()->post('email');
$password = $app->request()->post('password');
$pass = '';
if ($userInstance = User::getUser($user, 'user')) {
$pass = $userInstance->password;
}
if ($pass === $password) {
$app->response()->setBody("{ \"response\": \"OK\" }");
}
else {
$app->response()->setBody("{ \"response\": \"FAIL\" }");
}
});
$app->post('/add', function () use ($app) {

View File

@ -9,6 +9,10 @@ RedBean::setup('mysql:host='. $mysql_host .';dbname=' . $mysql_database, $mysql_
// SLIM FRAMEWORK
\Slim\Slim::registerAutoLoader();
$app = new \Slim\Slim();
$app->config('debug', true);
$app->error(function (\Exception $e) use ($app) {
echo "error";
});
// LOAD MODELS
spl_autoload_register(function ($class) {

View File

@ -1,11 +1,77 @@
<?php
use RedBeanPHP\Facade as RedBean;
class User {
const PROPERTIES = array(
'user',
'password',
'admin',
);
const ERRORS = array(
'UNDEFINED_CREDENTIALS' => 'User or password is not defined'
);
private $_id;
private $_user;
private function __construct($id = 0) {
$this->_id = $id;
$this->_user = RedBean::load('users', $id);
public static function getUser($value, $property = 'id') {
if ($property === 'id') {
$mapValue = 'id=:value';
}
else if ($property === 'user') {
$mapValue = 'user=:value';
}
$user = RedBean::findOne('users', $mapValue, array(':value' => $value));
return ($user) ? new User($user) : null;
}
public static function deleteUser($user) {
if ($user instanceof User) {
RedBean::trash($user);
unset($user);
return true;
}
else {
return false;
}
}
public function __construct($user = null) {
if ($user) {
$this->_user = $user;
}
else {
//echo RedBean;
$this->_user = RedBean::dispense('users');
$this->setDefaultProperties();
}
}
public function setDefaultProperties() {
}
public function setProperties($properties) {
foreach (self::PROPERTIES as $PROP) {
$this->_user[$PROP] = $properties[$PROP];
}
}
public function __get($name) {
if ($this->_user[$name]) {
return $this->_user[$name];
}
else {
return null;
}
}
public function store() {
return RedBean::store($this->_user);
}
public function showUserDetails() {

View File

@ -15,7 +15,7 @@ var UserStore = Reflux.createStore({
},
loginUser(loginData) {
APIUtils.post('user/login').then(result => {
APIUtils.post('user/login', loginData).then(result => {
console.log(result);
});
}