微博第三方登录使用social_django实现显示登陆的用户名
首先修改social_soce源码,将用户信息添加进cookie
![](https://i-blog.csdnimg.cn/blog_migrate/004196dbe513fa72176162d7830eed28.png)
将其修改为:
response = backend.strategy.redirect(url)
payload = jwt_payload_handler(user)
response.set_cookie('user_name',user.username,max_age=24*3600)
response.set_cookie('user_id',user.id,max_age=24*3600)
response.set_cookie('token',jwt_encode_handler(payload),max_age=24*3600)
return response
然后在前端页面获取Cookie信息:
getCookie: function (cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
},
payload = jwt_payload_handler(user)
response.set_cookie('user_name',user.username,max_age=24*3600)
response.set_cookie('user_id',user.id,max_age=24*3600)
response.set_cookie('token',jwt_encode_handler(payload),max_age=24*3600)
return response
然后在前端页面获取Cookie信息:
getCookie: function (cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
},