Exemplo n.º 1
0
    def post(self):

        parser.add_argument('username',
                            help='Username can not be none',
                            required=True)
        parser.add_argument('password',
                            help='Password can not be none',
                            required=True)

        data = parser.parse_args()
        user = UserModel.find_user_by_name(data['username'])

        if not user:
            return {'message': 'User does not exist'}

        if UserModel.check_password(data['password'], user.password):
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])

            return {
                'message': 'Login successfully',
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        else:
            return {'message': 'Wrong password or user name'}
    def post(self):
        loginparser = reqparse.RequestParser()
        loginparser.add_argument('email',
                                 help='Email cannot be blank',
                                 required=True)
        loginparser.add_argument('password',
                                 help='Password cannot be blank',
                                 required=True)
        data = loginparser.parse_args()
        current_user = UserModel.search_email(data['email'])
        if not current_user:
            return {'message': 'User {} doesn\'t exist'.format(data['email'])}

        if UserModel.check_password(data['password'], current_user.password):
            access_token = create_access_token(identity=data['email'])
            return {
                'message': 'Logged in as {}'.format(current_user.email),
                'access_token': access_token,
                'id': current_user.id,
                'email': current_user.email,
                'name': current_user.name
            }
        else:
            return {'message': 'Wrong password'}