简体   繁体   中英

How to authenticate users per application in django

I have chat app in django at http://mydomain.com/chat/
I want to display logged in users in the sidebar

but I don't want users who are logged in on main site http://mydomain.com which is different app


how can i do this
or if its not possible is there any other way to do it?

I'm considering that both the apps are in the same project and you are using a common authentication module for the same. (django.contrib.auth?)

Inherit the user model to create a new model say myuser.

class MyUser(User):
 isChatUser=BooleanField(default=False)

Alternatively you can use a charField with choices.

Now you'll have to show users who are authenticated and are registered as chat users.

def isChatUserLoggedIn(user):
 if user.is_authenticated():
  try:
   myuser = MyUser.objects.get(id=user.id)
   return (True if myuser.isChatUser else False)
  except:
   return False
#And then use the following when verifying your user-->
isChatUserLoggedIn(request.user)

I hope this was useful.

Maybe is not the best approach. But database routers would do the trick: https://docs.djangoproject.com/en/dev/topics/db/multi-db/ . it allows you to have multiple databases(thus, multiple auth systems). And use different databases with the app currently being executed.

As an alternative, you can extend the User model to create a new column a rewrite the auth system to check if the user that is trying to login is the user that's registered in it's repective application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM