I'm sorry to bother, since this problem has been already asked, but I tried everything that I found in the internet and I'm still not able to find the issue :'(
So basically, I did a flask app for controlling leds on my raspberry Pi and then I tried to implement a login system for it. Connecting to my account was not an issue, but when I try to access the interface for controlling the Leds I get ""GET /led_control/ HTTP/1.1" 302" and then redirected back again to login. The interface should only be accessed when I'm logged in.
This is the code for logging in:
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'username' in session:
return f(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('login'))
return wrap
#Login form
@app.route("/", methods = ["GET", "POST"])
@app.route("/login/", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form['username']
password = request.form['password']
if not (username and password):
flash("Username or Password cannot be empty.")
return redirect(url_for('login'))
else:
username = username.strip()
password = password.strip()
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password_hashed, password):
session[username] = True
return redirect(url_for("user", username=username))
else:
flash("Invalid username or password.")
else:
return render_template("login.html")
#User Home form
@app.route("/user/<username>/")
def user(username):
if not session.get(username):
abort(401)
return render_template("user.html", username=username)
When I click on the interface Button I get logged out
<h2> {{username.title()}} - Click here to access the interface!</h2>
<a href="{{ url_for('led_control')}}" role="button">Interface</a>
and this is the code for controlling the LEDs
@app.route("/led_control/")
@login_required
def led_control():
for p in pins:
pins[p]["state"] = GPIO.input(p)
template = { "pins" : pins}
return render_template("led_control.html", **template)
@app.route("/<activate>/<action>") # request url with pin number
def action(activate, action):
change = int(activate) # convert pin from url into int
device = pins[change]["name"]
if action == "on":
GPIO.output(change, GPIO.HIGH)
text = device + "is on"
if action == "off":
GPIO.output(change, GPIO.LOW)
text = device + "is off"
for p in pins:
pins[p]["state"] = GPIO.input(p)
template = {"pins" : pins}
return render_template("led_control.html", **template)
Thank you in advance for your time and effort.