from
flask
import
Flask, request, redirect, render_template
from
werkzeug.security
import
generate_password_hash, check_password_hash
app
=
Flask(__name__)
users
=
[]
@app
.route(
'/register'
, methods
=
[
'GET'
,
'POST'
])
def
register():
if
request.method
=
=
'POST'
:
username
=
request.form[
'username'
]
password
=
request.form[
'password'
]
password_hash
=
generate_password_hash(password)
users.append({
'username'
: username,
'password_hash'
: password_hash})
return
redirect(
'/login'
)
return
render_template(
'register.html'
)
@app
.route(
'/login'
, methods
=
[
'GET'
,
'POST'
])
def
login():
if
request.method
=
=
'POST'
:
username
=
request.form[
'username'
]
password
=
request.form[
'password'
]
user
=
next
((u
for
u
in
users
if
u[
'username'
]
=
=
username),
None
)
if
user
and
check_password_hash(user[
'password_hash'
], password):
return
redirect(
'/'
)
return
render_template(
'login.html'
, error
=
'Invalid username or password'
)
return
render_template(
'login.html'
)