I can't implement API in flask token based authorization 2.o

102 views Asked by At

Need help to implement token based authorization steps and solution. I am using flask as back-end and html and CSS front-end. But i am facing difficulty to find best content about api. my purpose is to build user login and registrations

This is my code

#database MySQL

from flask import Flask, render_template, request, redirect, url_for,jsonify
import db
import datetime
import time
app = Flask(__name__)
cursor, conn = db.connection(app)



@app.route("/")
def index():
    return render_template("signup.html", title="SignUp")

@app.route("/signUp", methods = ["POST"])

def signUp():
    username=str(request.form["user"])
    password = str(request.form["password"])
    email = str(request.form["email"])
    cursor = conn.cursor()
    cursor.execute("INSERT INTO users(name, password, email)VALUES(%s, %s, %s)", (username, password, email))
    conn.commit()
    conn.close()
    return redirect(url_for("login"))

@app.route("/login")
def login():
    return render_template("login.html", title="data")

@app.route("/checkUser", methods=["POST"])
def check():
    username = str(request.form["user"])
    password = str(request.form["password"])
    cursor.execute("SELECT * FROM users where name = %s and password=%s", (username, password))
    users = cursor.fetchone()
    if users is None:
        return "username is wrong"
    else:
        return render_template("home.html")
@app.route("/home")
def home():
    return render_template("home.html")


if __name__ == "__main__":``
    app.run(debug=True)
1

There are 1 answers

0
Shan Ali Khan On
db.py

from flaskext.mysql import MySQL
import config

def connection(app):
    mysql = MySQL()
    app.config['MYSQL_DATABASE_USER'] = config.dbuser
    app.config['MYSQL_DATABASE_PASSWORD'] = config.dbpassword
    app.config['MYSQL_DATABASE_DB'] = config.dbname
    app.config['MYSQL_DATABASE_HOST'] = config.dbhost
    mysql.init_app(app)
    conn = mysql.connect()
    cursor = conn.cursor()
    return cursor, conn

#config.py
dbuser = 'root'
dbpassword = ''
dbname = 'login_data'
dbhost = 'localhost'
debug = True
secret = 'flask-auth'
port = 8080