Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Add register user endpoint

Daoud Clarke9db91173d2bd
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.py
index 560745a..0f0e834 100644
--- a/mwmbl/platform/api.py
+++ b/mwmbl/platform/api.py
@@ -1,7 +1,29 @@
from ninja_extra import NinjaExtraAPI
from ninja_jwt.controller import NinjaJWTDefaultController
+from ninja_jwt.tokens import RefreshToken
+from mwmbl.models import MwmblUser
+from mwmbl.platform.schemas import Registration
api = NinjaExtraAPI(urls_namespace="platform")
api.register_controllers(NinjaJWTDefaultController)
+
[email protected]('/register')
+def register(request, registration: Registration):
+ # Check for existing user with this username
+ if MwmblUser.objects.filter(username=registration.username).exists():
+ return {"status": "error", "message": "Username already exists"}
+
+ user = MwmblUser(username=registration.username, email=registration.email)
+ user.set_password(registration.password)
+ user.save()
+
+ refresh = RefreshToken.for_user(user)
+
+ return {
+ "status": "ok",
+ "username": registration.username,
+ 'refresh': str(refresh),
+ 'access': str(refresh.access_token),
+ }
diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.py
new file mode 100644
index 0000000..9cb038f
--- /dev/null
+++ b/mwmbl/platform/schemas.py
@@ -0,0 +1,7 @@
+from ninja import Schema
+
+
+class Registration(Schema):
+ email: str
+ username: str
+ password: str