<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohamed ali Adweni on Medium]]></title>
        <description><![CDATA[Stories by Mohamed ali Adweni on Medium]]></description>
        <link>https://medium.com/@mouhamedaliadoinii?source=rss-bb0175c2cba6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*w-rpB16BcKbTW41g</url>
            <title>Stories by Mohamed ali Adweni on Medium</title>
            <link>https://medium.com/@mouhamedaliadoinii?source=rss-bb0175c2cba6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:00:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mouhamedaliadoinii/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mastering Flutter Authentication: A Step-by-Step Guide with Strapi and GetX]]></title>
            <link>https://medium.com/@mouhamedaliadoinii/mastering-flutter-authentication-a-step-by-step-guide-with-strapi-and-getx-17564a5f3ed0?source=rss-bb0175c2cba6------2</link>
            <guid isPermaLink="false">https://medium.com/p/17564a5f3ed0</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[strapi]]></category>
            <category><![CDATA[flutter-app-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Mohamed ali Adweni]]></dc:creator>
            <pubDate>Sun, 25 Feb 2024 19:22:54 GMT</pubDate>
            <atom:updated>2024-02-26T16:03:06.193Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*htz4UX1343-CDwSCtiAsGA.png" /></figure><p><strong>Prerequisites</strong></p><p>Before proceeding with this article, ensure you have the following:</p><ul><li><strong>Node.js:</strong> Make sure Node.js is installed on your computer. You can download it from the official website: <a href="https://nodejs.org/">Node.js</a>.</li><li><strong>Flutter Local Development Setup:</strong> Set up Flutter for local development. If you haven’t already installed Flutter, you can find installation instructions in the Flutter documentation: Flutter Installation Guide.</li></ul><p>Having these prerequisites in place will help you follow along with the steps outlined in this guide seamlessly. Let’s dive into mastering Flutter authentication with Strapi and GetX!</p><h3><strong>Setting up Strapi CMS</strong></h3><p>To establish an Authentication server using Strapi on your local machine, follow these steps:</p><p><strong>Install Strapi:</strong></p><p>Strapi simplifies the process of bootstrapping a server for API development. Start by navigating to your desired directory in your terminal or command prompt and execute the following command:</p><p>This command creates a new directory named strapi-auth-flutter and installs all necessary Strapi CMS dependencies.</p><p><strong>Navigate to the New Directory:</strong></p><p>After the installation process completes, navigate to the newly created directory using the following command:</p><pre>cd strapi-auth</pre><p><strong>Initiate Development Environment:</strong></p><p>Start the development environment by running the following command:</p><pre>npm run develop</pre><p>This command will initiate the Strapi server locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o94Ed07aSapGZC4o7SMKHg.png" /></figure><p><strong>Access Strapi Authentication Page:</strong></p><p>Once the server is up and running, open your web browser and navigate to http://localhost:1337/admin. You will be directed to the Strapi authentication page.</p><p><strong>Log in or Sign Up:</strong></p><p>On the Strapi authentication page, you’ll be prompted to either log in if you have an existing account or sign up if you’re a new user.</p><p><strong>Verification:</strong></p><p>Upon successful login or signup, you will gain access to the Strapi dashboard, confirming that the setup process was successful.</p><p>With Strapi CMS set up on your local machine, you’re now ready to configure authentication settings and manage your API development effortlessly. Let’s proceed with integrating Strapi APIs into your Flutter application for authentication.</p><h3><strong>Setting up the Flutter Application</strong></h3><p>To start your Flutter project, follow these steps:</p><p><strong>Create a Flutter Project:</strong></p><p>Open your terminal or command prompt and execute the following command from your desired directory:</p><pre>flutter create flutter_auth_project</pre><p>This command will create a new Flutter project named flutter_auth_project.</p><p><strong>Navigate to the Project Directory:</strong></p><p>After the project has been created, move into the newly created directory using the following command:</p><pre>cd flutter_auth_project</pre><p><strong>Run the Project:</strong></p><p>To confirm that Flutter is functioning correctly and to launch the basic Flutter application, execute the following command:</p><pre>flutter run</pre><p>This command will start the Flutter application in your testing environment, such as a mobile device emulator or a web browser.</p><p>By following these steps, you’ll have successfully initiated your Flutter project and verified its functionality. Now you’re ready to integrate Strapi APIs and implement authentication features using GetX in your Flutter application. Let’s proceed with the integration process.</p><p><strong>After Adding GetX to pubspec.yaml</strong></p><p>Once you’ve added GetX to your pubspec.yaml file, make sure to run flutter pub get in your terminal or IDE to fetch the dependencies and integrate GetX into your Flutter project.</p><p>This command will download and install the GetX package along with its dependencies into your Flutter project.</p><p>With GetX successfully added to your project, you’re now ready to leverage its powerful state management and navigation capabilities to streamline authentication implementation. Let’s proceed with integrating Strapi APIs and building authentication screens in Flutter.</p><p>To change your Flutter project’s main function to use GetX for navigation, you’ll need to make some adjustments in your project files. Here’s how you can do it:</p><ol><li><strong>Import Get package:</strong> Ensure that you’ve imported the Get package at the top of your main.dart file:</li></ol><pre>import &#39;package:flutter/material.dart&#39;; <br>import &#39;package:get/get.dart&#39;; // Add this line</pre><p><strong>Replace MaterialApp with GetMaterialApp:</strong> Replace MaterialApp with GetMaterialApp in your main function. GetMaterialApp is the GetX equivalent of MaterialApp and provides additional functionalities for navigation.</p><p><strong>Remove unnecessary MaterialApp imports:</strong> You can remove any imports related to MaterialApp as you&#39;re now using GetMaterialApp.</p><p>Here’s how your main.dart file should look after making these changes:</p><pre>import &#39;package:flutter/material.dart&#39;;<br>import &#39;package:get/get.dart&#39;;</pre><pre>void main() =&gt; runApp(GetMaterialApp(<br>      home: SignInView(),<br>      builder: EasyLoading.init(),<br>    ));</pre><p>With these changes, you’ve switched your Flutter project to use GetX for navigation. Now you can take advantage of GetX’s powerful state management and navigation features in your Flutter application.</p><h4>Data Management: User Model and API Requests :</h4><pre>lib<br>└── data<br>    ├── model<br>    │   └── user.dart<br>    └── remote<br>        └── api_request.dart</pre><h3>User Model</h3><p>user.dart:</p><pre>class User {<br>  final String id;<br>  final String username;<br>  final String email;<br>User({required this.id, required this.username, required this.email});<br>}</pre><h3>ApiRequest Class</h3><p>api_request.dart:</p><pre>import &#39;package:dio/dio.dart&#39;;<br>import &#39;package:dio_http_cache/dio_http_cache.dart&#39;;<br>import &#39;package:flutter/foundation.dart&#39;;<br>import &#39;package:get_storage/get_storage.dart&#39;;<br><br>import &#39;../../constants/strings/string.dart&#39;;<br><br>class ApiRequest {<br>  final String url;<br>  Map&lt;String, String&gt;? queryParameters;<br>  var data;<br>  final box = GetStorage();<br><br>  ApiRequest({<br>    required this.url,<br>    this.queryParameters,<br>    this.data,<br>  });<br><br>  Dio _dio() {<br>    var dio = Dio(BaseOptions(<br>      baseUrl: &quot;${Constants.BASE_URL}/api/&quot;,<br>      connectTimeout: 5000000,<br>      receiveTimeout: 5000000,<br>    ));<br>    dio.interceptors.add(DioCacheManager(CacheConfig()).interceptor);<br>    var token = box.read(&#39;token&#39;);<br>    dio.interceptors.add(<br>      InterceptorsWrapper(<br>        onRequest: (request, handler) {<br>          if (token != null &amp;&amp; token != &#39;&#39;) {<br>            request.headers[&#39;Authorization&#39;] = &#39;Bearer $token&#39;;<br>          }<br>          return handler.next(request);<br>        },<br>      ),<br>    );<br>    return dio;<br>  }<br><br>  get() async {<br>    try {<br>      var res = (await _dio().get(url, queryParameters: queryParameters));<br><br>      if (res.statusCode == 200) {<br>        return res.data;<br>      } else {<br>        return null;<br>      }<br>    } on DioError catch (e) {<br>      if (e.type == DioErrorType.response) {<br>        if (kDebugMode) {}<br>        return null;<br>      }<br>      if (e.type == DioErrorType.connectTimeout) {<br>        if (kDebugMode) {}<br>        return null;<br>      }<br><br>      if (e.type == DioErrorType.receiveTimeout) {<br>        if (kDebugMode) {<br>          print(&#39;unable to connect to the server&#39;);<br>        }<br>        return null;<br>      }<br><br>      if (e.type == DioErrorType.other) {<br>        if (kDebugMode) {}<br>        return null;<br>      }<br>      if (kDebugMode) {}<br>    }<br>  }<br><br>  Future post({<br>    Function(dynamic data)? onSuccess,<br>    Function(dynamic error)? onError,<br>  }) async {<br>    _dio().post(url, data: data).then((res) {<br>      if (onSuccess != null) {<br>        onSuccess(res.data);<br>      }<br>    }).catchError((error) {<br>      if (onError != null) onError(error);<br>    });<br>  }<br><br>  Future put({<br>    Function(dynamic data)? onSuccess,<br>    Function(dynamic error)? onError,<br>  }) async {<br>    _dio().put(url, data: data).then((res) {<br>      if (onSuccess != null) onSuccess(res.data);<br>    }).catchError((error) {<br>      if (onError != null) onError(error);<br>    });<br>  }<br><br>  Future delete({<br>    Function()? beforeSend,<br>    Function(dynamic data)? onSuccess,<br>    Function(dynamic error)? onError,<br>  }) async {<br>    _dio().delete(url).then((res) {<br>      if (onSuccess != null) onSuccess(res.data);<br>    }).catchError((error) {<br>      if (onError != null) onError(error);<br>    });<br>  }<br>}</pre><p>Sure, let’s organize your project structure as you described:</p><pre>lib<br>├── modules<br>   ├── signin<br>   │   ├── view<br>   │   ├── controller<br>   │   ├── binding<br>   │   └── data<br>   └── signup<br>       ├── view<br>       ├── controller<br>       ├── binding<br>       └── data<br></pre><p>Here’s a brief explanation of each folder:</p><ul><li><strong>view:</strong> This folder contains all the UI components related to the sign-in or sign-up screens.</li><li><strong>controller:</strong> This folder holds the GetX controllers responsible for handling the business logic and state management of the sign-in or sign-up screens.</li><li><strong>binding:</strong> This folder contains bindings used for dependency injection. You can bind your controllers or other dependencies here.</li><li><strong>data:</strong> This folder includes data-related classes such as models, repositories, or services used by the sign-in or sign-up screens.</li></ul><p>You can create these folders manually in your project directory. Make sure to organize your code files accordingly within each folder to maintain a clean and structured project.</p><h3>Sign In</h3><p><strong>Controller (signin/controller/signin_controller.dart):</strong></p><pre>import &#39;package:flutter/material.dart&#39;;<br>import &#39;package:flutter_auth_project/modules/signin/data/signin_data.dart&#39;;<br>import &#39;package:flutter_easyloading/flutter_easyloading.dart&#39;;<br>import &#39;package:get/get.dart&#39;;<br><br>class SignInController extends GetxController {<br>  /// GlobalKey<br>  final signInFormKey = GlobalKey&lt;FormState&gt;();<br><br>  /// TextEditingController<br>  final TextEditingController emailController = TextEditingController();<br>  final TextEditingController passwordController = TextEditingController();<br><br>  void signIn() {<br>    if (signInFormKey.currentState!.validate()) {<br>      EasyLoading.show(status: &#39;loading...&#39;);<br>      SignInServices.getInstance().login(<br>          data: {<br>            &quot;identifier&quot;: emailController.text,<br>            &quot;password&quot;: passwordController.text,<br>          },<br>          onSuccess: (data) async {<br>            clearForm();<br>            EasyLoading.dismiss();<br>          },<br>          onError: (e) {<br>            EasyLoading.dismiss();<br>            clearForm();<br>            Get.snackbar(&#39;Error&#39;, &#39;Please check your credentials!&#39;,<br>                snackStyle: SnackStyle.FLOATING,<br>                backgroundColor: Colors.red,<br>                colorText: Colors.white,<br>                icon: const Icon(<br>                  Icons.error,<br>                  color: Colors.white,<br>                ));<br>          });<br>    }<br>  }<br><br>  void clearForm() {<br>    // Clear text controllers<br>    emailController.clear();<br>    passwordController.clear();<br>    // Reset form state<br>    signInFormKey.currentState?.reset();<br>  }<br><br>  @override<br>  void onClose() {<br>    clearForm();<br>    super.onClose();<br>  }<br>}</pre><p><strong>View (signin/view/signin_view.dart):</strong></p><pre>import &#39;package:flutter/material.dart&#39;;<br>import &#39;package:get/get.dart&#39;;<br><br>import &#39;../controller/signin_controller.dart&#39;;<br><br>class SignInView extends StatelessWidget {<br>  final SignInController signInController = Get.put(SignInController());<br><br>  SignInView({super.key});<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Scaffold(<br>      appBar: AppBar(<br>        title: const Text(&#39;Sign In&#39;),<br>      ),<br>      body: Padding(<br>        padding: const EdgeInsets.all(16.0),<br>        child: Form(<br>          key: signInController.signInFormKey,<br>          child: Column(<br>            mainAxisAlignment: MainAxisAlignment.center,<br>            children: [<br>              TextFormField(<br>                controller: signInController.emailController,<br>                keyboardType: TextInputType.emailAddress,<br>                decoration: const InputDecoration(<br>                  labelText: &#39;Email&#39;,<br>                  hintText: &#39;Enter your email&#39;,<br>                ),<br>                validator: (value) {<br>                  if (value == null || value.isEmpty) {<br>                    return &#39;Please enter your email&#39;;<br>                  }<br>                  // Add more validation if needed<br>                  return null;<br>                },<br>              ),<br>              const SizedBox(height: 20),<br>              TextFormField(<br>                controller: signInController.passwordController,<br>                obscureText: true,<br>                decoration: const InputDecoration(<br>                  labelText: &#39;Password&#39;,<br>                  hintText: &#39;Enter your password&#39;,<br>                ),<br>                validator: (value) {<br>                  if (value == null || value.isEmpty) {<br>                    return &#39;Please enter your password&#39;;<br>                  }<br>                  // Add more validation if needed<br>                  return null;<br>                },<br>              ),<br>              const SizedBox(height: 20),<br>              ElevatedButton(<br>                onPressed: () {<br>                  signInController.signIn();<br>                },<br>                child: const Text(&#39;Sign In&#39;),<br>              ),<br>            ],<br>          ),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><p><strong>View (signin/data/signin_data.dart):</strong></p><pre>class SignInServices {<br>  static SignInServices? _instance;<br><br><br>  static SignInServices getInstance() {<br>    _instance ??= SignInServices();<br>    return _instance!;<br>  }<br><br><br>  Future signIn({<br>    required Function(dynamic data) onSuccess,<br>    required Function(dynamic error) onError,<br>    required Map&lt;String, dynamic&gt; data,<br>  }) async {<br>    try {<br>      var url = &quot;auth/local/&quot;;<br>      var response = await ApiRequest(url: url, data: json.encode(data)).post(<br>        onSuccess: (data) {<br>          onSuccess(data);<br>        },<br>        onError: (e) {<br>          onError(e);<br>        },<br>      );<br>      return response;<br>    } catch (e) {<br>      onError(e);<br>    }<br>  }<br><br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1_XR207jCNalImx14GqhVA.jpeg" /></figure><h3>Sign Up</h3><p><strong>Controller (signup/controller/signup_controller.dart):</strong></p><pre>class SignUpController extends GetxController {<br>  final signUpFormKey = GlobalKey&lt;FormState&gt;();<br>  final TextEditingController emailController = TextEditingController();<br>  final TextEditingController passwordController = TextEditingController();<br>  final TextEditingController usernameController = TextEditingController();<br><br>  void signUp() async {<br>    EasyLoading.show(status: &#39;loading...&#39;);<br>    try {<br>      await SignUpServices.getInstance().signUp(<br>        data: {<br>          &quot;email&quot;: emailController.text,<br>          &quot;password&quot;: passwordController.text,<br>          &quot;username&quot;: usernameController.text,<br>          &quot;confirmed&quot;: false,<br>          &quot;blocked&quot;: false,<br>        },<br>        onSuccess: (data) {<br>          EasyLoading.dismiss();<br>          Get.snackbar(<br>            &#39;Success&#39;,<br>            &#39;Sign up successful!&#39;,<br>            snackStyle: SnackStyle.FLOATING,<br>            backgroundColor: Colors.green,<br>            colorText: Colors.white,<br>            icon: const Icon(<br>              Icons.check_circle,<br>              color: Colors.white,<br>            ),<br>          );<br>        },<br>        onError: (error) {<br>          EasyLoading.dismiss();<br>        },<br>      );<br>    } catch (e) {<br>      EasyLoading.dismiss();<br>      Get.snackbar(<br>        &#39;Error&#39;,<br>        &#39;Failed to sign up. Please try again later.&#39;,<br>        snackStyle: SnackStyle.FLOATING,<br>        backgroundColor: Colors.red,<br>        colorText: Colors.white,<br>        icon: const Icon(<br>          Icons.error,<br>          color: Colors.white,<br>        ),<br>      );<br>    }<br>  }<br><br>  @override<br>  void dispose() {<br>    // Dispose controllers<br>    emailController.dispose();<br>    passwordController.dispose();<br>    usernameController.dispose();<br>    super.dispose();<br>  }<br>}</pre><p><strong>View (signup/view/signup_view.dart):</strong></p><pre>class SignUpView extends StatelessWidget {<br>  final SignUpController signUpController = Get.put(SignUpController());<br>  @override<br>  Widget build(BuildContext context) {<br>    return Scaffold(<br>      appBar: AppBar(<br>        title: const Text(&#39;Sign Up&#39;),<br>      ),<br>      body: Padding(<br>        padding: const EdgeInsets.all(16.0),<br>        child: Column(<br>          mainAxisAlignment: MainAxisAlignment.center,<br>          children: [<br>            TextFormField(<br>              controller: signUpController.emailController,<br>              keyboardType: TextInputType.emailAddress,<br>              decoration: const InputDecoration(<br>                labelText: &#39;Email&#39;,<br>                hintText: &#39;Enter your email&#39;,<br>              ),<br>              validator: (value) {<br>                if (value == null || value.isEmpty) {<br>                  return &#39;Please enter your email&#39;;<br>                }<br>                // Add email validation if needed<br>                return null;<br>              },<br>            ),<br>            const SizedBox(height: 20),<br>            TextFormField(<br>              controller: signUpController.passwordController,<br>              obscureText: true,<br>              decoration: const InputDecoration(<br>                labelText: &#39;Password&#39;,<br>                hintText: &#39;Enter your password&#39;,<br>              ),<br>              validator: (value) {<br>                if (value == null || value.isEmpty) {<br>                  return &#39;Please enter your password&#39;;<br>                }<br>                // Add password validation if needed<br>                return null;<br>              },<br>            ),<br>            const SizedBox(height: 20),<br>            TextFormField(<br>              controller: signUpController.usernameController,<br>              decoration: const InputDecoration(<br>                labelText: &#39;Username&#39;,<br>                hintText: &#39;Enter your username&#39;,<br>              ),<br>              validator: (value) {<br>                if (value == null || value.isEmpty) {<br>                  return &#39;Please enter your username&#39;;<br>                }<br>                // Add username validation if needed<br>                return null;<br>              },<br>            ),<br>            const SizedBox(height: 20),<br>            ElevatedButton(<br>              onPressed: () {<br>                  signUpController.signUp();<br><br>              },<br>              child: const Text(&#39;Sign Up&#39;),<br>            ),<br>          ],<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dT45I2axfj_0WJ2RzdOlxg.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17564a5f3ed0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>