按住 Ctrl +点击
跳转到:
methods:{
isRegister(){
var that = this;
register({
password:that.password,
username:that.mobile ,
code:that.code,
}).then((response)=> {
cookie.setCookie('name',response.data.username,7);
cookie.setCookie('token',response.data.token,7)
//存储在store
// 更新store数据
that.$store.dispatch('setInfo');
//跳转到首页页面
this.$router.push({ name: 'index'})
})
.catch(function (error) {
that.error.mobile = error.username?error.username[0]:'';
that.error.password = error.password?error.password[0]:'';
that.error.username = error.mobile?error.mobile[0]:'';
that.error.code = error.code?error.code[0]:'';
});
},
按住Ctrl +点击鼠标
把
//注册
export const register = parmas => { return axios.post(`${host}/users/`, parmas) }
改成:
//注册
export const register = parmas => { return axios.post(`${local_host}/users/`, parmas) }
注册后说有两种情况:
1.注册后,需要用户自己登录
2.注册后,自动登录
现在做,注册后自动帮用户登录:
要把token返回
说要重载这个函数:
CreateModelMixin
把
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
拷贝过来:
下面分析一下 生成token的源码:
from rest_framework_jwt.serializers import jwt_encode_handler, jwt_payload_handler
re_dict = serializer.data
payload = jwt_payload_handler(user)
re_dict["token"] = jwt_encode_handler(payload)
完整的:
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = self.perform_create(serializer)
re_dict = serializer.data
payload = jwt_payload_handler(user)
re_dict["token"] = jwt_encode_handler(payload)
re_dict["name"] = user.name if user.name else user.username
headers = self.get_success_headers(serializer.data)
return Response(re_dict, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer):
return serializer.save()