|
| 1 | +--- |
| 2 | +sidebar_label: Generated Code |
| 3 | +--- |
| 4 | + |
| 5 | +# Authentication: Generated Code |
| 6 | + |
| 7 | +In FlutterFlow, enabling Authentication is a very simple task. You can check the documentation for the same here but ideally it is just enabling Authentication in Settings, choose your Authentication Type and adding an Action to your desired Auth button. But behind the scenes, a lot of code generation happens to enable this function for you, lets go through it one by one. |
| 8 | + |
| 9 | +We will first discuss the base authentication architecture and then discuss the code changes when we choose custom authentication vs Firebase/Supabase auth. |
| 10 | + |
| 11 | +## File structure |
| 12 | +When we enable Authentication in the Settings dashboard, it creates the following folders in our file structure to manage custom authentication. |
| 13 | +``` |
| 14 | +lib/ |
| 15 | + auth/ |
| 16 | + custom_auth/ |
| 17 | + auth_util.dart |
| 18 | + custom_auth_manager.dart |
| 19 | + custom_auth_user_provider.dart |
| 20 | +``` |
| 21 | + |
| 22 | +Similarly, when we enable say Firebase authentication, the following files and folders are generated for you. |
| 23 | + |
| 24 | +``` |
| 25 | +lib/ |
| 26 | + auth/ |
| 27 | + firebase_auth/ |
| 28 | + auth_util.dart |
| 29 | + email_auth.dart (along with other providers) |
| 30 | + firebase_auth_manager.dart |
| 31 | + firebase_user_provider.dart |
| 32 | + auth_manager.dart |
| 33 | + base_auth_user_provider.dart |
| 34 | +``` |
| 35 | + |
| 36 | +:::info |
| 37 | +This documentation is exclusively focused on the generated code for Custom Authentication. For instructions on integrating custom authentication into your FlutterFlow app, please refer here. |
| 38 | +::: |
| 39 | + |
| 40 | +# Custom Auth Manager |
| 41 | +The most crucial component of our generated authentication system is the ``CustomAuthManager`` class. It is responsible for managing authentication session attributes such as the ``authenticationToken``, ``refreshToken``, ``tokenExpiration``, and user-specific attributes like `uid` and `userData`. |
| 42 | + |
| 43 | +This class provides essential functionalities including: |
| 44 | +`signIn()`: Handles user sign-in processes. |
| 45 | + |
| 46 | +`signOut()`: Manages user sign-out actions. |
| 47 | + |
| 48 | +`updateAuthUserData()`: Updates authentication and user data. |
| 49 | + |
| 50 | +`persistAuthData()`: Persists authentication data across sessions for persistent login capabilities. |
| 51 | + |
| 52 | +In addition to the `CustomAuthManager`, we have another important file in our authentication framework: `custom_auth_user_provider.dart. ` |
| 53 | + |
| 54 | +This file defines a class, ``<ProjectName>AuthUser``, to encapsulate the state of an authenticated user. It leverages BehaviorSubject from the [rxdart](https://pub.dev/packages/rxdart) package to manage a stream of the user object, enabling real-time updates to the user's authentication state. This stream is initially set with a user object that indicates a logged-out state. Subsequent authentication actions will update this stream, enabling real-time adjustments to any part of the application that depends on the user's authentication status. |
| 55 | + |
| 56 | +Building on our authentication framework, the `custom_auth_manager.dart` file brings in the currentUser variable, an instance of the ``<ProjectName>AuthUser`` class. This global reference allows for quick and centralized access to the currently signed-in user's information, enabling access to their authentication state across the application. |
| 57 | + |
| 58 | +The `loggedIn` property further simplifies verifying if a user is logged in by checking the currentUser's status. |
| 59 | + |
| 60 | +## Auth Manager Initialization |
| 61 | +Then, we have the auth_util file, which contains a singleton instance of `CustomAuthManager` |
| 62 | + |
| 63 | +``` |
| 64 | +final _authManager = CustomAuthManager(); |
| 65 | +CustomAuthManager get authManager => _authManager; |
| 66 | +``` |
| 67 | + |
| 68 | +The `authManager.initialize()` is called in `main()` before runApp is executed. |
| 69 | + |
| 70 | +The `initialize()` method creates an instance of SharedPreferences, preparing it for `authToken`, `refreshToken`, etc, and also handles the logic for token expiration, including the automatic logout when these tokens expire. |
| 71 | + |
| 72 | +:::info |
| 73 | +Also note that this initialization occurs only because the 'Persist Auth Sessions' option has been enabled in the Custom Authentication Settings. |
| 74 | + |
| 75 | +<img src="/img/persist-auth-session.png" alt="Alt text for the image" /> |
| 76 | + |
| 77 | +::: |
| 78 | + |
| 79 | +This file also offers easy-to-use getters for essential information such as the user's ID, login token, and other data. This setup simplifies the process of accessing and managing login details throughout your app. |
| 80 | + |
| 81 | +## Log in Implementation |
| 82 | +When the Log In action is activated by tapping a button, we initiate a series of operations behind the scenes to ensure a smooth login process. |
| 83 | + |
| 84 | +Upon calling the signIn method, it triggers the `_updateCurrentUser` method from `CustomAuthManager` internally. |
| 85 | + |
| 86 | +This method receives various parameters such as `authenticationToken`, `refreshToken`, `tokenExpiration`, `authUid`, and `userData`, updating the CustomAuthManager class's properties with these details. Consequently, this stores the current session's authentication and user information effectively. |
| 87 | + |
| 88 | +:::info |
| 89 | +To learn more about the concepts of Authentication Token, Refresh Token, and Token Expiry Time, please refer the [Concepts](concepts/token) doc. |
| 90 | +::: |
| 91 | + |
| 92 | +A new user object, marked as logged in (`loggedIn` set to true), along with the provided `authUid` and `userData`, is then added to the user object stream mentioned earlier. This update informs all the stream's subscribers about the changed user state, signaling that the user has successfully logged in. |
| 93 | + |
| 94 | +Additionally, the `persistAuthData` method is invoked to save the updated authentication details (tokens, expiration, user ID, etc.) for future sessions. |
| 95 | + |
| 96 | +After signing in, `context.goNamedAuth('AuthPage', context.mounted);` is called that navigates the user to the Logged In Page specified in FlutterFlow's Authentication Settings. |
| 97 | + |
0 commit comments