Advertisement
sriyanto

route_login

Oct 12th, 2022
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. @app.route('/', methods=['GET', 'POST'])
  2. def login():
  3.     # Output message if something goes wrong...
  4.     msg = ''
  5.     # Check if "username" and "password" POST requests exist (user submitted form)
  6.     if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
  7.         # Create variables for easy access
  8.         username = request.form['username']
  9.         password = request.form['password']
  10.         # Check if account exists using MySQL
  11.         #cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
  12.         cursor.execute('SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password,))
  13.         # Fetch one record and return result
  14.         account = cursor.fetchone()
  15.         # If account exists in accounts table in out database
  16.         if account:
  17.             # Create session data, we can access this data in other routes
  18.             session['loggedin'] = True
  19.             session['id'] = account['id']
  20.             session['username'] = account['username']
  21.             # Redirect to home page
  22.            # return 'Logged in successfully!'
  23.             return redirect(url_for('home'))
  24.         else:
  25.             # Account doesnt exist or username/password incorrect
  26.             msg = 'Incorrect username/password!'
  27.     # Show the login form with message (if any)
  28.     return render_template('index.html', msg=msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement