hwi/oauth-bundle
Symfony bundle for OAuth1.0a/OAuth2 login and user authentication. Supports Symfony 6.4–8.0 (PHP 8.3+) and integrates dozens of providers (Google, GitHub, Facebook, Apple, LinkedIn, Azure, Keycloak, etc.).
# config/packages/hwi_oauth.yaml
hwi_oauth:
resource_owners:
facebook:
type: facebook
client_id: <client_id>
client_secret: <client_secret>
scope: "email"
options:
display: popup #dialog is optimized for popup window
services:
hwi_oauth.user.provider.entity:
class: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider
# app/config/routing.yml
hwi_oauth_redirect:
resource: "[@HWIOAuthBundle](https://github.com/HWIOAuthBundle)/Resources/config/routing/redirect.php"
prefix: /demo/secured/connect
facebook_login:
path: /demo/secured/login_facebook
# config/packages/security.yaml
firewalls:
# ...
main:
pattern: ^/demo/secured/
oauth:
resource_owners:
facebook: /demo/secured/login_facebook
login_path: /demo/secured/login
failure_path: /demo/secured/login
oauth_user_provider:
service: hwi_oauth.user.provider.entity
access_control:
# Turn on anonymous for testings need.
- { path: ^/login, roles: PUBLIC_ACCESS }
The following example bases also on the Facebook "Login with Javascript SDK" guide.
# templates/Secured/hello.html.twig
{% block content %}
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '12345678910', // App ID from the app dashboard
channelUrl : '//yourdomain.com/channel.html', // Channel file for x-domain comms
status : true, // Check Facebook Login status
xfbml : true // Look for social plugins on the page
});
};
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function fb_login() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
alert('Already connected, redirect to login page to create token.');
document.location = "{{ url("hwi_oauth_service_redirect", {service: "facebook"}) }}";
} else {
// not_authorized
FB.login(function(response) {
if (response.authResponse) {
document.location = "{{ url("hwi_oauth_service_redirect", {service: "facebook"}) }}";
} else {
alert('Cancelled.');
}
}, {scope: 'email'});
}
});
}
</script>
<h1 class="title">Hello {{ name }}!</h1>
<a href="{{ path('_demo_secured_hello_admin', { 'name': name }) }}">Hello resource secured for <strong>admin</strong> only.</a>
<p>
<a href="#" onclick="fb_login();">Facebook Connect Button (Dialog)</a>
</p>
{# Bonus: Show all available login link in HWIOAuthBundle #}
{% render(controller('HWI\\Bundle\\OAuthBundle\\Controller\\LoginController::connectAction')) %}
{% endblock %}
Make sure {scope: 'email'} is added as the second argument to FB.login. Or elsewhere, you would have to prompt the user with the authentication for the basic data, and then ask him again to accept that you need his email.
Open the browser and go to /demo/secured/hello/World to view and test the login button!
How can I help you explore Laravel packages today?