login refactored

This commit is contained in:
Mikan
2024-09-17 22:33:21 +03:00
parent 241340770d
commit 031122bd46
8 changed files with 38 additions and 16 deletions

21
common/exceptions.py Normal file
View File

@@ -0,0 +1,21 @@
import typing
from django.http import JsonResponse, HttpRequest
class NovelPlusHttpExceptionResponse(JsonResponse):
def __init__(self,
request: HttpRequest,
message: str,
status_code: int = 400,
admin_note: typing.Union[None, str, dict] = None,
**kwargs):
answer = {
'message': message,
}
if admin_note is not None:
if request.user.is_superuser:
answer['admin_note'] = admin_note
super().__init__(answer, status=status_code)

View File

@@ -1,2 +0,0 @@
@import url(https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700;800&display=swap);:root{--color-header-background:#feb198;--color-base-background:#ffe0ce;--color-base-primary:#000;--color-base-secondary:#edc881;--color-base-tertiary:#7d5555;--color-footer-primary:#fff}body{background-color:var(--color-base-background);margin:0;padding:0}h1,h2{margin:0;text-transform:uppercase}h1{font-size:80px}h2{font-size:40px}.body-text{font-size:26px;font-weight:500}.body-text,.primary{font-family:Montserrat,sans-serif}.primary{background:var(--color-base-secondary);border:1px solid var(--color-base-tertiary);border-radius:40px;box-sizing:border-box;color:var(--color-base-tertiary);cursor:pointer;text-decoration:none;text-transform:uppercase;width:fit-content}.primary:hover{color:var(--color-base-primary)}.primary.selected,.primary:hover{border-color:var(--color-base-primary)}.primary.selected{background:var(--color-base-tertiary);color:var(--color-base-secondary)}.primary.large{font-size:80px;font-weight:600;padding:6px 54px}.primary.medium{font-size:40px;font-weight:500;padding:14px 54px}.primary.small{font-size:26px;font-weight:500;padding:10px 29px}.content-box{margin-left:auto;margin-right:auto;width:1880px}.fill-width{width:100%}.align-center,.align-end,.align-start,.horizontal-box,.stretched-box,.vertical-box{display:flex}.stretched-box{flex-direction:row;justify-content:space-between}.vertical-box{flex-direction:column}.horizontal-box{flex-direction:row}.align-end{align-items:flex-end}.align-center{align-items:center}.align-start{align-items:flex-start}.align-left{margin-right:auto}.section{margin:0 auto;min-height:100vh;padding:30px 0}.header{background-color:var(--color-header-background);padding-top:40px}.header,.header .bottom-menu,.header .upper-menu{padding-bottom:20px}.header .upper-menu{gap:60px;padding-top:20px}.header .bottom-menu{border-top:1px solid var(--color-base-primary);padding-top:40px}.header .navigation{gap:20px}.header .avatar{border-radius:100px;height:100px;width:100px}.header .menu-button>.avatar{align-items:center;background-color:#fcfcfa}.header .dropdown-menu{position:relative}.header .menu-button{background:none;border:none;cursor:pointer}.header .menu-content{background-color:#333;border:1px solid #fcfcfa;border-radius:5px;display:flex;flex-direction:column;padding:10px;width:max-content}.header .menu-content a,.header .menu-content button{background:none;border:2px outset buttonborder;border-image:none;border-image:initial;color:#fcfcfa;padding:10px 20px;text-decoration:none}.header .menu-content a:hover,.header .menu-content button:hover{background-color:#444}.header .menu-click-area{position:absolute;right:0;top:0}.header .menu-click-area>div{gap:10px}.header .vk-login{padding-top:20px}.login-from-menu{color:#fff}.card{color:#fff;text-decoration:none}.card,.card>div{height:350px;width:500px}.card>div{background:linear-gradient(180deg,#2d0c0c00 68.5%,#2d0c0c)}.card>div:hover{background:linear-gradient(180deg,#2d0c0c00 30.5%,#2d0c0c)}.card>div>div{color:var(--color-base-secondary);height:66px;margin-left:auto;margin-right:auto;text-align:center;width:450px}.store{grid-gap:39px;display:grid;gap:39px;grid-template-columns:min-content min-content min-content;width:1578px}.about .button-list{gap:20px}.footer{background-color:var(--color-base-tertiary);padding:130px 150px}.footer,.footer a{color:var(--color-footer-primary)}.footer a{font-family:Montserrat,sans-serif;font-size:40px;font-weight:500;text-decoration:none}.footer a:hover{color:var(--color-base-secondary)}.vacancy{gap:56px}.mini-card{color:#000;gap:12px;text-decoration:none}.mini-card:hover{background-color:var(--color-header-background)}.section.item .preview{gap:39px}.section.item .preview>img{height:350px;width:500px}
/*# sourceMappingURL=main.8278e824.css.map*/

View File

@@ -0,0 +1,2 @@
@import url(https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700;800&display=swap);:root{--color-header-background:#feb198;--color-base-background:#ffe0ce;--color-base-primary:#000;--color-base-secondary:#edc881;--color-base-tertiary:#7d5555;--color-footer-primary:#fff;--color-error-text:red}.error{color:red;color:var(--color-error-text)}body{background-color:var(--color-base-background);margin:0;padding:0}h1,h2{margin:0;text-transform:uppercase}h1{font-size:80px}h2{font-size:40px}.body-text{font-size:26px;font-weight:500}.body-text,.primary{font-family:Montserrat,sans-serif}.primary{background:var(--color-base-secondary);border:1px solid var(--color-base-tertiary);border-radius:40px;box-sizing:border-box;color:var(--color-base-tertiary);cursor:pointer;text-decoration:none;text-transform:uppercase;width:fit-content}.primary:hover{color:var(--color-base-primary)}.primary.selected,.primary:hover{border-color:var(--color-base-primary)}.primary.selected{background:var(--color-base-tertiary);color:var(--color-base-secondary)}.primary.large{font-size:80px;font-weight:600;padding:6px 54px}.primary.medium{font-size:40px;font-weight:500;padding:14px 54px}.primary.small{font-size:26px;font-weight:500;padding:10px 29px}.content-box{margin-left:auto;margin-right:auto;width:1880px}.fill-width{width:100%}.align-center,.align-end,.align-start,.horizontal-box,.stretched-box,.vertical-box{display:flex}.stretched-box{flex-direction:row;justify-content:space-between}.vertical-box{flex-direction:column}.horizontal-box{flex-direction:row}.align-end{align-items:flex-end}.align-center{align-items:center}.align-start{align-items:flex-start}.align-left{margin-right:auto}.section{margin:0 auto;min-height:100vh;padding:30px 0}.header{background-color:var(--color-header-background);padding-top:40px}.header,.header .bottom-menu,.header .upper-menu{padding-bottom:20px}.header .upper-menu{gap:60px;padding-top:20px}.header .bottom-menu{border-top:1px solid var(--color-base-primary);padding-top:40px}.header .navigation{gap:20px}.header .avatar{border-radius:100px;height:100px;width:100px}.header .menu-button>.avatar{align-items:center;background-color:#fcfcfa}.header .dropdown-menu{position:relative}.header .menu-button{background:none;border:none;cursor:pointer}.header .menu-content{background-color:#333;border:1px solid #fcfcfa;border-radius:5px;display:flex;flex-direction:column;padding:10px;width:max-content}.header .menu-content a,.header .menu-content button{background:none;border:2px outset buttonborder;border-image:none;border-image:initial;color:#fcfcfa;padding:10px 20px;text-decoration:none}.header .menu-content a:hover,.header .menu-content button:hover{background-color:#444}.header .menu-click-area{position:absolute;right:0;top:0}.header .menu-click-area>div{gap:10px}.header .vk-login{padding-top:20px}.login-from-menu{color:#fff}.card{color:#fff;text-decoration:none}.card,.card>div{height:350px;width:500px}.card>div{background:linear-gradient(180deg,#2d0c0c00 68.5%,#2d0c0c)}.card>div:hover{background:linear-gradient(180deg,#2d0c0c00 30.5%,#2d0c0c)}.card>div>div{color:var(--color-base-secondary);height:66px;margin-left:auto;margin-right:auto;text-align:center;width:450px}.store{grid-gap:39px;display:grid;gap:39px;grid-template-columns:min-content min-content min-content;width:1578px}.about .button-list{gap:20px}.footer{background-color:var(--color-base-tertiary);padding:130px 150px}.footer,.footer a{color:var(--color-footer-primary)}.footer a{font-family:Montserrat,sans-serif;font-size:40px;font-weight:500;text-decoration:none}.footer a:hover{color:var(--color-base-secondary)}.vacancy{gap:56px}.mini-card{color:#000;gap:12px;text-decoration:none}.mini-card:hover{background-color:var(--color-header-background)}.section.item .preview{gap:39px}.section.item .preview>img{height:350px;width:500px}
/*# sourceMappingURL=main.9460849f.css.map*/

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -6,8 +6,8 @@
<title>NovelPlus</title>
<link rel="icon" href="{% static '/misc/icon.ico' %}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script defer="defer" src="{% static 'frontend/js/main.db899d4e.js' %}"></script>
<link rel="stylesheet" href="{% static 'frontend/css/main.8278e824.css' %}">
<script defer="defer" src="{% static 'frontend/js/main.78917bb4.js' %}"></script>
<link rel="stylesheet" href="{% static 'frontend/css/main.9460849f.css' %}">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>

View File

@@ -7,6 +7,7 @@ from django.contrib.auth import logout, authenticate, login
from django.http import JsonResponse, HttpResponse, HttpRequest
from django.views.decorators.csrf import csrf_exempt
from common.exceptions import NovelPlusHttpExceptionResponse
from users.models import User
@@ -63,8 +64,9 @@ def login_user(request: HttpRequest) -> HttpResponse:
login(request, user)
return get_me(request)
else:
return HttpResponse(
"invalid username or password",
return NovelPlusHttpExceptionResponse(
request,
"Неправильный логин или пароль",
status=401
)
@@ -77,11 +79,10 @@ def register_user(request: HttpRequest) -> HttpResponse:
data = dict(request.GET or request.POST)
if User.objects.filter(username=data['username'].lower()).exists():
return JsonResponse(
{
'message': "username already exists"
},
status=400
return NovelPlusHttpExceptionResponse(
request,
"Пользователь с таким ником уже существует",
status=401
)
user = User.objects.create_user(
@@ -120,7 +121,7 @@ def login_via_vk(request: HttpRequest) -> HttpResponse:
'first_name': vk_user.get('first_name'),
'last_name': vk_user.get('last_name'),
'email': vk_user.get('email', None),
'username': str(uuid.uuid4())
'username': str(vk_user.get('user_id'))+"_"+str(uuid.uuid4())
}
)
if create is True: