Logging in to a Django site with a magic token

I have a simple video website for my kids and each kid has a separate login. This is so they can each have their own videos, but also so that some videos can be private (ie. hidden from the outside world, or other logged in users). Typing in a username and password is impossible for my kids to do, as they are almost 5 and 2 years old, and they use this website on Google TV. So, with a magic token-style login, all they need to do is navigate to their bookmark on the Google TV homepage and press OK on the remote control.

(I don't need crazy security--it wouldn't be the end of the world if somehow someone guessed the magic token and saw some private videos, which are basically just home videos uploaded to Youtube. Videos that I really wouldn't want the public to see don't get uploaded to Youtube in the first place.)

I couldn't find how to do this easily, although one person on stackoverflow suggested "logging in the user in the view by calling 'login'". The tricky part was figuring out that I had to set the User object's backend to 'django.contrib.auth.backends.ModelBackend'. It's a bit of a hack, but it works, and it's simple.


class MagicToken(models.Model):
    user = models.OneToOneField(User)
    magictoken = models.CharField(max_length=128, unique=True)
    def __unicode__(self):
        return unicode(self.user)


from django.http import HttpResponse, HttpResponseRedirect, Http404
import django.contrib.auth.login
class MagicTokenLogin(View):
    def get(self, request, token):
            magic_token_obj = MagicToken.objects.get(magictoken=token)
        except MagicToken.DoesNotExist:
            raise Http404
        user = magic_token_obj.user
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        django.contrib.auth.login(request, user)
        if request.user.is_authenticated():
            # login successful
            return HttpResponseRedirect(reverse('some-view-for-logged-in-users'))
            # login failed
            return HttpResponseRedirect(reverse('some-view'))


Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information. Pixel Gun 3d Hack

Much obliged to you for such an elegantly composed article. It's brimming with astute data and diverting depictions. Your perspective is the best among numerous fifa 16 crack

I'm very pleased to find this site. I wanted to thank you for people time with this fantastic read!! I certainly savored every
part of it and I also have you ever book-marked to check out new information in your blog. Alsoo please have a look at my page

This page really has all the information I wanted about this subject and didn’t know who to ask.

Adults ages 50 and up should not focus solely on weight machines for their workouts. While this may increase their strength on the machines, it may actually detract from strengthening the muscle groups that are used in daily activities - especially those which are progressively weakened by the aging process.

One key to sticking with your fitness routine is variety. Incorporating new exercises and new strategies prevents your workouts from getting boring. Any of the ideas presented in this article might help introduce a little novelty to your personal Male Extra Reviews fitness program. Feel free to incorporate the suggestions that look like they will work for you.

Add new comment