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

# 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 = ''
  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('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')
  1. 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"

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

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.1.tar.gz (7.4 kB view hashes)

Uploaded Source

Built Distribution

masonite_socialite-0.0.1-py3-none-any.whl (10.4 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