111 lines
4.6 KiB
Python
111 lines
4.6 KiB
Python
from django.urls import path, include
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
from drf_yasg import openapi
|
|
from rest_framework.decorators import api_view
|
|
from dj_rest_auth.views import (
|
|
LoginView, LogoutView, PasswordResetView,
|
|
PasswordResetConfirmView, UserDetailsView
|
|
)
|
|
from rest_framework import serializers
|
|
|
|
# 定义Swagger请求和响应Schema
|
|
class StandardLoginRequestSchema(serializers.Serializer):
|
|
username = serializers.CharField(required=True, help_text="用户名")
|
|
password = serializers.CharField(required=True, help_text="密码")
|
|
|
|
class StandardLoginResponseSchema(serializers.Serializer):
|
|
key = serializers.CharField(help_text="认证令牌")
|
|
|
|
class PasswordResetRequestSchema(serializers.Serializer):
|
|
email = serializers.EmailField(required=True, help_text="用户注册邮箱")
|
|
|
|
class PasswordResetConfirmRequestSchema(serializers.Serializer):
|
|
new_password1 = serializers.CharField(required=True, help_text="新密码")
|
|
new_password2 = serializers.CharField(required=True, help_text="确认新密码")
|
|
uid = serializers.CharField(required=True, help_text="用户ID(编码后)")
|
|
token = serializers.CharField(required=True, help_text="重置密码令牌")
|
|
|
|
class EmptyResponseSchema(serializers.Serializer):
|
|
detail = serializers.CharField(help_text="操作结果信息")
|
|
|
|
# 添加Swagger文档到dj_rest_auth视图
|
|
login_view = swagger_auto_schema(
|
|
method='post',
|
|
request_body=StandardLoginRequestSchema,
|
|
responses={
|
|
200: openapi.Response('登录成功', StandardLoginResponseSchema),
|
|
400: openapi.Response('登录失败', openapi.Schema(
|
|
type=openapi.TYPE_OBJECT,
|
|
properties={
|
|
'non_field_errors': openapi.Schema(
|
|
type=openapi.TYPE_ARRAY,
|
|
items=openapi.Schema(type=openapi.TYPE_STRING)
|
|
)
|
|
}
|
|
))
|
|
},
|
|
operation_description="使用用户名和密码进行标准登录",
|
|
tags=['用户认证']
|
|
)(LoginView.as_view())
|
|
|
|
logout_view = swagger_auto_schema(
|
|
method='post',
|
|
responses={
|
|
200: openapi.Response('登出成功', EmptyResponseSchema)
|
|
},
|
|
operation_description="退出登录,使当前令牌失效",
|
|
tags=['用户认证'],
|
|
security=[{'Bearer': []}]
|
|
)(LogoutView.as_view())
|
|
|
|
password_reset_view = swagger_auto_schema(
|
|
method='post',
|
|
request_body=PasswordResetRequestSchema,
|
|
responses={
|
|
200: openapi.Response('密码重置邮件已发送', EmptyResponseSchema)
|
|
},
|
|
operation_description="发送密码重置邮件到用户邮箱",
|
|
tags=['用户认证']
|
|
)(PasswordResetView.as_view())
|
|
|
|
password_reset_confirm_view = swagger_auto_schema(
|
|
method='post',
|
|
request_body=PasswordResetConfirmRequestSchema,
|
|
responses={
|
|
200: openapi.Response('密码重置成功', EmptyResponseSchema)
|
|
},
|
|
operation_description="使用重置链接中的令牌确认密码重置",
|
|
tags=['用户认证']
|
|
)(PasswordResetConfirmView.as_view())
|
|
|
|
user_details_view = swagger_auto_schema(
|
|
method='get',
|
|
responses={
|
|
200: openapi.Response('获取用户信息成功', openapi.Schema(
|
|
type=openapi.TYPE_OBJECT,
|
|
properties={
|
|
'id': openapi.Schema(type=openapi.TYPE_INTEGER, description='用户ID'),
|
|
'username': openapi.Schema(type=openapi.TYPE_STRING, description='用户名'),
|
|
'email': openapi.Schema(type=openapi.TYPE_STRING, description='邮箱'),
|
|
'phone_number': openapi.Schema(type=openapi.TYPE_STRING, description='手机号码'),
|
|
'first_name': openapi.Schema(type=openapi.TYPE_STRING, description='名'),
|
|
'last_name': openapi.Schema(type=openapi.TYPE_STRING, description='姓'),
|
|
'is_active': openapi.Schema(type=openapi.TYPE_BOOLEAN, description='是否激活')
|
|
}
|
|
))
|
|
},
|
|
operation_description="获取当前登录用户的详细信息,不包含敏感字段如密码",
|
|
tags=['用户认证'],
|
|
security=[{'Bearer': []}]
|
|
)(UserDetailsView.as_view())
|
|
|
|
# 定义URL模式
|
|
urlpatterns = [
|
|
path('login/', login_view, name='rest_login'),
|
|
path('logout/', logout_view, name='rest_logout'),
|
|
path('password/reset/', password_reset_view, name='rest_password_reset'),
|
|
path('password/reset/confirm/', password_reset_confirm_view, name='rest_password_reset_confirm'),
|
|
path('user/', user_details_view, name='rest_user_details'),
|
|
# 包含其他dj_rest_auth URLs
|
|
path('', include('dj_rest_auth.urls')),
|
|
] |