<?php $state = urlencode(Lib::getCurrentUrl()); $auth = new FacebookAuth(Config::get('FACEBOOK_APP_ID'), Config::get('FACEBOOK_APP_SECRET'), Config::get('FACEBOOK_REDIRECT_URI'), Config::get('FACEBOOK_SCOPES'), $state); ?> <a href="<?php echo $auth->getAuthLink(); ?> " component="LoginWithFacebook"> <span class="icon"></span> <span class="text">[[COMPONENT name=Label text='Login with Facebook' id=login-with-facebook]]</span> </a>
<?php $redirect = function () { if (array_key_exists('state', $_GET)) { header('Location: ' . $_GET['state']); } else { header('Location: /'); } }; $auth = new FacebookAuth(Config::get('FACEBOOK_APP_ID'), Config::get('FACEBOOK_APP_SECRET'), Config::get('FACEBOOK_REDIRECT_URI'), Config::get('FACEBOOK_SCOPES'), $state); $info = $auth->getUserInfo($_GET['code']); if (null === $info) { // Access denied $redirect(); } else { // Access granted $email = $info['email']; $nick = $info['name']; $picture = $info['picture']; $user = Users::getByEmail($email); if (null == $user) { $user = Users::add($email); $user->setNick($nick); $user->setPicture(Image::INSERT($picture)); } $user->login(); $redirect(); }