Skip to content

Commit

Permalink
Fix endpoint for confirm email
Browse files Browse the repository at this point in the history
  • Loading branch information
ltan02 committed Mar 18, 2024
1 parent 6f17426 commit 3845da9
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 22 deletions.
3 changes: 3 additions & 0 deletions backend/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
"http://auctions.microvaninc.com",
"http://www.auctions.microvaninc.com",
"https://auctions.microvaninc.com",
"https://www.auctions.microvaninc.com",
]

CORS_ALLOW_CREDENTIALS = True
Expand Down Expand Up @@ -177,6 +179,7 @@
"DEFAULT_AUTHENTICATION_CLASSES": (
"util.authentication.AWSCognitoIDTokenAuthentication",
),
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.AllowAny",),
}

SIMPLE_JWT = {
Expand Down
27 changes: 18 additions & 9 deletions backend/user/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect
from rest_framework import status
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView

Expand All @@ -13,7 +14,9 @@ class BidderListApiView(APIView):

def get_permissions(self):
if self.request.method == "GET":
self.permission_classes = [IsAdminUser]
self.permission_classes = [IsAuthenticated]
else:
self.permission_classes = [AllowAny]
return super().get_permissions()

def get(self, request):
Expand Down Expand Up @@ -303,6 +306,8 @@ def get(self, request):


class LoginAPIView(APIView):
permission_classes = [AllowAny]

cognitoService = AWSCognitoService()

def post(self, request, *args, **kwargs):
Expand Down Expand Up @@ -347,6 +352,8 @@ def post(self, request, *args, **kwargs):


class PasswordResetAPIView(APIView):
permission_classes = [AllowAny]

cognitoService = AWSCognitoService()

def post(self, request, *args, **kwargs):
Expand All @@ -361,6 +368,8 @@ def post(self, request, *args, **kwargs):


class PasswordResetConfirmAPIView(APIView):
permission_classes = [AllowAny]

cognitoService = AWSCognitoService()

def post(self, request, *args, **kwargs):
Expand All @@ -378,17 +387,17 @@ def post(self, request, *args, **kwargs):


class VerifyEmailAPIView(APIView):
permission_classes = [AllowAny]

cognitoService = AWSCognitoService()

def post(self, request, *args, **kwargs):
email = request.data.get("email")
verification_code = request.data.get("verification_code")
def get(self, request, *args, **kwargs):
email = request.query_params.get("email")
verification_code = request.query_params.get("code")

result = self.cognitoService.verify_email(email, verification_code)
if result:
return Response(
{"success": "Email verified successfully."}, status=status.HTTP_200_OK
)
return redirect("https://www.auction.microvaninc.com/register/verified")
else:
return Response(
{"error": "Failed to verify email"}, status=status.HTTP_400_BAD_REQUEST
Expand Down Expand Up @@ -452,7 +461,7 @@ def post(self, request, *args, **kwargs):


class RefreshTokenAPIView(APIView):
permission_classes = [IsAuthenticated]
permission_classes = [AllowAny]

cognitoService = AWSCognitoService()

Expand Down
6 changes: 5 additions & 1 deletion backend/util/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ def has_group(self, group_name):
class AWSCognitoIDTokenAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
token = request.COOKIES.get("idToken")
unauthenticated_user = GenericAuthenticatedUser()
if not token:
return None
return (unauthenticated_user, None)

if request.path.startswith("/api/v1/auth"):
return (unauthenticated_user, None)

try:
decoded_token = self.verify_jwt_token(token)
Expand Down
6 changes: 5 additions & 1 deletion backend/util/middleware.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import jwt
from django.utils.deprecation import MiddlewareMixin
from util.jwt import decode_token

from services import AWSCognitoService
from util.jwt import decode_token


class RefreshTokenMiddleware(MiddlewareMixin):
def process_request(self, request):
if request.path.startswith("/api/v1/auth"):
return

refresh_token = request.COOKIES.get("refreshToken")
if not refresh_token:
return
Expand Down
5 changes: 3 additions & 2 deletions backend/vehicle/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle
from django.shortcuts import get_object_or_404
import pandas as pd
import requests
from django.shortcuts import get_object_or_404

from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle
from .serializers import VehicleSerializer


Expand Down
2 changes: 1 addition & 1 deletion backend/vehicle/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from django.urls import include, path

from .views import (
UploadFileView,
VehicleDetailApiView,
VehicleFilterList,
VehicleListApiView,
VehiclePriceApiView,
UploadFileView,
)

urlpatterns = [
Expand Down
7 changes: 3 additions & 4 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from rest_framework import status
import pandas as pd
from rest_framework import permissions, status
from rest_framework.generics import get_object_or_404
from rest_framework.parsers import FileUploadParser
from rest_framework.response import Response
from rest_framework.views import APIView
import pandas as pd
from rest_framework.parsers import FileUploadParser
from rest_framework import permissions

from core.permissions import IsAdminUser, IsAuthenticated

Expand Down
2 changes: 1 addition & 1 deletion emailService/.aws-sam/build.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ architecture = "x86_64"
handler = "src/handlers/cognitoCustomMessage.cognitoCustomMessageHandler"
manifest_hash = "70a557897a9d2205fadda31828179700"
packagetype = "Zip"
functions = ["helloFromLambdaFunction"]
functions = ["cognitoCustomMessage"]

[layer_build_definitions]
11 changes: 8 additions & 3 deletions emailService/src/handlers/cognitoCustomMessage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
export const cognitoCustomMessageHandler = async (event) => {
if (event.triggerSource === "CustomMessage_SignUp") {
const firstName = event.request.userAttributes["given_name"];
const email = encodeURIComponent(event.request.userAttributes["email"]);
const verificationCode = event.request.codeParameter;

const verificationLink = `http://localhost:8000/api/v1/auth/email-verify/?email=${email}&code=${verificationCode}`;

const emailContent = `
<html>
Expand Down Expand Up @@ -73,6 +77,7 @@ export const cognitoCustomMessageHandler = async (event) => {
color: #ffffff;
text-align: center;
text-decoration: none;
text-decoration-color: #ffffff;
font-size: 18px;
font-weight: 600;
}
Expand Down Expand Up @@ -120,13 +125,13 @@ export const cognitoCustomMessageHandler = async (event) => {
and Conditions.
</p>
<div class="button-container">
<a href="{####}" class="button-link">Click here to verify</a>
<a href="${verificationLink}" class="button-link">Click here to verify</a>
</div>
<p>
If the button does not work, use this link or copy this link into your browser: <br /><a
href="{####}"
href="${verificationLink}"
class="link"
>{####}</a
>${verificationLink}</a
>
</p>
Expand Down

0 comments on commit 3845da9

Please sign in to comment.