So I am trying to make an authentication system using OAuth, Flask and Postgres. I'm not too sure what is going on with my code. I created a class for the database which would house the connection and create cursors whenever a query is made. I also made a class for the user which would send queries to be executed on the database class.
This is what I am trying to do:
- In the main application, we check if the user has been authenticated
- If not, we go through the process of OAuth/OpenConnect
- Once we get the credentials from the Google API, we check the database if there was anything stored with the id from the response with a method from the user class
- If no id was found, we insert into the database using the user class method to make a query
- Then we use flask-login methods,
@login_manager.user_loaderwhich then calls the user class method to SELECT from the database - Where my issue is, it appears that I am unable to retrieve the data
Some resources I have tried to follow to fix this problem:
- should I reuse the cursor in the python MySQLdb module
- Executing different queries using mysql-python
- PostgreSQL/performance one general cursor or create for every query
Not sure what's wrong.. I have psql open on my terminal and when I run the SELECT, I do in fact see the entry with the correct id.. But my application isn't able to fetch the data.
db.py
import psycopg2
from psycopg2 import Error
class Database:
def __init__(self, db, user, password, port, host):
self.db = db
self.user = user
self.password = password
self.port = port
self.host = host
def connect(self):
self.connection = psycopg2.connect(
database=self.db,
user=self.user,
password=self.password,
port=self.port,
host=self.host
)
def executeQuery(self, query, values=None):
print('query:', query)
print('values:', values)
cursor = self.connection.cursor()
cursor.execute(query, values)
self.connection.commit()
cursor.close()
def close(self):
self.connection.close()
user.py
from flask_login import UserMixin
from db import db
class User(UserMixin):
def __init__(self, id, name, email, profile_pic):
self.id = id
self.name = name
self.email = email
self.profile_pic = profile_pic
@staticmethod
def get(user_id):
user = db.executeQuery('SELECT * FROM users WHERE id=%s;', (user_id,))
print('user:', user)
if not user:
return None
current_user = user.fetchone()
user = User(
id = current_user[0],
name = current_user[1],
email = current_user[2],
profile_pic = current_user[3]
)
return user
@staticmethod
def create(id, name, email, profile_pic):
db.executeQuery(
'INSERT INTO users (id, name, email, profile_pic) VALUES (%s,%s,%s,%s);', (id, name, email, profile_pic,)
)
Documentation for Psycopg2:
https://www.psycopg.org/docs/usage.html
Maybe, my design for the database and user class is completely off. Feel free to let me know if I need to provide more information!