Flexible Social Authentication for Masonite Framework
Project description
Masonite Socialite
Flexible Social Authentication for Masonite Framework
Masonite Socialite is an authentication package for Masonite Framework. Extremely flexible and modular, Masonite Socialite supports authentication with Facebook, Twitter, Github, LinkedIn, Google and more.
Here's a demo of how you can use it:
Installation
You can install the package via pip
:
pip install masonite-socialite
Configuration
Add the Service Provider to your provider list in config/providers.py
:
from socialite.providers import SocialiteProvider
PROVIDERS = [
...
# Third Party Providers
SocialiteProvider,
]
This will add a new socialite:install command to craft. Just run:
craft socialite:install
Usage
- Configure your OAuth keys for the provider you want to use in your
.env
file.
# Facebook
SOCIAL_AUTH_FACEBOOK_KEY = ''
SOCIAL_AUTH_FACEBOOK_SECRET = ''
# Twitter
SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''
# Google
SOCIAL_AUTH_GOOGLE_OAUTH_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH_SECRET = ''
# Github
SOCIAL_AUTH_GITHUB_KEY = ''
SOCIAL_AUTH_GITHUB_SECRET = ''
# LinkedIn
SOCIAL_AUTH_LINKEDIN_KEY = ''
SOCIAL_AUTH_LINKEDIN_SECRET = ''
- In your controller,
SocialAuthController
for example, put the following code:
from masonite.controllers import Controller
from masonite.request import Request
from socialite import Socialite
class SocialAuthController(Controller):
"""Controller For Social Authentication."""
def redirect_to_provider(self, request: Request, socialite: Socialite):
"""Redirect the user to the authentication page"""
return socialite.driver('auth').redirect()
def handle_provider_callback(self, request: Request, socialite: Socialite):
"""Obtain the user information"""
user = socialite.driver('auth').user()
# => print(user)
return request.redirect('/home')
- You need to define the routes:
from masonite.routes import Get, RouteGroup, Post
from config.socialite import SOCIAL_AUTH_PREFIX
ROUTES = [
....
RouteGroup([
Get(f'/{SOCIAL_AUTH_PREFIX}/@backend/login', 'SocialAuthController@redirect_to_provider'),
Get(f'/{SOCIAL_AUTH_PREFIX}/@backend/callback', 'SocialAuthController@handle_provider_callback'),
], middleware=('socialite.backend', )),
....
]
The uri routes need to be started by SOCIAL_AUTH_PREFIX Without that your callback may be wrong.
SOCIAL_AUTH_NAMESPACE = "social"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for masonite_socialite-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f260a2e9b05024a7b9ba61d3c470b85277c641811e6049adbce39638525e5bc4 |
|
MD5 | 5efd8f3509d9116057adc706668d5ef0 |
|
BLAKE2b-256 | 443a70a837bbeffa369528ac5529ddca5b969c1ef3d72bcfdf448b5d0c27a03b |