Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
How to capture IP Address of user when using django-allauth for sign in?
Post Body

Hello friends! I have a custom user model in which ip_address is one of the fields. I have provided users a standard sign up process using model form and a social login option using Google for a quick login process. I have used 'django-allauth' library for integrating social logins.

If a user chooses to signup using the regular signup process, I am able to capture the IP address successfully using request.META.get('HTTP_X_FORWARDED_FOR') or request.META.get('REMOTE_ADDR') . But When a user uses the social login option (Google), since everything is being handled by django-allauth, I am not sure how I can also capture the IP Address at the time of sign in. Is there any way I can try some way via signals to achieve this? Or is there any other way?

Any guidance on this would be appreciated.

Edit: After banging my head around for a few hours, and thanks to suggestions from u/xNovax and u/oliw, I finally managed to capture the IP address of user successfully. I was previously trying to write some method inside a view. After going through multiple posts on the internet, below is a solution that finally worked for me. I created a signal to capture or update the IP Address every time a user successfully logs into the website.

signals.py file:

from django.contrib.auth import user_logged_in

def get_ip(request):

"""Function to capture IP Address of user"""

try:

x_forward = request.META.get("HTTP_X_FORWARDED_FOR")

if x_forward:

ip = x_forward.split(",")[0]

else:

ip = request.META.get("REMOTE_ADDR")

except:

ip = ""

return ip

def login_ip(sender, user, request, **kwargs):

user.ip_address = get_ip(request)user.save()

user_logged_in.connect(login_ip)

Author
User Disabled
Account Strength
0%
Disabled 4 months ago
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
6,531
Link Karma
663
Comment Karma
5,737
Profile updated: 4 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago