Deny access to unauthenticated users with django channels
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Deny Access To Unauthenticated Users With Django Channels
00:38 Answer 1 Score 0
00:48 Accepted Answer Score 3
01:30 Thank you
--
Full question
https://stackoverflow.com/questions/4566...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangochannels
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping
--
Chapters
00:00 Deny Access To Unauthenticated Users With Django Channels
00:38 Answer 1 Score 0
00:48 Accepted Answer Score 3
01:30 Thank you
--
Full question
https://stackoverflow.com/questions/4566...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangochannels
#avk47
ACCEPTED ANSWER
Score 3
Denying the connection on connect attempt is as simple as this: do not send the accept message at all if you don't want to establish a connection. Channels will close it automatically after a configured amount of time (5 seconds or smth like that by default).
def connect(self, message, **kwargs):
if not message.user.is_anonymous:
self.send({"accept": True})
If you do not want to wait and close the connection immediately, just send {"close": True} instead:
def connect(self, message, **kwargs):
if not message.user.is_anonymous:
self.send({"accept": True})
else:
self.send({"close": True})
For the sake of completeness, here is the explanation from the channels docs. Sadly, this information is not listed in the documentation itself, only in release notes to v1.0.
ANSWER 2
Score 0
Just do this:
if user.is_authenticated():
# allow it