Login

"Magic Link" Management Command

Author:
webology
Posted:
March 24, 2021
Language:
Python
Version:
Not specified
Score:
3 (after 3 ratings)

Django Management Command to print a "Magic Link" for one-click log-in. This is nice for people who project switch or don't want to remember passwords.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Django Management Command to print a "Magic Link" for one-click login
by Jeff Triplett <jefftriplett> / https://twitter.com/webology

https://gist.github.com/jefftriplett/e87b36d750f94e48080a2be46e71dbe1

Relies on: https://github.com/aaugustin/django-sesame

$ pip install django-click
$ pip install django-sesame[ua]

This should live in one of your app that is installed in your `settings.INSTALLED_APPS`
/management/commands/magic-link.py

Example: 

$ python manage.py magic-link
http://localhost:8000/?sesame=AAAAAZXgFCVouyZCceM

"""
import djclick as click

from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from sesame.utils import get_query_string


@click.command()
@click.option("username", "--user", default=None)
def command(username):
    User = get_user_model()

    if username:
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            user = None
    else:
        user = User.objects.first()

    domain_name = Site.objects.get_current()

    if user:
        click.echo(f"http://{domain_name}/{get_query_string(user)}")
    else:
        click.secho(f"No user or username was found", fg="red")

More like this

  1. "Magic Link" Management Command by webology 4 months, 2 weeks ago
  2. Closest ORM models to a latitude/longitude point by simonw 4 months, 2 weeks ago
  3. Log the time taken to execute each DB query by kennyx46 4 months, 2 weeks ago
  4. django database snippet by ItsRLuo 4 months, 2 weeks ago
  5. Serialize a model instance by chriswedgwood 5 months, 2 weeks ago

Comments

Please login first before commenting.