Skip to main content

Flexible Social Authentication for Masonite Framework

Project description

Masonite Socialite

Flexible Social Authentication for Masonite Framework

Build Status GitHub license Twitter

Table of Contents

Example

class SocialAuthController(Controller):
    """SocialAuthController Controller Class."""

    def login(self, socialite: Socialite):
        return socialite.driver('github').redirect()

    def callback(self, view: View, request: Request, socialite: Socialite):
        user = socialite.driver('github').user()
        # Your application logic here.

Installation

# Using pip
$ pip install masonite-socialite

# Using Pipenv
$ pipenv install masonite-socialite

# Using Poetry
$ poetry add masonite-socialite

Add SocialiteProvider to your providers 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. Then run in your terminal:

craft socialite:install

Configuration

Before using Socialite, you need to get credentials from the provider(s) you want to use. Depending on the providers your application requires, you'll put the right credentials in the .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 = ''

If you don't know how to get those values, you can find a detailed guide here.

Routing

You need two routes: one for redirecting the user to the appropriate OAuth provider, and another for receiving the callback from the provider after authentication.

"""Web Routes."""

from masonite.routes import Get

ROUTES = [
    Get('auth/@provider', 'SocialAuthController@login'),
    Get('auth/@provider/callback', 'SocialAuthController@callback'),
]

Controllers

You can access Socialite using the Socialite helper:

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

    def handle_provider_callback(self, request: Request, socialite: Socialite, auth: Auth):
        """Obtain the user information"""
        user = socialite.driver(request.provider).user()
        # => print(user)
        return request.redirect('/home')

Providers

  • Github
  • Facebook
  • Twitter
  • Google
  • Linkedin
  • Gitlab

We are accepting new providers. Send new provider pull requests. You can follow this tutorial to add a new provider.

Support

Masonite Socialite appreciates help from a wide range of different backgrounds. Small improvements or fixes are always appreciated and issues labeled as easy may be a good starting point. If you are considering larger contributions outside the traditional coding work, please contact us through hellomasonite@gmail.com .

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

Uploaded Source

File details

Details for the file masonite-socialite-0.0.6.tar.gz.

File metadata

  • Download URL: masonite-socialite-0.0.6.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.3.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1

File hashes

Hashes for masonite-socialite-0.0.6.tar.gz
Algorithm Hash digest
SHA256 320e6db9d6b3206405d81ff972498aae54266d4dc8ce3e7212bf9d9a93b6075b
MD5 04cd6439ef097944dee0c7d5c8f3b95c
BLAKE2b-256 283d540d6257241f9e23c598f31057fd11d6cf97b75b20e768426cf224236265

See more details on using hashes here.

Supported by

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