public static function createUserFromFbToken($token) { $fbuser = App::getFbUserFromToken($token); if ($fbuser) { $f = $fbuser; $fb = App::getFb(); $r = $fb->get("/{$f['id']}/picture?type=square&width=500&height=500&redirect=0", $token); $fb_pic = $r->getGraphUser()['url']; $details = ["first_name" => "{$f['first_name']}", "last_name" => "{$f['last_name']}"]; if (isset($f['email'])) { $details["email"] = $f['email']; } $user_id = User::create($details); //print_r($f); if ($user_id) { $conn = Connection::getInstance("write"); $command = "INSERT INTO social_users (user_id, social_id, type, name) VALUES(\n\t\t\t\t\t\t{$user_id}, {$f['id']}, 'facebook', '{$f['name']}')"; $conn->execInsert($command); $u = new User($user_id); $u->setProfilePicture($fb_pic); return $user_id; } else { return false; } } }
$app->response->headers->set('Content-Type', 'application/json'); $app->add(new \AuthMiddleware()); $app->post('/authenticate', function () use($app) { $body = json_decode($app->request->getBody()); if ($id = App::Login($body->user, $body->password)) { $u = new User($id); $token = App::generateToken($id); echo json_encode(array("token" => "{$token}")); } else { $app->response()->status(404); } }); $app->post('/fb-token', function () use($app) { $b = json_decode($app->request->getBody()); $t = false; $fbuser = App::getFbUserFromToken($b->fb_access_token); if ($user_id = App::findBySocial('facebook', $fbuser['id'])) { $t = App::generateToken($user_id); } else { $user_id = App::createUserFromFbToken($b->fb_access_token); if ($user_id) { $t = App::generateToken($user_id); //echo json_encode(array("token" => $t)); } } //echo $t; if ($t) { echo json_encode(array("token" => "{$t}")); } else { echo json_encode(array("status" => "duplicate")); }