I have a django project where I have 2 different login page.
- is in the landing page. ex: https://website.com/
- is the django admin login. ex: https://website.com/admin/ For both the login, I am using the same postgres user table.
The requirement is, When I login from landing login page, if the user has access to admin, he should be able to login both landing page and admin page and vice versa. When I logout from any of the 2 logins, it should be able to logout from both the logins if the logged in user is same for both the logins.
My login and logout code from the landing page
@api_view(['POST'])
def login(request):
if request.method == 'POST' and request.data:
data = request.data
username = data['username']
password = data['password']
user = auth.authenticate(username=username, password=password)
if (user is not None) and (user.is_active):
auth.login(request, user)
token, created = Token.objects.get_or_create(user=user)
return Response({"username": str(user), "token": token.key, "status": STATUS['SUCCESS'], "message": MESSAGE_SUCCESS['LOGIN']}, status=status.HTTP_200_OK)
return Response({"status": STATUS['ERROR'], "message": MESSAGE_ERROR['LOGIN']}, status=status.HTTP_200_OK)
@api_view(['POST'])
@authentication_classes((TokenAuthentication,))
@permission_classes((IsAuthenticated,))
def logout(request):
try:
if request.method == 'POST' and request.data:
data = request.data
username = data['username']
password = data['password']
user = auth.authenticate(username=username, password=password)
auth.logout(request)
update_session_auth_hash(request, user)
return Response({"status": STATUS['SUCCESS'], "message": MESSAGE_SUCCESS['LOGOUT']}, status=status.HTTP_200_OK)
except:
return Response({"status": STATUS['ERROR'], "message": MESSAGE_ERROR['LOGOUT']}, status=status.HTTP_200_OK)
Currently I am able to achieve only one side of this. that is When I login from landing page, if he is an admin it will login automatically to admin login, similarly the logout.
I am not able to login or logout from the landing page login when I login/logout from admin login.