Skip to content

Instantly share code, notes, and snippets.

@kelunik

kelunik/Auth.php Secret

Created November 29, 2014 17:12
Show Gist options
  • Save kelunik/036d6b04f535db54fbe3 to your computer and use it in GitHub Desktop.
Save kelunik/036d6b04f535db54fbe3 to your computer and use it in GitHub Desktop.
<?php
class Auth {
private function getLoginData (GitHubAPI $api) {
$db = new Pool(DB_HOST, DB_USER, DB_PASS, DB_DB);
$q = (yield $db->prepare("SELECT `id`, `name`, `mail` FROM `users` WHERE `github_token` = ?"));
$q = (yield $q->execute([$api->getToken()]));
if(yield $q->rowCount()) {
yield (yield $q->fetchRow());
} else {
$mail = (yield $api->queryPrimaryMail());
if($mail == null) {
yield; return;
}
$q = (yield $db->prepare("SELECT `id`, `name`, `mail` FROM `users` WHERE `mail` = ?"));
$q = (yield $q->execute([$mail]));
if(yield $q->rowCount()) {
$stmt = (yield $db->prepare("UPDATE `users` SET `github_token` = ? WHERE `mail` = ?"));
$stmt = (yield $stmt->execute([$api->getToken(), $mail]));
yield (yield $q->fetchRow());
} else {
$username = (yield $api->queryUsername());
if($username == null) {
yield; return;
}
$q = (yield $db->prepare("INSERT INTO `users` (`name`, `mail`, `github_token`) VALUES (?, ?, ?)"));
$q = (yield $q->execute([$username, $mail, $api->getToken()]));
if($q) {
yield [$q->insertId, $username, $mail];
} else {
error_log("Couldn't insert new user: {$username} / {$mail}");
yield; return;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment