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'))


Much obliged for the tips and info that you provide above, it is very useful for me..i truly value it.
Zobello - The Online Men's Fashion Store
online clothes shopping | online shopping fashion | online dress shopping | online shopping clothes | online shopping clothes | clothes shopping online | online shopping for clothes | shopping online clothes | clothing shopping sites | clothing online shopping | Blezer for man | blazers for men | casual blazers for men | men blazers | buy blazers online |
blazer online shopping | blazers for men online | casual blazer | blazers online india mens casual blazer | blazers for men online | blazers for mens | Swimwear for men | swimwear online india | buy swimwear online india | swimwear india | swimwear online | beachwear india | buy swimwear online | beachwear online | beachwear online india | buy swimwear online | swim shorts for men | swimming trunks online | swimsuit india | buy swimsuit online | canvas shoes for men | sneakers shoes | sneakers for men | sneakers shoes for men | sneaker shoes | buy sneakers online | best sneakers for men | sneakers shoes online | canvas sneakers | high ankle sneakers online india | high ankle shoes online | sneakers shoes online | backpacks for men | backpacks online | backpack bags | backpack online | buy backpacks online | backpacks online india | backpack online india | buy backpacks online india | backpacks india | backpack india | cheap backpacks online india | messenger bags for men | messenger bag | leather messenger bags for men | messenger bags for men india | messenger bags online shopping india | messenger bags india | messenger bags online india | messenger bags for men india | messenger bag online india | messenger bag india | Duffle bags online | duffle bags for men | duffle bags online india | duffel bags online | duffle bag online | duffle bags online india | leather duffle bag india | leather duffle bags online india | duffel bag online | buy duffle bag online | bags online india | online bag shopping | online bags shopping | online shopping bags | bags online shopping india | bags online | online bags | bags online shopping | buy bags online | buy laptop bags online

I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information.
billa bong

I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing. Snapchat Hack

Hey there I feel so fortunate to come over your blog you express it in a very easiest way I am very much active person over your blog pretty good to read your blog its quit interesting to read your blog always because it looks fresh all the time.
M City Condos

ONLINEPAIDCLICK is, actually, the mechanics of bringing coins for on line clicks. we're the leading net advertising and marketing organisation and advocate both truthful and 5bf1289bdb38b4a57d54c435c7e4aa1c tactics to various domains of advertising ranging from local ads to e-mail advertising. Video

Nice to be visiting your blog again, it has been months for me. Well this article that i’ve been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
cheap designer shoes

All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks
Veronica's Bridal Shop

Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it's nice to find a website that details so much information about different artists.
Click Here

I have not any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
free tarot reading

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
desiree gruber

It is good that you are allowing to their kids to use the facility of the internet and prohibited the wrong content. You give http://best-essay-writing.services/ with limited permission to their kids so that they can see the interesting and informative video and articles. You manage the good and proper work.

A debt of gratitude is in order for the web journal stacked with such a large number of data. Halting by your web journal assisted me with getting what I was searching for. Pixel Gun 3d Hack

An entrancing dialog is quality remark. I surmise that it is best to compose additional on this matter, it won't be an unthinkable point however for the most part individuals are insufficient to chat on such themes. To the following. Salud clash of kings hack tool

"Boredom is a killer—which is why a cure for boredom can be a lifesaver.

The truth is, portable Bluetooth speakers have been the handheld solution to elevate the moment, whether at the beach, the middle of a party, or a lazy afternoon at home for years now—and your brand can get in on that excitement.

Right now—with C2BPromo.com, you can put you logo on a wide array of Promotional Bluetooth Speakers, in all different shapes and sizes, from waterproof to resilient wood—with models coming in at 6-7 dollars all the way up to high-end luxury designs. Multiple surfaces on almost every customized Bluetooth speaker can showcase your logo—so no matter which way it’s turned, your name is always center staged." Promotional Bluetooth Speaker

Thanks for sharing this informative info. I can see this being helpful for me and others in the near future!

I really needed to build a fast remark keeping in mind the end goal to offer thanks to you for these remarkable substance you are putting on your site. You are continually leading a dynamite work. I should state you have composed incredible article.

Add new comment