Skip to content

Commit 569525a

Browse files
authored
Authentication Changes (#6)
* Add auth file Signed-off-by: PoojaB26 <[email protected]> * Add assets & update CSS * Add Authentication directories * Add Firebase Setup changes Signed-off-by: PoojaB26 <[email protected]> * Add firebase related imgs Signed-off-by: PoojaB26 <[email protected]> * Add landscape style css * Add Firestore + Billing setup info * Add Google Auth (Firebase) docs * Fix h3 CSS * Script: Convert links to html tags * Update Google Auth docs * Add Email Auth * Move Firebase Auth under How-Tos * Move imgs to folder * Add authentication approaches concept * Add verification info * Add anon auth how-tos * Fix admonitions & formatting * Shuffle the auth sections and address review comments * Revert changes * Add images for auth methods * Rename file * Fix slug * Fix formatting issues * Move supabase docs to correct folder --------- Signed-off-by: PoojaB26 <[email protected]>
1 parent 3949c1f commit 569525a

Some content is hidden

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

60 files changed

+1005
-3
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+
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Types",
3+
"position": 2
4+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Authentication Types
3+
sidebar_position: 1
4+
---
5+
6+
# Authentication Types
7+
FlutterFlow provides native support for a variety of Authentication Services, including **Firebase**, **Supabase**, and **Custom Authentication** options. To integrate these services into your app, simply navigate to 'App Settings,' select 'Authentication,' and then choose your preferred service. From there, you can set up initial pages for both entry and logged-in states. Follow any additional steps as necessary to complete the setup.
8+
9+
## Firebase Authentication
10+
11+
In FlutterFlow, you can seamlessly connect with **Firebase** and utilize the available authentication
12+
methods.
13+
14+
Firebase Authentication integrates tightly with other Firebase services, leveraging industry
15+
standards like OAuth 2.0 and OpenID Connect. This makes it highly adaptable for use with your custom
16+
backend, ensuring a secure and scalable solution.
17+
18+
:::info
19+
Learn how to enable Firebase Authentication and integrate popular auth providers in your FlutterFlow
20+
app. [Learn more here](#)
21+
:::
22+
23+
## Supabase Authentication
24+
25+
In FlutterFlow, you can also integrate Supabase to manage authentication efficiently.
26+
27+
Supabase provides a powerful and flexible authentication solution, similar to Firebase but with some
28+
unique advantages like support for PostgreSQL.
29+
30+
:::info
31+
Discover how to set up Supabase Authentication and link it with available auth providers within your
32+
FlutterFlow app. [Learn more here](#)
33+
:::
34+
35+
## Custom Authentication
36+
37+
In FlutterFlow, you have the flexibility to implement custom authentication solutions tailored to
38+
your specific needs. This allows for a highly personalized approach to security, enabling you to
39+
design and integrate authentication mechanisms that perfectly fit the unique requirements of your
40+
application. Whether you need to work with legacy systems or have specific security protocols,
41+
custom authentication provides the necessary control.
42+
43+
:::info
44+
Explore how to implement custom authentication strategies in your FlutterFlow app, ensuring your
45+
authentication flow aligns with your business requirements. [Learn more here](#)
46+
:::
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Custom Auth",
3+
"position": 3
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: custom-auth
3+
sidebar_position: 3
4+
---
5+
6+
# Enabling custom authentication [TODO / Different PR]
7+
To enable custom authentication in FlutterFlow:
8+
- Open Setting and Integrations > App Settings > Authentication.
9+
- Turn on the **Enable Authentication** toggle and set Authentication Type to Custom.
10+
- To ensure that your users are directed to the appropriate pages based on their login status, you must set the initial pages.
11+
- By default, the **Persist Auth Sessions** option is enabled, which means users remain logged in until they actively log out. With this option enabled, your app will automatically open to the homepage whenever it's restarted.
12+
13+
<iframe src="https://demo.arcade.software/pKfPIx8T6uEbds8xon1t?embed&show_copy_link=true" title="app.flutterflow.io/authentication" frameborder="0" loading="lazy" webkitallowfullscreen mozallowfullscreen allowfullscreen allow="clipboard-write" width="100%" height="600"></iframe>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
id: auth-token
3+
sidebar_label: Tokens
4+
sidebar_position: 1
5+
---
6+
7+
# Tokens: Types and Lifespans
8+
9+
Before diving into **Authentication**, let's first clarify some key terms we'll encounter. These
10+
concepts are essential for understanding how secure access and user verification work.
11+
12+
- **Authentication Token**: An Auth Token acts as a digital key provided to
13+
client apps upon successful login. This key verifies the user's identity for subsequent actions
14+
within the app, eliminating the need for repeated login prompts. It ensures secure and streamlined
15+
access to the app's features.
16+
17+
- **Refresh Token**: This token functions as a secondary mechanism to renew the
18+
authentication token without requiring the user to re-enter login credentials. This is
19+
particularly useful for maintaining a user's session securely, even when the primary
20+
authentication token expires, enhancing both security and user experience.
21+
22+
- **Token Expiry Time** : This refers to the lifespan of an authentication token.
23+
It defines the period during which the token remains valid. This is a critical security measure to
24+
prevent unauthorized access, ensuring that tokens are regularly refreshed and authenticated.
25+
26+
Developers must save these values in the persisted app state to keep it secure.
27+
28+
:::warning[Remember]
29+
Not all login APIs will return an Auth Token, Refresh Token, and Token Expiry Time. The details of
30+
the response depend on the configuration of your backend API. Always check your API documentation to
31+
understand what is returned upon successful authentication.
32+
:::
33+
34+
<figure>
35+
![login-request.png](..%2F..%2Fimgs%2Flogin-request.png)
36+
<figcaption class="centered-caption">1.1 An example of a Login API transaction that
37+
returns the above values in its response</figcaption>
38+
</figure>
39+
40+
### Example of Auth using Tokens
41+
42+
For example, when a user logs in using a Login API, the server verifies the submitted credentials
43+
and typically responds with both an **Auth Token** and a **Refresh Token** (both strings). The
44+
authentication token is short-lived; for instance, it may be valid for 1 hour and is used to
45+
authenticate API requests (_as shown in diagram 1.1_).
46+
47+
If you need to use the token, it is typically included in any authenticated API request, such as
48+
accessing a list of users available only to logged-in users. In this case, the app sends a request
49+
to the API with a header that includes an Authorization header containing the authentication token.
50+
51+
<figure>
52+
![token-success-request.png](..%2F..%2Fimgs%2Ftoken-success-request.png)
53+
<figcaption class="centered-caption">An example of an Authenticated API request that
54+
sends a Authorization Bearer Token that uses the saved Auth Token</figcaption>
55+
</figure>
56+
57+
After an hour, or as determined by the token's expiry time, the authentication token expires. Any
58+
attempt to access another post with the expired token will result in a 401 Unauthorized response.
59+
60+
To retrieve a new token, a common practice involves the client app making a request to a specific
61+
endpoint, submitting the saved refresh token in the request body. The server then validates the
62+
refresh token and responds with a new authentication token.
63+
64+
![token-fail-request.png](..%2F..%2Fimgs%2Ftoken-fail-request.png)
65+
66+
Thanks to the refresh token, users can continue accessing the app without needing to log in again.
67+
This process enhances security by limiting the lifespan of each token while ensuring a seamless
68+
experience for the user.
69+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Firebase Auth",
3+
"position": 2
4+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: Anonymous Login
3+
sidebar_position: 3
4+
---
5+
6+
# Anonymous Login
7+
8+
:::info[Prerequisites]
9+
10+
Before getting started with this section, ensure you have:
11+
12+
- Completed all steps in [**Firebase Setup**](..%2F..%2F..%2FFirebase%2FConnect%20to%20Firebase%20Setup.md).
13+
14+
- Completed [**Initial Setup**](docs/Integrations/Authentication/How-Tos/Firebase-Auth/initial-setup.md)
15+
required for authentication.
16+
17+
- Learn more about the concepts
18+
of [**Anonymous Authentication**](docs/Integrations/Authentication/Concepts/authentication-approaches.md)
19+
:::
20+
21+
## Enable Anonymous Authentication in Firebase
22+
23+
To enable Anonymous authentication, first go to your Firebase console and enable
24+
the authentication provider:
25+
26+
<iframe src="https://demo.arcade.software/rzWEzk1DdYGG7V5AA8pd?embed&show_copy_link=true" title="EcommerceFlow - Authentication - Sign-in method - Firebase console" frameborder="0" loading="lazy" webkitallowfullscreen mozallowfullscreen allowfullscreen allow="clipboard-write" width="100%" height="600"></iframe>
27+
28+
## Add Anonymous Login Action
29+
30+
1. On the button designated for anonymous authentication, add a new Action.
31+
32+
2. Search for and select the **Log In** action (located under Backend/Database > Firebase
33+
Authentication).
34+
35+
3. Set the Auth Provider to **Anonymous**.
36+
37+
4. Enable the **Create User Document** toggle and set the Collection to _users_. This action will
38+
create an entry for the user in the database without any details upon successful login.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Apple Login
3+
sidebar_position: 4
4+
---
5+
6+
# Apple Login

0 commit comments

Comments
 (0)