gold eggs backend
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
This commit is contained in:
27
core/middlewares/fcm_token.py
Normal file
27
core/middlewares/fcm_token.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import jwt
|
||||
from django.conf import settings
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
from core.http.models import User
|
||||
|
||||
|
||||
class JWTFCMMiddleware(MiddlewareMixin):
|
||||
def process_request(self, request):
|
||||
auth_header = request.headers.get("Authorization")
|
||||
if auth_header and auth_header.startswith("Bearer "):
|
||||
token = auth_header.split(" ")[1]
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token, settings.SECRET_KEY, algorithms=["HS256"]
|
||||
)
|
||||
user_id = payload.get("user_id")
|
||||
fcm_token = request.headers.get("FCM-Token")
|
||||
if user_id:
|
||||
user = User.objects.get(id=user_id)
|
||||
if fcm_token:
|
||||
user.fcm_token = fcm_token
|
||||
user.save()
|
||||
except jwt.ExpiredSignatureError:
|
||||
pass
|
||||
except jwt.InvalidTokenError:
|
||||
pass
|
||||
Reference in New Issue
Block a user