2

I'm wondering what the best approach is to store user authentication data in a neo4j database with django using the inbuilt auth system.

Has anybody got any experience of doing so?

I'm imagining that it has something to do with subclassing the AbstractBaseUser and BaseUserManager but for the life of me I can't figure it out.

Would very much appreciate a code snippet if anybody has achieved this before.

Many Thanks

Jamie
  • 33
  • 3

1 Answers1

0

If you want to extend the Django User model, first check this article. It shows different ways of extending the User model. In my last workaround I needed all the information in Neo4j so I adapt my model to have the fields of the user in my model (It was a model of Student). Whenever a new student register to the app, I have a signal to react after the save (post_save) and it stores the password and the username. You can explore the Django signals here

For the model I have:

class StudentProfile(DjangoNode):
    first_name = StringProperty(max_length=30)
    last_name = StringProperty(max_length=150)
    email = EmailProperty()
    birth = DateProperty()
    username = StringProperty(max_length=150, unique=True)
    password = ''

For the signal:

@receiver(post_save, sender=StudentProfile, dispatch_uid='create_user_student')
def create_user_student(sender, instance, created, **kwargs):
    if created:
        user = User.objects.create_user(instance.username)
        user.set_password(instance.password)
        user.save()


@receiver(post_delete, sender=StudentProfile, dispatch_uid='delete_user_student')
def delete_user_student(sender, instance, **kwargs):
    User.objects.filter(username=instance.username).delete()

Besides the main view of the StudentProfile, I have a view that uses the built-in Django authentication system:

from django.contrib.auth import authenticate, login as do_login, logout as do_logout

...

@api_view(["POST"])
def login(request):
    username = request.data.get('username')
    password = request.data.get('password')

    user = authenticate(username=username, password=password)

    if user is not None:
        do_login(request, user)
        return Response({'login': 'ok'}, status=status.HTTP_200_OK)
    return Response({'login': 'Error on credentials'}, status=status.HTTP_403_FORBIDDEN)
dnarvaez27
  • 472
  • 1
  • 6
  • 10
  • How do you integrate the user in your neo4j db and djangos User object? I mean in your receiver methods, which database the User,objects.create is accessing to? How did you make it to access neo4j? – cuneyttyler May 21 '22 at 12:20
  • @cuneyttyler As i did research on web i found that he used django_neomodel package to extend the djangonode – Devang Hingu Mar 29 '23 at 13:02
  • i just wonder how you used authenticate method with graphdb! – Devang Hingu Mar 29 '23 at 13:22
  • What I did was actually to use a mysql database for storing user data only and replicate that in neo4j database. Use mysql with django auth and neomodel with accessing other data. – cuneyttyler Mar 30 '23 at 14:07