0

For context the normal login works with the handleSubmit function

What the react looks like:

function AuthPage() {
  const dispatch = useDispatch()
  const navigate = useNavigate()
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [errors, setErrors] = useState([]);

  const demoLogin = async (e) => {
    e.preventDefault();
    const demoEmail = 'demo@aa.io'
    const demoPassword = 'password'
    const data = await dispatch(login(demoEmail, demoPassword));
    console.log(data)
    if (data) {
      return setErrors(data);
    }
  }

  const handleSubmit = async (e) => {
    e.preventDefault();
    setErrors([]);
    const data = await dispatch(login(email, password));
    if (data) {
      return setErrors(data);
    }
    navigate('/channel')
  };

What the store looks like:

export const login = (email, password) => async (dispatch) => {
  console.log(email, password)
  const response = await fetch('/api/auth/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      email,
      password
    })
  });

  if (response.ok) {
    const data = await response.json();
    dispatch(setUser(data))
    return null;
  } else if (response.status < 500) {
    const data = await response.json();
    if (data.errors) {
      return data.errors;
    }
  } else {
    return ['An error occurred. Please try again.']
  }

}

The backend:

@auth_routes.route('/login', methods=['POST'])
def login():
    """
    Logs a user in
    """
    form = LoginForm()
    # Get the csrf_token from the request cookie and put it into the
    # form manually to validate_on_submit can be used
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        # Add the user to the session, we are logged in!
        user = User.query.filter(User.email == form.data['email']).first()
        login_user(user)
        return user.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}, 401

And the seed file:

def seed_users():
    demo = User(
        username='Demo', email='demo@aa.io', hashedPassword='password')
    marnie = User(
        username='marnie', email='marnie@aa.io', hashedPassword='password')
    bobbie = User(
        username='bobbie', email='bobbie@aa.io', hashedPassword='password')

    db.session.add(demo)
    db.session.add(marnie)
    db.session.add(bobbie)

    db.session.commit()

In my backend terminal I get:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'dict'

What the demoLogin function looks like now is after a bunch of trial and error, they were originally just strings, or the function wasn't async and didn't have an await. I've tried a bunch of different variations. I'd appreciate any help!

  • 1
    read [this](https://stackoverflow.com/questions/56808425/sqlalchemy-psycopg2-programmingerror-cant-adapt-type-dict) – Dean Van Greunen Jan 09 '22 at 06:42
  • 2
    Does this answer your question? [SQLAlchemy (psycopg2.ProgrammingError) can't adapt type 'dict'](https://stackoverflow.com/questions/56808425/sqlalchemy-psycopg2-programmingerror-cant-adapt-type-dict) – Dean Van Greunen Jan 09 '22 at 06:42
  • 1
    After reading those I ended up putting a conditional checking if the form.email.data == the demo email and if it is just set the state to that user. Seems like a bandaid fix for now! – Dylan Welzel Jan 09 '22 at 07:00

0 Answers0