Skip to main content

A simple Flask library to make login management easier.

Project description

Login.py

Login.py is a simple library for Flask that makes you able to handle login easily without getting plenty of useless lines of code.

Installation

pip3 install login.py

Usage

You can find the full demo of login.py here. You can also find the source code of login.py here.

initLogin(app, db)

This function is used to create the User class. Having this function is necessary for the rest of the program. Without this, the Users cannot get stored into the database.

from flask import Flask, request
from flask_login import UserMixin
from flask_sqlalchemy import SQLAlchemy
from login import *

# Create the "app" and "db" variables
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'aghzi vnguierhtrutizo hard to guess indeeed'
db = SQLAlchemy(app)

# Create the User class and add it to the database
User = initLogin(app, db)
db.create_all()

createUser(username, password, db, User)

This function is used to sign up a new user into the database. This is an example of usage:

@app.route('/register/post', methods=['POST'])
def register():
    try:
        createUser(request.form['username'], request.form['password'], db, User)
        return "New user created you are now logged in as " + current_user.username
    except: 
    		return "This username is already taken"

loginUser(username, password, User)

This function is used to login a user, that means that the user must already be in the database.

@app.route('/login/post', methods=['POST'])
def login():
    try:
        loginUser(request.form['username'], request.form['password'], User)
        return "You are now logged in as " + current_user.username
    except: 
    		return "Invalid username or password"

logout_user()

This is not a function from login.py, but a function from flask_login. The usage of this function is very simple:

@app.route('/logout')
def logout(): 
    logout_user()
    return "You are now logged out."

@login_required

When you add this line under a url route, it locks the page to the logged in users. The users that are not logged will have a Unauthorized error showing up.

@app.route('/locked')
@login_required # Using login_required to make a page private
def locked(): 
	return "Hello " + current_user.username + " welcome to your private page."

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

login.py-0.0.1.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

login.py-0.0.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file login.py-0.0.1.tar.gz.

File metadata

  • Download URL: login.py-0.0.1.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for login.py-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6baed3ac213a3a3d6ab52c5d9bb21433691f39bc3f69c1bcb97f619330fd2488
MD5 9b0d541a936585ad84376c8b73cd2a0e
BLAKE2b-256 ce78ff8b442e456f911eb8b662b647ec3aa388021a4918937183fd9ac70f87fa

See more details on using hashes here.

File details

Details for the file login.py-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: login.py-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for login.py-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2bdafafe8f4e7f3633372e70a467bbf15685145bf31348954ba0bbcf0b8359a3
MD5 f82b2df6aee0a0b7e896fc6cb411a9b9
BLAKE2b-256 48aa537518370ebcc4520cb7c38bdca284052a4b5c05e5a4912d0887d5aa57ce

See more details on using hashes here.

Supported by

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