Ejemplo n.º 1
0
    def put(self):
        """
        The `put()` method will modify current_user's email and password
        """
        content = request.json
        if content.get('username') is not None \
                and content.get('email') is not None:
            check_user = User.objects(username=content.get('username'),
                                      email=content.get('email')).first()
            if check_user:
                return make_response("user existed", 400)

            updated_user = {
                "$set": {
                    'username': content.get('username'),
                    'email': content.get('email')
                }
            }
            query = {
                "username": current_user.username,
                "email": current_user.email
            }
            userCollection.update_one(query, updated_user)
            current_user.username = content.get('username')
            current_user.email = content.get('email')
            return make_response("account updated successfully", 200)

        return make_response("wrong parameters", 400)
Ejemplo n.º 2
0
 def post(self):
     """
     The `post()` method will create new username+pwd
     """
     content = request.json
     if current_user.is_authenticated:
         return make_response("authenticated wrong", 400)
     if User.objects(email=content.get('email')).first() is not None:
         return make_response("email has been registered", 401)
     if User.objects(username=content.get('username')).first() is not None:
         return make_response("username has been registered", 402)
     if content.get('username') is not None and content.get('password') \
             is not None and content.get('email') is not None:
         hashed_password = bcrypt.\
             generate_password_hash(content.get('password')).decode('utf-8')
         user = User(username=content.get('username'),
                     email=content.get('email'),
                     password=hashed_password)
         user.save()
         return make_response("register successfully", 200)
     else:
         return make_response("parameter wrong", 400)
Ejemplo n.º 3
0
 def post(self):
     """
     The `post()` method will serve as users Login
     """
     content = request.json
     if current_user.is_authenticated:
         return make_response("authenticated wrong", 400)
     if content.get('email') is not None and \
             content.get('password') is not None:
         check_user = User.objects(email=content.get('email')).first()
         if check_user:
             if bcrypt.check_password_hash(check_user["password"],
                                           content.get('password')):
                 session.permanent = True
                 app.permanent_session_lifetime = \
                     datetime.timedelta(minutes=30)
                 login_user(check_user)
                 return make_response("login successfully", 200)
             else:
                 return make_response("wrong password", 400)
         else:
             return make_response("need register", 401)
     else:
         return make_response("wrong parameters", 402)
Ejemplo n.º 4
0
def load_user(user_id):
    """
    loading user session for flask-login
    """
    return User.objects(pk=user_id).first()