chayapan / favlinks

MIT License
1 stars 0 forks source link

1. User Registration #2

Open chayapan opened 8 months ago

chayapan commented 8 months ago
  1. User Registration

Expectations:

chayapan commented 8 months ago

See this notes:

  1. https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms
  2. https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Authentication

Use the standard Django auth module from https://docs.djangoproject.com/en/5.0/topics/auth/default/#all-authentication-views

Use BaseUserCreationForm to register new accounts. https://docs.djangoproject.com/en/5.0/topics/auth/default/#django.contrib.auth.forms.BaseUserCreationForm

chayapan commented 8 months ago

We might explore how to use JWT and see if we can add it to the project.

This project seem promising https://github.com/humanitec/django-oauth-toolkit-jwt

But this is a distraction...

chayapan commented 7 months ago

Add messages framework to pass message between web session

https://docs.djangoproject.com/en/5.0/ref/contrib/messages/

chayapan commented 7 months ago

The simplest and most up to date is https://stackoverflow.com/questions/16857450/how-to-register-users-in-django-rest-framework

  1. Serializer
  2. api (the view)
from rest_framework import serializers
from django.contrib.auth import get_user_model # If used custom user model

UserModel = get_user_model()

class UserSerializer(serializers.ModelSerializer):

    password = serializers.CharField(write_only=True)

    def create(self, validated_data):

        user = UserModel.objects.create_user(
            username=validated_data['username'],
            password=validated_data['password'],
        )

        return user

    class Meta:
        model = UserModel
        # Tuple of serialized model fields (see link [2])
        fields = ( "id", "username", "password", )
from rest_framework import permissions
from rest_framework.generics import CreateAPIView
from django.contrib.auth import get_user_model # If used custom user model

from .serializers import UserSerializer

class CreateUserView(CreateAPIView):

    model = get_user_model()
    permission_classes = [
        permissions.AllowAny # Or anon users can't register
    ]
    serializer_class = UserSerializer
chayapan commented 7 months ago

Use password less signup for Django provided by this library. drfpasswordless https://github.com/aaronn/django-rest-framework-passwordless