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
secret_keySpecify 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()
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()
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!
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: '[email protected]',
exp: datetime.datetime.utcnow() + datetime.timedelta(seconds=expiry)
}is_valid()
is_valid()Returns True if the current token is still valid.
Usage
auth.is_valid() # True or Falseget_token()
get_token()Returns the token sent in the request as a string.
Usage
auth.get_token()user()
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'].
Usage
auth.get_token()Last updated