Implement a simple logout mechanism
I would have done a DELETE to /login/ to make it like a resource but my browser won't do a raw <form> with method="DELETE" :(
This commit is contained in:
parent
94b78dacca
commit
861b6f0f71
|
@ -2,6 +2,9 @@
|
||||||
<body>
|
<body>
|
||||||
{% if current_user and current_user.is_authenticated %}
|
{% if current_user and current_user.is_authenticated %}
|
||||||
<h1>Hello {{ current_user.name }} from Vanth</h1>
|
<h1>Hello {{ current_user.name }} from Vanth</h1>
|
||||||
|
<form action="/logout/" method="POST">
|
||||||
|
<input type="submit">Log out</input>
|
||||||
|
</form>
|
||||||
{% else %}
|
{% else %}
|
||||||
<h1>Please log in</h1>
|
<h1>Please log in</h1>
|
||||||
<form action="/login/" method="POST">
|
<form action="/login/" method="POST">
|
||||||
|
|
|
@ -18,10 +18,16 @@ def load_user(user_id):
|
||||||
def login():
|
def login():
|
||||||
if flask.request.method == 'GET':
|
if flask.request.method == 'GET':
|
||||||
return flask.render_template('index.html')
|
return flask.render_template('index.html')
|
||||||
else:
|
elif flask.request.method == 'POST':
|
||||||
user = vanth.user.load(uuid.uuid4())
|
user = vanth.user.load(uuid.uuid4())
|
||||||
flask_login.login_user(user)
|
flask_login.login_user(user)
|
||||||
return flask.redirect('/')
|
elif flask.request.method == 'DELETE':
|
||||||
|
flask_login.logout_user()
|
||||||
|
return flask.redirect('/')
|
||||||
|
|
||||||
|
def logout():
|
||||||
|
flask_login.logout_user()
|
||||||
|
return flask.redirect('/')
|
||||||
|
|
||||||
def create_app(config):
|
def create_app(config):
|
||||||
app = flask.Flask('vanth', template_folder='../templates')
|
app = flask.Flask('vanth', template_folder='../templates')
|
||||||
|
@ -40,7 +46,8 @@ def create_app(config):
|
||||||
)
|
)
|
||||||
|
|
||||||
app.route('/', methods=['GET'])(index)
|
app.route('/', methods=['GET'])(index)
|
||||||
app.route('/login/', methods=['GET', 'POST'])(login)
|
app.route('/login/', methods=['GET', 'POST', 'DELETE'])(login)
|
||||||
|
app.route('/logout/', methods=['POST'])(logout)
|
||||||
|
|
||||||
sepiida.endpoints.add_resource(app, vanth.api.about.About, endpoint='about')
|
sepiida.endpoints.add_resource(app, vanth.api.about.About, endpoint='about')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue