Skip to content

Commit

Permalink
Fix kiswa#68. Now possible to be logged in from many browsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiswa committed Mar 20, 2015
1 parent c346026 commit 96fe66f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
33 changes: 27 additions & 6 deletions api/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ function setUserToken($user, $expires) {
'uid' => $user->id
), getJwtKey());

// Store the valid token in the user db
$user->token = $token;
$dbToken = R::dispense('token');
$dbToken->token = $token;

if (null == $user->ownToken) {
$user->ownToken = [];
}
$user->ownToken[] = $dbToken;

R::store($user);
}

Expand Down Expand Up @@ -215,7 +221,7 @@ function loadBoardData($board, $data) {
// Clean a user bean for return to front-end.
function sanitize($user) {
$user['salt'] = null;
$user['token'] = null;
$user['ownToken'] = null;
$user['password'] = null;
}

Expand Down Expand Up @@ -262,13 +268,21 @@ function validateToken($requireAdmin = false) {
// Retrieve user's token from DB and compare to header token.
function checkDbToken() {
$user = getUser();
$isValid = false;

if (null != $user) {
if (isset(getallheaders()['Authorization'])) {
$hash = getallheaders()['Authorization'];
return $hash == $user->token;

foreach ($user->ownToken as $token) {
if ($hash == $token->token) {
$isValid = true;
}
}
}
}
return false;

return $isValid;
}

// Clear a user's token from the DB.
Expand All @@ -282,7 +296,14 @@ function clearDbToken() {
if (null != $payload) {
$user = R::load('user', $payload->uid);
if (0 != $user->id) {
$user->token = null;
$hash = getallheaders()['Authorization'];

foreach ($user->ownToken as $token) {
if ($hash == $token->token) {
R::trash($token);
}
}

R::store($user);
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/userRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

logAction($lookup->username . ' logged in.', null, null);
$jsonResponse->message = 'Login successful.';
$jsonResponse->data = $lookup->token;
$jsonResponse->data = R::findOne('token', ' user_id = ? ORDER BY id DESC ', [$lookup->id])->token;
$app->response->setStatus(200);
}
}
Expand Down

0 comments on commit 96fe66f

Please sign in to comment.