Login

"Magic Link" Management Command

Author:
webology
Posted:
March 24, 2021
Language:
Python
Version:
Not specified
Score:
4 (after 4 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.