Upload backend/hue_portal/core/auth_views.py with huggingface_hub
Browse files
backend/hue_portal/core/auth_views.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.contrib.auth import authenticate, get_user_model
|
| 2 |
+
from rest_framework import permissions, status
|
| 3 |
+
from rest_framework.response import Response
|
| 4 |
+
from rest_framework.views import APIView
|
| 5 |
+
from rest_framework_simplejwt.tokens import RefreshToken
|
| 6 |
+
|
| 7 |
+
from .models import UserProfile
|
| 8 |
+
from .serializers import RegisterSerializer, AuthUserSerializer
|
| 9 |
+
|
| 10 |
+
User = get_user_model()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _user_role(user):
|
| 14 |
+
profile = getattr(user, "profile", None)
|
| 15 |
+
return profile.role if profile else UserProfile.Roles.USER
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class RegisterView(APIView):
|
| 19 |
+
permission_classes = [permissions.IsAuthenticated]
|
| 20 |
+
|
| 21 |
+
def post(self, request):
|
| 22 |
+
if _user_role(request.user) != UserProfile.Roles.ADMIN:
|
| 23 |
+
return Response({"detail": "Bạn không có quyền tạo tài khoản."}, status=status.HTTP_403_FORBIDDEN)
|
| 24 |
+
|
| 25 |
+
serializer = RegisterSerializer(data=request.data)
|
| 26 |
+
serializer.is_valid(raise_exception=True)
|
| 27 |
+
user = serializer.save()
|
| 28 |
+
return Response(AuthUserSerializer(user).data, status=status.HTTP_201_CREATED)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class LoginView(APIView):
|
| 32 |
+
permission_classes = [permissions.AllowAny]
|
| 33 |
+
|
| 34 |
+
def post(self, request):
|
| 35 |
+
username = request.data.get("username") or request.data.get("email")
|
| 36 |
+
password = request.data.get("password")
|
| 37 |
+
|
| 38 |
+
if not username or not password:
|
| 39 |
+
return Response({"detail": "Thiếu thông tin đăng nhập."}, status=status.HTTP_400_BAD_REQUEST)
|
| 40 |
+
|
| 41 |
+
user = authenticate(request, username=username, password=password)
|
| 42 |
+
|
| 43 |
+
if not user:
|
| 44 |
+
try:
|
| 45 |
+
user_obj = User.objects.get(email=username)
|
| 46 |
+
if user_obj.check_password(password):
|
| 47 |
+
user = user_obj
|
| 48 |
+
except User.DoesNotExist:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
if not user:
|
| 52 |
+
return Response({"detail": "Thông tin đăng nhập không hợp lệ."}, status=status.HTTP_401_UNAUTHORIZED)
|
| 53 |
+
|
| 54 |
+
refresh = RefreshToken.for_user(user)
|
| 55 |
+
data = {
|
| 56 |
+
"access": str(refresh.access_token),
|
| 57 |
+
"refresh": str(refresh),
|
| 58 |
+
"user": AuthUserSerializer(user).data,
|
| 59 |
+
}
|
| 60 |
+
return Response(data, status=status.HTTP_200_OK)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class LogoutView(APIView):
|
| 64 |
+
permission_classes = [permissions.IsAuthenticated]
|
| 65 |
+
|
| 66 |
+
def post(self, request):
|
| 67 |
+
refresh_token = request.data.get("refresh")
|
| 68 |
+
if not refresh_token:
|
| 69 |
+
return Response({"detail": "Thiếu refresh token."}, status=status.HTTP_400_BAD_REQUEST)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
token = RefreshToken(refresh_token)
|
| 73 |
+
token.blacklist()
|
| 74 |
+
except Exception:
|
| 75 |
+
return Response({"detail": "Refresh token không hợp lệ."}, status=status.HTTP_400_BAD_REQUEST)
|
| 76 |
+
|
| 77 |
+
return Response({"detail": "Đã đăng xuất."}, status=status.HTTP_200_OK)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class CurrentUserView(APIView):
|
| 81 |
+
permission_classes = [permissions.IsAuthenticated]
|
| 82 |
+
|
| 83 |
+
def get(self, request):
|
| 84 |
+
return Response(AuthUserSerializer(request.user).data)
|
| 85 |
+
|
| 86 |
+
|