Example #1
0
    async def post(self):
        username = self.get_json_argument('username')
        password = self.get_json_argument('password')
        email = self.get_json_argument('email')
        code = self.get_json_argument('code')

        # check user exist
        user = User.select().where(User.username == username or User.email == email).limit(1)
        if len(user) > 0:
            raise JsonException(10001, 'username of email exist')

        # check verify code
        key = self.gen_verify_code_key(email)
        verify_code = await redis.get(key)
        verify_code = str(verify_code, 'utf-8')
        if verify_code != code:
            raise JsonException(10002, 'verify code is incorrect')

        hashed_password = password_hash(password)
        user = User(username=username, password=hashed_password, email=email)
        user.save()
        access_token = gen_access_token()
        auth = Auth(source_id=0, user_id=user.id, access_token=access_token)
        auth.save()
        self.finish_json(result={
            "user": user.to_dict(),
            'access_token': access_token
        })
Example #2
0
    def post(self):
        args = self.get_json_arguments()
        source = int(args.get("source_id", 0))
        username = args['username']
        password = args['password']

        if not username or not password:
            raise JsonException(1000, 'need username and password')

        pwd = password_hash(password)
        try:
            user = User.get(User.username == username)
        except:
            user = None
        if user is None or user.password != pwd:
            raise JsonException(1001, 'wrong password')
        access_token = gen_access_token()
        auth = Auth.single(
            Auth.source_id == source and Auth.user_id == user.id)
        if auth is None:
            auth = Auth(source_id=source, user_id=user.id)
        auth.access_token = access_token
        auth.save()

        self.finish_json(result={
            'user': user.to_dict(),
            'access_token': access_token
        })