Flask-login not sure how to make it work using sqlite3

So far, I have made my user object and my login function, but I donโ€™t understand the user_loader part at all. I am very confused, but here is my code, please point me in the right direction.

@app.route('/login', methods=['GET','POST']) def login(): form = Login() if form.validate(): user=request.form['name'] passw=request.form['password'] c = g.db.execute("SELECT username from users where username = (?)", [user]) userexists = c.fetchone() if userexists: c = g.db.execute("SELECT password from users where password = (?)", [passw]) passwcorrect = c.fetchone() if passwcorrect: #session['logged_in']=True #login_user(user) flash("logged in") return redirect(url_for('home')) else: return 'incorrecg pw' else: return 'fail' return render_template('login.html', form=form) @app.route('/logout') def logout(): logout_user() return redirect(url_for('home')) 

my user

 class User(): def __init__(self,name,email,password, active = True): self.name = name self.email = email self.password = password self.active = active def is_authenticated(): return True #return true if user is authenticated, provided credentials def is_active(): return True #return true if user is activte and authenticated def is_annonymous(): return False #return true if annon, actual user return false def get_id(): return unicode(self.id) #return unicode id for user, and used to load user from user_loader callback def __repr__(self): return '<User %r>' % (self.email) def add(self): c = g.db.execute('INSERT INTO users(username,email,password)VALUES(?,?,?)',[self.name,self.email,self.password]) g.db.commit() 

my database

 import sqlite3 import sys import datetime conn = sqlite3.connect('data.db')#create db with conn: cur = conn.cursor() cur.execute('PRAGMA foreign_keys = ON') cur.execute("DROP TABLE IF EXISTS posts") cur.execute("DROP TABLE IF EXISTS users") cur.execute("CREATE TABLE users(id integer PRIMARY KEY, username TEXT, password TEXT, email TEXT)") cur.execute("CREATE TABLE posts(id integer PRIMARY KEY, body TEXT, user_id int, FOREIGN KEY(user_id) REFERENCES users(id))") 

I also created LoginManager in my init . I'm not sure what to do next, but I know that I need to somehow configure this

 @login_manager.user_loader def load_user(id): return User.query.get(id) 

How do I configure this site code to work in my database?

EDIT: please let me know if this looks right or could be improved :)

  @login_manager.user_loader def load_user(id): c = g.db.execute("SELECT id from users where username = (?)", [id]) userid = c.fetchone() return userid @app.route('/login', methods=['GET','POST']) def login(): form = Login() if form.validate(): g.user=request.form['name'] g.passw=request.form['password'] c = g.db.execute("SELECT username from users where username = (?)", [g.user]) userexists = c.fetchone() if userexists: c = g.db.execute("SELECT password from users where password = (?)", [g.passw]) passwcorrect = c.fetchone() if passwcorrect: user = User(g.user, 'email', g.passw) login_user(user) flash("logged in") return redirect(url_for('home')) else: return 'incorrecg pw' else: return 'fail' return render_template('login.html', form=form) @app.route('/logout') def logout(): logout_user() return redirect(url_for('home')) import sqlite3 from flask import g class User(): def __init__(self,name,email,password, active = True): self.name = name self.email = email self.password = password self.active = active def is_authenticated(self): return True #return true if user is authenticated, provided credentials def is_active(self): return True #return true if user is activte and authenticated def is_annonymous(self): return False #return true if annon, actual user return false def get_id(self): c = g.db.execute('SELECT id from users where username = (?)', [g.user]) id = c.fetchone() return unicode(id) #return unicode id for user, and used to load user from user_loader callback def __repr__(self): return '<User %r>' % (self.email) def add(self): c = g.db.execute('INSERT INTO users(username,email,password)VALUES(?,?,?)',[self.name,self.email,self.password]) g.db.commit() 
+4
source share
3 answers

The user_loader callback function is a way to tell Flask-Login about how to look up the user ID from the database? Since you are using sqllite3, you need to implement the user_loader function to query your sqlite database and retrieve / return the username / username you saved. Sort of:

 @login_manager.user_loader def load_user(id): c = g.db.execute("SELECT username from users where username = (?)", [id]) userrow = c.fetchone() userid = userrow[0] # or whatever the index position is return userid 

When you call login_user (user), it calls the load_user function to determine the user ID.

Here's how the process thread works:

  • You verify that the user has entered the correct username and password by checking the database.

  • If the username / password matches, you need to get the "object" of the user from the user ID. your user object can be userobj = User (userid, email..etc.). Just create an instance.

  • Log in by pressing login_user (userobj).

  • Redirect everywhere, flash, etc.

+1
source

Are you using SQLAlchemy by accident? Here is an example of my model.py for a project that I had while using Sqlite3 and flash drives.

 USER_COLS = ["id", "email", "password", "age"] 

Have you created an engine?

 engine = create_engine("sqlite:///ratings.db", echo=True) session = scoped_session(sessionmaker(bind=engine, autocommit=False, autoflush=False)) Base = declarative_base() Base.query = session.query_property() Base.metadata.create_all(engine) 

Here is an example user class:

 class User(Base): __tablename__ = "Users" id = Column(Integer, primary_key = True) email = Column(String(64), nullable=True) password = Column(String(64), nullable=True) age = Column(Integer, nullable=True) def __init__(self, email = None, password = None, age=None): self.email = email self.password = password self.age = age 

Hope this helps you understand a little.

+1
source

With SQLAlchemy, it fully handles all the queries for you, so you donโ€™t need to write all this out.

But if you continue to use it that way, your user_loader function must create an object of class "User". How:

 userrow = c.fetchone() username = userrow[0] u = User(username=username) return u 
+1
source

Source: https://habr.com/ru/post/1479978/


All Articles