![]() ![]() I then applied that decorator to any route that required login: surveys_create_page(): When that decorator sees that there is no current user, it forces a redirect to the auth blueprint's login route, and passes along the current URL as the next URL. Login_url = url_for("auth.login", next_url=request.url) To mark routes accordingly, I defined a login_required decorator: def decorated_function(*args, **kwargs): Typically, in an app with user authentication, logging in is required for some routes while optional for others. inject_user():įinally, I registered the new auth blueprint in the usual way: from th import bp as auth_bpĪpp.register_blueprint(auth_bp, url_prefix="") I also used the app.context_processor decorator to inject a user variable into every template that gets rendered. To make sure I could access the identity package's Auth object from any blueprint, I added it to app.config: (Ĭlient_credential=app.config, The global app configuration happens in the root _init_.py, mostly inside the create_app function. That gives the authentication-related pages a more consistent UI with the rest of the site. The templates in the auth blueprint now extend base.html, the only template in the root templates directory. Return redirect(auth.log_out(url_for("index", _external=True))) Next_url = session.pop("next_url", url_for("index")) Return render_template("auth/auth_error.html", result=result) Redirect_uri=url_for(".auth_response", auth_response(): That's a much nicer user experience in a large app. By remembering that, a user can login from any page and get redirected back to that page (versus getting redirected back to the index). However, I added a line to store next_url inside the current session. The routes in the blueprint are similar to those in the original sample. The auth blueprintįirst I added an auth blueprint to bundle up the auth-related routes and templates. To aid others in their integration, I'll walk through the notable bits. You can see my integration in the identity branch of flask-surveys-container-app, and look through the changes in the pull request. I have developed a few Blueprint-based apps lately, so I decided to attempt the integration myself. The ms-identity-python-webapp sample demonstrates how to use that flow in a minimal Flask web app.įlask developers often use Blueprints and Application factories to organize large Flask apps, and many requested guidance on integrating the authentication code with a Blueprint-based application. ![]() However, the author of that library recently developed a higher-level package, identity, to wrap the most common authentication flow. With some additional configuration, it even acts as a generic customer identity management solution.įor Python developers, msal has been the traditional package for interacting with the API. Over the last month, I've been working a lot with the Microsoft Graph API identity platform, which makes it possible for users to login to a website with a Microsoft 365 account. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |