Fixed first session login persistance issue

The first time a user logs into Airflow, it fails to persist the session.   This causes the login form to silently reload, leading a user to question wtf just happened.

The cause is the user_id is not populated from the auto-increment id after it is inserted into the database. I don’t have the time right now to investigate what is happening inside flask/alchemy to cause this, so I just did a quick fix of querying the database after committing the changes.  Since this only happens the first time an email is used to login, the extra load is basically irrelivant.

On a side note, I tried doing a “session.refresh(user)” after the “session.commit”, however this gives an error of “Instance '<User at 0x110f872b0>' is not persistent within this Session”, so it just redoes the query that would happen if a user logs in with a user that already exists in the airflow database.

Resolves https://github.com/astronomerio/engineering/issues/163
diff --git a/airflow/contrib/auth/backends/astronomer_auth.py b/airflow/contrib/auth/backends/astronomer_auth.py
index ed0f3e8..b0af533 100644
--- a/airflow/contrib/auth/backends/astronomer_auth.py
+++ b/airflow/contrib/auth/backends/astronomer_auth.py
@@ -201,6 +201,12 @@
                 username=username,
                 is_superuser=False)
 
+            # Add to database immediately, then query for the full object
+            session.merge(user)
+            session.commit()
+            user = session.query(models.User).filter(
+                models.User.username == username).first()
+
         session.merge(user)
         session.commit()
         flask_login.login_user(AstronomerUser(user))