我刚刚在我的网站上实施了Facebook登录/注册。当用户登录时,如果我的 myModel <中已经有 Facebook UserID 和电子邮件,我会检查(通过AJAX调用) / strong>即可。如果是,则用户使用 make_password(密码)登录,我在内部为其创建,否则将创建用户。
我的代码:
#Get Facebook detail, through AJAX
userEmail = request.POST['email']
userID = request.POST['userID']
userName = request.POST['username'].replace(" ", "")
#Check if the user already exist.
try:
new_user = myModel.objects.get(email = userEmail, fb_id = userID)
except myModel.DoesNotExist:
new_user = None
if new_user is not None:
user = authenticate(username = new_user.username, password = "internal")
if user is not None:
login(request, user)
return HttpResponse("User Authenticated Successfully")
return HttpResponse("User NOT Authenticated!")
#Create and save user here...
pwd = make_password("internal")
new_user = myModel(email=userEmail, fb_id=userID, username=userName, password=pwd)
new_user.save()
#Create and save this user in Django **User** model.
created_user = User.objects.created_user(userName, userEmail, pwd)
created_user.save()
return HttpResponse("User created successfully!")
但即使我手动(硬编码)有用的用户名和密码,用户也始终无。
答案 0 :(得分:1)
如果make_password
是来自django.contrib.auth的那个,那么您将两次哈希密码。您在调用该函数时将pwd
转换为哈希值,但随后将其传递给create_user
,再次对其进行哈希处理。在创建用户时使用普通User.objects.create()
,或将原始字符串“internal”传递给create_user
。