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 = ''
SOCIAL_AUTH_FACEBOOK_REDIRECT_URI = ''
# Twitter
SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''
SOCIAL_AUTH_TWITTER_REDIRECT_URI = ''
# Google
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_REDIRECT_URI = ''
# Github
SOCIAL_AUTH_GITHUB_KEY = ''
SOCIAL_AUTH_GITHUB_SECRET = ''
SOCIAL_AUTH_GITHUB_REDIRECT_URI = ''
# LinkedIn
SOCIAL_AUTH_LINKEDIN_KEY = ''
SOCIAL_AUTH_LINKEDIN_SECRET = ''
SOCIAL_AUTH_LINKEDIN_OAUTH2_REDIRECT_URI = ''
- In your controller,
SocialAuthController
for example, put the following code:
from masonite.auth import Auth
from masonite.controllers import Controller
from masonite.request import Request
from app.User import User
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(request.param('provider')).redirect()
def handle_provider_callback(self, request: Request, socialite: Socialite, auth: Auth):
"""Obtain the user information"""
user_info = socialite.driver(request.param('provider')).user()
user = User.first_or_create(
email=user_info.email,
name=user_info.username,
access_token=user_info.access_token,
provider=user_info.provider)
auth.once().login_by_id(user.id)
return request.redirect('/home')
The request.param('provider')
represents the name of the provider.
- You need to define the routes:
from masonite.routes import Get, RouteGroup
ROUTES = [
RouteGroup([
Get('/oauth/@provider/login', 'SocialAuthController@redirect_to_provider'),
Get('/oauth/@provider/callback', 'SocialAuthController@handle_provider_callback'),
]),
]
Visit, http://localhost:8000/oauth/facebook/login/
Models
You can now retrieve information from the provider by using the api provider wrapper.
- Modify your model and add the api function
"""User Model."""
from config.database import Model
from socialite.api import ProviderAPI
class User(Model):
"""User Model."""
__fillable__ = ['name', 'email', 'password', 'provider', 'access_token']
__auth__ = 'email'
@property
def api(self):
return ProviderAPI(self.provider, access_token=self.access_token)
- Use the api wrapper with facebook
from masonite.controllers import Controller
from masonite.request import Request
from masonite.view import View
class InfoWelcomeController(Controller):
"""Controller For Welcoming The User."""
def show(self, view: View, request: Request):
user = request.user()
user.api.get("me").json()
return view.render('welcome')
List of supported providers
- Github
- Gitlab
- Bitbucket
- Trello
- Slack
- Dropbox
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
Hashes for masonite_socialite-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f364be5a2dead9413015cf7baf9d17ce7a7d2e2bfa8c0d66f8cdf8506ee7dcce |
|
MD5 | 0f3173c4231692257a3434e61ca8b3a0 |
|
BLAKE2b-256 | 96057ed813c8f97cc3fa815ccc489cc825c88245412813ccf9ca7564eabf0f93 |