Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Authentication

Dylan Kok edited this page Jan 1, 2021 · 2 revisions

Methods

.is_auth()

Gets user's authentication status

import auth as auth

auth.is_auth()
# If logged in, returns True
# Otherwise, returns False

Options

.is_auth(True): Changes method into a accessor method. Returns sanitised user data.

.get_sid()

Retrieve user's current session ID

import auth as auth

auth.get_sid()
# If logged in, returns user session id of type string
# Otherwise, returns None

Options

This method has no options

.generate_password_hash(raw_password)

Hashes & changes the password in the database

import auth as auth

auth.generate_password_hash("helloWorld-12345")
# Returns hash of password.

Options

This method has no options

Usages

Prevent unauthenticated users from viewing page

import auth as auth
from flask import ...

@app.route('/settings')
def login():
    if auth.is_auth():
        return render_template('...')
    else:
        return redirect(url_for('...'))

Prevent already authenticated users from viewing page

import auth as auth
from flask import ...

@app.route('/login')
def login():
    if not auth.is_auth():
        return render_template('...')
    else:
        return redirect(url_for('...'))

Dynamically render page data according to login status

...
    # Get login status using accessor argument
    result = auth.is_auth(True)  # Returns dictionary of user via database
    
    # if not logged in
    if not result:
        return render_template('customer/marketplace.html',
                               listings=list(shop_db.find()), loggedin=False)
                               
    # if logged in
    else:
        return render_template('customer/marketplace.html',
                               listings=list(shop_db.find()), loggedin=True, user=result)
...