Merge pull request #19 from amalfra/dev

Connection to database failed. Ensure api is writable.
This commit is contained in:
Matt 2014-10-20 15:05:06 -04:00
commit 43075eec67
2 changed files with 28 additions and 25 deletions

12
api/api.php Normal file → Executable file
View File

@ -15,8 +15,16 @@ $app->response->headers->set('Content-Type', 'application/json');
$jsonResponse = new JsonResponse();
require_once('helpers.php'); // Must come after $jsonResponse exists.
R::setup('sqlite:taskboard.db');
createInitialUser();
// Catch Exception if connection to DB failed
try {
R::setup('sqlite:taskboard.db');
createInitialUser();
} catch(Exception $e) {
$app->response->setStatus(503);
$jsonResponse->message = 'Connection to Database failed. Ensure api is writable.';
$app->response->setBody($jsonResponse->asJson());
}
$app->notFound(function() use ($app, $jsonResponse) {
$app->response->setStatus(404);

41
api/userRoutes.php Normal file → Executable file
View File

@ -6,34 +6,29 @@ $app->post('/login', function() use ($app, $jsonResponse) {
$expires = ($data->rememberme)
? (2 * 7 * 24 * 60 * 60) /* Two weeks */
: (1.5 * 60 * 60) /* One and a half hours */;
try {
$lookup = R::findOne('user', ' username = ? ', [$data->username]);
$jsonResponse->message = 'Invalid username or password.';
$app->response->setStatus(401);
$lookup = R::findOne('user', ' username = ? ', [$data->username]);
if (null != $lookup) {
$hash = password_hash($data->password, PASSWORD_BCRYPT, array('salt' => $lookup->salt));
if ($lookup->password == $hash) {
if ($lookup->logins == 0 && $lookup->username == 'admin') {
$jsonResponse->addAlert('warning', "This is your first login, don't forget to change your password.");
$jsonResponse->addAlert('success', 'Go to Settings to add your first board.');
}
setUserToken($lookup, $expires);
$lookup->logins = $lookup->logins + 1;
$lookup->lastLogin = time();
R::store($lookup);
$jsonResponse->message = 'Invalid username or password.';
$app->response->setStatus(401);
logAction($lookup->username . ' logged in.', null, null);
$jsonResponse->message = 'Login successful.';
$jsonResponse->data = $lookup->token;
$app->response->setStatus(200);
if (null != $lookup) {
$hash = password_hash($data->password, PASSWORD_BCRYPT, array('salt' => $lookup->salt));
if ($lookup->password == $hash) {
if ($lookup->logins == 0 && $lookup->username == 'admin') {
$jsonResponse->addAlert('warning', "This is your first login, don't forget to change your password.");
$jsonResponse->addAlert('success', 'Go to Settings to add your first board.');
}
setUserToken($lookup, $expires);
$lookup->logins = $lookup->logins + 1;
$lookup->lastLogin = time();
R::store($lookup);
logAction($lookup->username . ' logged in.', null, null);
$jsonResponse->message = 'Login successful.';
$jsonResponse->data = $lookup->token;
$app->response->setStatus(200);
}
} catch (Exception $ex) {
}
if (!is_writable('taskboard.db')) {
$jsonResponse->message = 'The api directory is not writable.';
}
$app->response->setBody($jsonResponse->asJson());
});