Skip to content

Commit 289089b

Browse files
committed
Merge branch 'refs/heads/main' into pooja/maps
2 parents 967f258 + f973695 commit 289089b

File tree

172 files changed

+4989
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+4989
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Authentication",
3+
"position": 1
4+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Auth Methods
3+
sidebar_position: 1
4+
5+
---
6+
7+
# Authentication Methods Overview
8+
9+
Authentication enables users to create accounts and log into your app, establishing a secure,
10+
verified connection. In the dynamic world of applications, users can authenticate using various
11+
methods, including **email login**, **OAuth**, and **phone authentication**, among others.
12+
13+
While each method has its unique features and advantages, they all share a common goal: enhancing
14+
security and verifying the identity of users to provide a safe and personalized user experience.
15+
16+
## Email Login Authentication
17+
18+
The email login method involves users registering with an email address and
19+
password.
20+
21+
Security in this approach is enhanced through **Email Verification**,
22+
where a link or code is sent to the user's email to confirm ownership. This step
23+
prevents unauthorized account creation and ensures that the user can recover
24+
their account and receive important communications.
25+
26+
![email-login.png](imgs%2Femail-login.png)
27+
28+
## OAuth (Open Authorization)
29+
30+
**OAuth** is a popular authentication protocol that enables users to authorize
31+
one
32+
application to interact with another on their behalf without revealing their
33+
password. This method is commonly used to allow applications to access service
34+
features or user information from other services, such as logging into a
35+
third-party app using Google or Facebook credentials.
36+
37+
By using OAuth, the user's
38+
login credentials stay secure with the original service provider, and only
39+
specific permissions are granted to third-party apps via access tokens. This
40+
approach minimizes the risk of exposing sensitive user data and streamlines the
41+
login process across various platforms.
42+
43+
## Phone Authentication
44+
45+
Another method is phone authentication, where a user's phone number is used as a
46+
form of identity verification. Upon registering or logging in, the user receives
47+
a text message with a verification code that must be entered to proceed. This
48+
method leverages the security of mobile networks and the uniqueness of phone
49+
numbers to ensure that the person attempting access is the legitimate owner of
50+
the account.
51+
52+
![phone-login.png](imgs%2Fphone-login.png)
53+
54+
## Anonymous Authentication
55+
56+
Anonymous Authentication allows users to interact with your application without
57+
signing in with permanent credentials, by creating temporary anonymous accounts.
58+
This method is beneficial for users who want to test services before committing
59+
to creating an account. If a user decides to sign up later, their anonymous
60+
account can be upgraded to a regular account, preserving their data and
61+
interactions.
62+
63+
Each anonymous session is typically isolated, with strict permissions to prevent
64+
access to sensitive features or user data. When upgrading to a full account,
65+
secure practices are used to link the anonymous data to the new authenticated
66+
profile, ensuring that no data leakage or unauthorized access occurs during the
67+
transition.
68+
69+
Each authentication method aims to balance user convenience with high security,
70+
ensuring that personal and sensitive data remains protected while providing a
71+
seamless user experience.
72+
73+
![anon-user.png](imgs%2Fanon-user.png)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
1.14 MB
Loading
54.4 KB
Loading
29.3 KB
Loading
446 KB
Loading
13 KB
Loading
66.4 KB
Loading
72.1 KB
Loading

0 commit comments

Comments
 (0)