OAuth2:使用电子邮件(而不是用户名)进行身份验证 [英] OAuth2: authenticate with email instead of username

查看:340
本文介绍了OAuth2:使用电子邮件(而不是用户名)进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将OAuth2与django-oauth-toolkit django-rest-framework一起使用。

I'm using OAuth2 with django-oauth-toolkit django-rest-framework.

我通常通过以下方式对用户进行身份验证,以获取令牌:

I usually authenticate my users the following way, in order to get a token:

curl -X POST -d "grant_type=password&username=new_user&password=new_user" -u "GZwzDjPM89BceT8a6ypKGMbXnE4jWSzsyqbM3dlK:" http://localhost:8000/o/token/

是否可以使用电子邮件代替

Is there a way to use the email instead of the username to authenticate my users?

谢谢!

推荐答案

是!可以通过在用户模型中将电子邮件设置为用户名来实现。

Yes! it is possible by setting the email as username in User model.

class User(AbstractBaseUser, PermissionsMixin):
 email =  models.EmailField(
    verbose_name='email address',
    max_length=255,
    unique=True,
 )
 first_name = models.CharField(max_length=30)
 last_name = models.CharField(max_length=30)

 USERNAME_FIELD = 'email'

则电子邮件现在可以在请求中用作用户名

then email can now be used as username in the request.

curl -X POST -d "grant_type=password&username=test@test.com&password=new_user" -u "GZwzDjPM89BceT8a6ypKGMbXnE4jWSzsyqbM3dlK:" http://localhost:8000/o/token/

这篇关于OAuth2:使用电子邮件(而不是用户名)进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆