Authentication
Authentication pages to authenticate authorized users
Login the user->127.0.0.1:5000/api/loginRegister new user->127.0.0.1:5000/api/registerLogout existing user->127.0.0.1:5000/api/logout
Login
Login function is implemented at main.py:
Whenever user presses the login button, it will make a call to /api/login URL endpoint and login the user by the session created by Flask.
Method: HTTP POST
| Parameters | Data Type |
|---|---|
| String | |
| pass | String |
Sample input
email=testing123@hotmail.com
pass=iamwarrior12345
@app.route("/api/login", methods=["POST"])
def api_login():
if request.method == "POST": # Only if data has been posted
result = request.form # Get the data
email = result["email"]
password = result["pass"]
try:
# Try signing in the user with the given information
user = auth.sign_in_with_email_and_password(email, password)
except:
# If there is any error, redirect back to login
return Response(
json.dumps({"error": "Wrong username/password"}),
status=400,
mimetype="application/json",
)
session["email"] = email
return Response(
json.dumps({"success": "Successful authentication"}),
status=200,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "Method not POST"}),
status=400,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 otherwise
Sample JSON output
{"success": "Successful authentication"}
{"error": "Wrong username/password"}
{"error": "Method not POST"}
Signup
Registration function is implemented at main.py:
Whenever user presses the register button, it will make a call to /api/register URL endpoint and login the user upon successful registration.
Method: HTTP POST
| Parameters | Data Type |
|---|---|
| String | |
| pass | String |
email=testing123@hotmail.com
pass=iamwarrior12345
@app.route("/api/register", methods=["POST"])
def api_register():
if request.method == "POST": # Only if data has been posted
result = request.form # Get the data submitted
email = result["email"]
password = result["pass"]
try:
# Try creating the user account using the provided data
auth.create_user_with_email_and_password(email, password)
# Login the user
user = auth.sign_in_with_email_and_password(email, password)
session["email"] = email
return Response(
json.dumps({"success": "Successful registration"}),
status=200,
mimetype="application/json",
)
except:
# If there is any error, display
return Response(
json.dumps({"error": "Error in registration"}),
status=400,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "Method not POST"}),
status=400,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 otherwise
Sample JSON output
{"success": "Successful registration"}
{"error": "Error in registration"}
{"error": "Method not POST"}
Logout
Logout function is implemented at main.py:
Whenever user presses the logout button, it will make a call to /api/logout URL endpoint and logout the user.
Method: HTTP GET
@app.route("/api/logout", methods=["GET"])
def api_logout():
if request.method == "GET":
# remove the email from the session if it is there
session.pop("email", None)
return Response(
json.dumps({"success": "Successfully logged out"}),
status=200,
mimetype="application/json",
)
else:
return Response(
json.dumps({"error": "Method not GET"}),
status=400,
mimetype="application/json",
)
The response will be a JSON output with a HTTP Status code, showing 200 on success and 400 otherwise
Sample JSON output
{"success": "Successfully logged out"}
{"error": "Method not GET"}