简体   繁体   中英

How to authenticate a user using django rest framework

I am trying to authenticate a user using django rest framework and authenticate import below but for some reason i keep getting null as a result when i check the front end with reactJS , i am referring to the variable logged_in_used below:

from django.contrib.auth import authenticate

...

if request.method == "POST":
        username = request.data.get('username')
        password = request.data.get('password')

        get_user = User.objects.get(username=username)
        logged_in_used = authenticate(username=get_user.username, password=get_user.check_password(password))

         ...
         # data with token dictionnary
         ...

         result = (data_with_token, logged_in_used, status.HTTP_200_OK)

Is there something that i am doing wrong here?

try it like this

from django.contrib.auth import authenticate, login

...

if request.method == "POST":
    username = request.POST['username']
    password = request.POST['password']

    logged_user = authenticate(request, username=username, password=password)

    if logged_user is not None:
        login(request, logged_user)

from here you can redirect or whatever else:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM