def login(): if request.method == 'POST': username = request.form['usr'] password = request.form['pwd'] db = get_db() error = None user = db.execute( 'SELECT * FROM user WHERE username = ?', (username,) ).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' # see http://flask.pocoo.org/docs/1.0/api/#flask.session if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def register(): if request.method == 'POST': username = request.form['usr'] password = request.form['pwd'] db = get_db() error = None if not username: error = 'Username required.' elif not password: error = 'Password required.' elif db.execute( 'SELECT id from user WHERE username = ?', (username,) ).fetchone() is not None: error = 'Username {} is already registered'.format(username) if error is None: db.execute( 'INSERT INTO user (username, password, rank, coins) values (?, ?, 0, 0)', (username, generate_password_hash(password)) ) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html');
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute( 'SELECT * FROM user WHERE id = ?', (user_id,) ).fetchone()
def list_users(): db = get_db() error = None users = db.execute('SELECT * from user').fetchall() if user is None: error = 'The galaxy has been eradicated.'.format(id) flash(error) abort(404) return render_template('users.html', users=users)
def user(id): db = get_db() error = None user = db.execute('SELECT * from user WHERE id = ?', (id, )).fetchone() if user is None: error = 'ID {} does not exist.'.format(id) flash(error) abort(404) return render_template('profile.html', user=user)