Giter Site home page Giter Site logo

masonite-socialite's Introduction

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@redirect_to_provider'),
    Get('auth/@provider/callback', 'SocialAuthController@handle_provider_callback'),
]

Controllers

You can access Socialite using the Socialite helper:

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):
        """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 [email protected] .

masonite-socialite's People

Contributors

corentinalcoy avatar nioperas06 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.