flask-spaceauth/example.py

38 lines
1019 B
Python
Raw Normal View History

2021-09-15 20:43:18 +00:00
from flask import Flask, request, url_for, Markup, get_flashed_messages
from spaceauth import SpaceAuth, login_required, cap_required, current_user
2018-04-07 08:41:20 +00:00
2021-09-15 20:43:18 +00:00
app = Flask("spaceauth-example")
app.config["SECRET_KEY"] = "testing"
app.config["SPACEAUTH_CONSUMER_KEY"] = "17817145-de34-4547-b067-64632f04156a"
app.config["SPACEAUTH_CONSUMER_SECRET"] = "SbZGSw8UgV9uWXvQzxn10czBBTLpE7"
2018-04-07 08:41:20 +00:00
auth = SpaceAuth(app)
2021-09-15 20:43:18 +00:00
@app.route("/")
2018-04-07 08:41:20 +00:00
def index():
2021-09-15 20:43:18 +00:00
return Markup(
'<pre>%r</pre>Hey! <a href="%s">Login with spaceauth</a> / <a href="%s">Logout</a> / %r / <a href="%s">Members only space</a>'
) % (
get_flashed_messages(),
url_for("spaceauth.login"),
url_for("spaceauth.logout"),
current_user.get_id(),
url_for("profile"),
)
2018-04-07 08:41:20 +00:00
2021-09-15 20:43:18 +00:00
@app.route("/profile")
2018-04-07 08:41:20 +00:00
@login_required
def profile():
2021-09-15 20:43:18 +00:00
return Markup("Hey {}!").format(current_user.get_id())
2018-04-07 08:41:20 +00:00
2021-09-15 20:43:18 +00:00
@app.route("/staff")
@cap_required("staff")
2018-04-07 08:41:20 +00:00
def staff_only():
2021-09-15 20:43:18 +00:00
return "This is staff-only zone!"
2018-04-07 08:41:20 +00:00
if __name__ == "__main__":
app.run()