7-13 vue和注册功能联调

 

按住 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()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值