Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override the login functionality #137

Open
amity29 opened this issue Jul 13, 2019 · 1 comment
Open

Override the login functionality #137

amity29 opened this issue Jul 13, 2019 · 1 comment

Comments

@amity29
Copy link

amity29 commented Jul 13, 2019

How can I add some custom verification at the time of login? What I want to do is to send an email at the time of user registration and if the user verifies the email then only he can log in. For eg in Django auth, we can achieve like -

        user = authenticate(request, email=email, password=password)
        if user is not None:
            if user.email_verification:
                login(request, user)
                return JsonResponse({"status":"success", "message":"login success"})
            else:
                return JsonResponse({"status":"fail", "message":"verify email"})

in django-rest-framework-simplejwt user directly gets login by calling url-
path('login', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),

How can I achieve email verification status check?

@MandelaK
Copy link

MandelaK commented Nov 23, 2019

Hi @amity29, not sure if this is still an issue for you, but if you are using the TokenObtainPairSerializer, then you could override the validate method there and add a check for the user's verification status like in this snippet below:

class UserLoginSerializer(TokenObtainPairSerializer):

    def validate(self, attrs):
        data = super().validate(attrs)

         # do your extra validation here
        username_field = get_user_model().USERNAME_FIELD
        username = attrs[username_field]
        user = get_user_model().objects.get(**{username_field: username]})
        
        if not user.email_verification:
            raise exceptions.AuthenticationFailed({"status":"fail", "message":"verify email"})
        login(request, user)
        return data

I would do something like this, but there are probably better ways too. Hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants