Skip to main content

Flexible Social Authentication for Masonite Framework

Project description

Masonite Socialite

Build Status GitHub license Twitter

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

  1. 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 = ''
  1. 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.

  1. 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.

  1. 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', 'resource_owner_key', 'resource_owner_secret']

    __auth__ = 'email'

    @property
    def api(self):
        return ProviderAPI(self.provider, access_token=self.access_token, 
        resource_owner_key=self.resource_owner_key, resource_owner_secret=self.resource_owner_secret)

NB

If you are not using the providers which support the oauth1 you don't need to pass the resource_owner_key, resource_owner_secret. 2. 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
  • Facebook
  • Twitter
  • Google
  • Linkedin
  • Gitlab
  • Bitbucket
  • Trello
  • Slack
  • Instagram
  • Dropbox
  • Pinterest

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

masonite-socialite-0.0.5.tar.gz (9.4 kB view hashes)

Uploaded Source

Built Distribution

masonite_socialite-0.0.5-py3-none-any.whl (11.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page