Exemple #1
0
    def post(self):
        response = {}
        username = str(self.get_body_argument("username", ''))
        password = str(self.get_body_argument("password", ''))
        if not check_uname_passwd(username, password):
            self.set_status(400)
            response['msg'] = "The type of username or password is error"
            self.write(response)
            return

        user = yield self.db.users.find_one({"username": username})
        if not user:
            self.set_status(400)
            response['msg'] = "The user doesn't exit"
            self.write(response)
            return

        hashed = str(user.get('password', ''))
        if check_passwd(password, hashed):
            userid = str(user.get('id', ''))
            token = token_encode({"id": userid}, options.secret_key)
            response['token'] = token
            response['msg'] = "Login Success"
            self.write(response)
            return

        self.set_status(401)
        response['msg'] = "The password was error"
        self.write(response)
        return
Exemple #2
0
    def post(self):
        response = {}
        username = str(self.get_body_argument("username", ''))
        password = str(self.get_body_argument("password", ''))
        email = str(self.get_body_argument("email", ''))
        coutry = str(self.get_body_argument("coutry", ''))
        if not check_uname_passwd(username, password) or not check_email(email):
            self.set_status(400)
            response['msg'] = "The type of username or password or email is error"
            self.write(response)
            return

        user_id = shortid_generate()
        passwd = passwd_hash(str(password))
        user = {'id': user_id, 'username': str(username), 'password': passwd, 'email': str(email), 'coutry': coutry, 'admin_auth': False, 'score': 0, 'banned': False}
        db_uname, db_email = yield [self.db.users.find({'username': str(username)}).count(), self.db.users.find({'email': str(email)}).count()]
        if db_uname or db_email:
            self.set_status(400)
            if db_email and db_uname:
                response['msg'] = "The email and username have exited"
            elif db_email:
                response['msg'] = "The email have exited"
            else:
                response['msg'] = "The username have exited"
            self.write(response)
            return

        try:
            result = yield self.db.users.insert(user)
        except Exception as e:
            # add log here
            self.set_status(404)
            response['msg'] = "Register Error."
            self.write(response)
            return
        if result:
            self.set_status(201)
            response['msg'] = "Register Success"
            self.write(response)
            return
        else:
            self.set_status(404)
            response['msg'] = "Register Error"
            self.write(response)
            return