Last active
May 29, 2022 08:15
-
-
Save LordGhostX/01e9330dc4533a992a481fcd58fdd115 to your computer and use it in GitHub Desktop.
Shows how LoginRadius services can be integrated into a Flask application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import * | |
from LoginRadius import LoginRadius as LR | |
app = Flask(__name__) | |
app.config["SECRET_KEY"] = "SECRET_KEY" | |
LR_AUTH_PAGE = "https://<APP_NAME>.hub.loginradius.com/auth.aspx?action={}&return_url={}" | |
LR.API_KEY = "LR_API_KEY" | |
LR.API_SECRET = "LR_API_SECRET" | |
loginradius = LR() | |
@app.route("/") | |
def index(): | |
return "Hello World!" | |
@app.route("/register/") | |
def register(): | |
# redirect the user to our LoginRadius register URL | |
return redirect(LR_AUTH_PAGE.format("register", request.host_url[:-1] + url_for("login"))) | |
@app.route("/login/") | |
def login(): | |
access_token = request.args.get("token") | |
if access_token is None: | |
# redirect the user to our LoginRadius login URL if no access token is provided | |
return redirect(LR_AUTH_PAGE.format("login", request.base_url)) | |
# fetch the user profile details with their access tokens | |
result = loginradius.authentication.get_profile_by_access_token(access_token) | |
if result.get("ErrorCode") is not None: | |
# redirect the user to our login URL if there was an error | |
return redirect(url_for("login")) | |
session["user_acccess_token"] = access_token | |
return redirect(url_for("dashboard")) | |
@app.route("/dashboard/") | |
def dashboard(): | |
access_token = session.get("user_acccess_token") | |
if access_token is None: | |
return redirect(url_for("login")) | |
# fetch the user profile details with their access tokens | |
result = loginradius.authentication.get_profile_by_access_token(access_token) | |
if result.get("ErrorCode") is not None: | |
# redirect the user to our login URL if there was an error | |
return redirect(url_for("login")) | |
return jsonify(result) | |
@app.route("/logout/") | |
def logout(): | |
access_token = session.get("user_acccess_token") | |
if access_token is None: | |
return redirect(url_for("login")) | |
# invalidate the access token with LoginRadius API | |
loginradius.authentication.auth_in_validate_access_token(access_token) | |
session.clear() | |
return "You have successfully logged out!" | |
if __name__ == "__main__": | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment