Ask Documentation
WebsiteGitHubPyPI
  • Introduction
  • Getting Started
    • Install & Get Started
    • Hello, World!
  • Routes & Requests
    • Routes
    • Defining Routes
    • Request Data
    • HTTP Status Codes & Methods
    • CORS
  • Response
    • JSON Response
    • JSON Response With an HTTP Status Code
  • Classes
    • Class Instance Variable
    • Initialization/Constructor Method
  • Data Types
    • Dictionaries
  • Built-in Utilities
    • Quick_set()
    • Deep()
    • Serialize()
    • Require_keys()
    • Random Generators
    • Pattern Matching
    • Email
  • Database
    • Ask and Databases
    • Models/Classes
      • Columns
      • Initialization/Constructor
      • Serialization
      • The &basic decorator
    • CRUD
      • Add
      • Select
      • Update
      • Delete
    • Check if a Row Exists
    • Sorting
    • Database Lists
  • JWT Authentication
    • Introduction
    • Protecting Routes
    • How to Create a Basic Login System
    • Properties & Methods of _auth
    • Making Requests to Protected Routes
  • Decorators
    • What are Decorators?
    • Create and Use Custom Decorators
    • Built-in Decorators
  • Security
    • Hashing
    • Route Security
    • Environment Variables
  • Configuring the Transpiler
  • Askfile.toml
  • Modules & Libraries
    • Importing an Ask Module
    • Includes
    • Importing Python Modules
  • Development Tools
    • Editor Syntax Highlighting
    • Automatic API documentation
    • CLI Flags
    • Running in development mode
    • Versioning System
  • Contribute
    • Feature Requests
    • Bug Reports
    • Contribute Code
Powered by GitBook
On this page
  • secret_key
  • Usage
  • login()
  • Usage
  • decode()
  • Usage
  • encode() Advanced!
  • Usage
  • is_valid()
  • Usage
  • get_token()
  • Usage
  • user()
  • Usage
Export as PDF
  1. JWT Authentication

Properties & Methods of _auth

Properties and methods.

auth is a built-in object. It's used for, generating & verifying tokens. It also has a few other useful methods and properties.

secret_key

Specify the secret key used for encoding/decoding tokens. This is by default a randomly generated UUID string.

Usage

auth.secret_key = 'secret key goes here'

login()

Generates a JWT.

Usage

auth.login([user], [expiry])

Parameters:

  • String.

  • Typically the users email address or username.

  • Integer.

  • Seconds.

  • How many seconds is the token valid for. E.g. 3600 == one hour.

decode()

Returns the payload for the current token. Use this function inside routes decorated with &protected. You can e.g. use this function to get the email/username from the token (the user parameter passed into login()).

  • Returns a dictionary.

Usage

auth.decode()

encode() Advanced!

Basically the same as login(), but you provide the payload to be encoded, while login() takes a username/email and automatically converts it into a payload dictionary with an expiration value. Use encode() if you want to encode more data than just a username and an expiration.

When using encode() you have to create at least a key called exp in the payload dictionary that holds the current timestamp (Unix epoch seconds) as it's value.

Example:exp: datetime.datetime.utcnow() + datetime.timedelta(seconds=expiry).

Usage

auth.encode(payload)

Parameters:

  • Dictionary.

  • Put data to be encoded into the token here. This data can be obtained later with the decode() method.

  • Example:

{
    user: 'name@example.com',
    exp: datetime.datetime.utcnow() + datetime.timedelta(seconds=expiry)
}

is_valid()

Returns True if the current token is still valid.

Usage

auth.is_valid() # True or False

get_token()

Returns the token sent in the request as a string.

Usage

auth.get_token()

user()

Returns the user/username/email of the current authenticated user's token, you can also access this via the decode method _auth.decode()['user'].

This method only works if the token payload has a key called user, it will have this if you used the login() method to generate the token, but if you generated the token with the encode() method, then this might not work.

Usage

auth.get_token()
PreviousHow to Create a Basic Login SystemNextMaking Requests to Protected Routes

Last updated 4 years ago