2017-06-16 6 views
1

ログインのFlask/pythonの検証がmysqlでうまく動作しません。私はGoogleとドキュメントを検索していましたが、同じことについてスタックオーバーフローに関する質問はほとんど見ていませんが、まだ答えはありません。フラスコ/ pythonの資格情報をmysqlで検証する方法は?

from flask import Flask, render_template, flash, request, url_for, redirect, session 
from content_management import Content 

#form validations 
from wtforms import Form, BooleanField, TextField, PasswordField, validators 
#to encrypt the password 
from passlib.hash import sha256_crypt 
#for SQL injection 
from MySQLdb import escape_string as thwart 
import gc 
from functools import wraps 
from mysql_connect import connection 

app = Flask(__name__) 

@app.route('/login/', methods=['GET','POST']) 
def login_page(): 
    error = '' 
    try: 
     c, conn = connection() 
     if request.method == "POST": 
      d = c.execute("SELECT * FROM clients WHERE email = (%s)", (thwart(request.form['email']),)) 
      d = c.fetchone()[2] 

      if request.form['password'] == d: 
       email = request.form['email'] 
       c.execute("SELECT * FROM clients WHERE email = (%s)", (thwart(email),)) 
       clients_table = c.fetchall() 
       clientcid = clients_table[0] 
       flash(clientcid) 
       phone = clients_table[1] 
       rating = clients_table[4] 
       conn.commit() 

       c.execute("SELECT * FROM cpersonals WHERE cid = (%s)", (clientcid,)) 
       cpersonals_table = c.fetchall() 
       first_name = cpersonals_table[1] 
       last_name = cpersonals_table[2] 
       address = cpersonals_table[3] 
       czip = cpersonals_table[4] 
       reg_date = cpersonals_table[5] 
       conn.commit() 

       c.close() 
       conn.close() 

       session['logged_in'] = 'client' 
       session['clientcid'] = clientcid 
       session['email'] = email 
       session['phone'] = phone 
       session['rating'] = rating 
       session['first_name'] = first_name 
       session['last_name'] = last_name 
       session['address'] = address 
       session['czip'] = czip 
       session['reg_date'] = reg_date 
       flash("You are now logged in.") 
       return redirect(url_for("dashborad")) 

      else: 
       error = "Invalid credentials, try again." 

     return render_template("login.html") 

枠組み

+1

DBにパスワードを保存していますので、パスワードハッシュを保存してください。 'werkzeug.security import generate_password_hash、check_password_hash'から取得します。 – Ejaz

答えて

0

これは完全な答えではありませんせずにMySQLで資格情報を検証する簡単な方法があるが、むしろ、いくつかの提案(それはあなたがあなたの問題を解決するために何をすべきかのコード例を示します) :

flask-sqlalchemmyのようなフラスコ拡張子を使用していないフラスコをデータベースに使用しています。SQLAlchemy(データベースツールキット)をPythonで使いやすくするライブラリです。 flask-login(ログインとログアウトを処理する機能、よくある「私を覚えてください」機能など)を使って、あなたのやろうとしていることはかなり簡単です。 flask-sqlalchemyとflask-loginを使用してデータベースからの資格情報を確認してユーザーのログインを処理するサンプルファイルを作成したい場合は、作成することができますが、これらのツールを使用するアプリケーションの例はたくさんあります。

私が言及したようなデータベースツールキットを使用すると、データベースシステムの移植性(sqlalchemyを使用するコードは通常、mysql、postgreSQL、sqliteなどの複数のデータベースバックエンドで動作します) 。

関連する問題