Created
November 15, 2015 23:41
-
-
Save UndergroundLabs/23a80b8bd673a59febfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace QuizApp\Routes; | |
use \Psr\Http\Message\ServerRequestInterface; | |
use \Psr\Http\Message\ResponseInterface; | |
use \Psr\Http\Message\RequestInterface; | |
$app->get('/login', function(ServerRequestInterface $req, ResponseInterface $resp) use ($config) { | |
if(array_key_exists('HTTP_REFERER', $req->getHeaders())) | |
{ | |
$referer = $req->getHeader('HTTP_REFERER'); | |
$host = parse_url($referer[0], PHP_URL_HOST); | |
$next = ($host == $config['site']['domain']) ? $referer[0] : $this->router->urlFor('index'); | |
} | |
else | |
{ | |
$next = $this->router->urlFor('index'); | |
} | |
$callback = 'http://' . $config['site']['domain'] . $this->router->pathFor('fb_callback'); | |
$redirect = $this->FBServices->login_url($config['facebook']['permissions'], $callback); | |
$this->flash->addMessage('next_page', $next); | |
return $resp->withStatus(301)->withHeader('location', $redirect); | |
})->setName('login')->add($is_logged_in); | |
/* | |
* This route is used by Facebook after the user accepts or declines the application request. | |
*/ | |
$app->get('/fb_callback', function(ServerRequestInterface $req, ResponseInterface $resp) { | |
try { | |
# Sets up the access token to make calls to graph API | |
$this->FBServices->set_token(); | |
# Get user information | |
$me = $this->FBServices->me(array('email', 'first_name', 'last_name', 'location', 'gender')); | |
} catch(\Facebook\Exceptions\FacebookResponseException $e) { | |
$this->logger->addInofo($e->getMessage()); | |
return $resp->withStatus(500) | |
->withHeader('Location', $this->router->urlFor('error')); | |
} catch(\Facebook\Exceptions\FacebookSDKException $e) { | |
$this->logger->addInfo($e->getMessage()); | |
return $resp->withStatus(500) | |
->withHeader('Location', $this->router->urlFor('error')); | |
} catch(\Exception $e) { | |
$this->logger->addInfo($e->getMessage()); | |
return $resp->withStatus(500) | |
->withHeader('Location', $this->router->urlFor('error')); | |
} | |
# Insert the user into the database. | |
$user = $this->UserServices->create($me->getId(), | |
$me->getFirstname(), | |
$me->getLastname(), | |
$me->getEmail(), | |
$me->getLocation(), | |
$me->getGender()); | |
# Create the user session, effectively, logging the user in. | |
$this->SessionServices->create_user_session($user->id, $me->getId()); | |
# If flash does not include next_page paramater, redirect to homepage | |
if(!$this->flash->getMessages()['next_page']) | |
return $resp->withStatus(302)->withHeader('Location', '/'); | |
# Redirect to last page the user was viewing | |
return $resp->withStatus(302)->withHeader('Location', $this->flash->getMessages()['next_page']); | |
})->setName('fb_callback'); | |
$app->get('/logout', function(ServerRequestInterface $req, ResponseInterface $resp) { | |
$this->SessionServices->destroy(); | |
return $resp->withRedirect($this->router->urlFor('index')); | |
})->setName('logout'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment