Skip to main content

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

  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.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(request.param('provider')).redirect()

    def handle_provider_callback(self, request: Request, socialite: Socialite):
        """Obtain the user information"""
        user = socialite.driver(request.param('provider')).user()
        # => print(user)
        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(f'/oauth/@provider/login', 'SocialAuthController@redirect_to_provider'),
        Get(f'/oauth/@provider/callback', 'SocialAuthController@handle_provider_callback'),
    ]),
]

Visit, http://localhost:8000/oauth/facebook/login/

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for masonite-socialite, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size masonite_socialite-0.0.2-py3-none-any.whl (10.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size masonite-socialite-0.0.2.tar.gz (7.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page