Guide on transitioning a token authenticated user from an API to session-based authentication within a Flutter app through a Flutter web view

My goal is to achieve the following: imagine I have an authenticated user with an API token. When this user clicks a button, I want to open a web view and authenticate them using session-based authentication. https://i.stack.imgur.com/CsZ57.png

This is what I have tried so far, including various solutions and suggestions:

  1. Using url_launcher:

    • Open a browser on click using launch(url)
    • Send a token as GET parameters to the backend server
    • Authenticate the user and redirect back. This works well but has poor UX as the user needs to switch between the browser and the app.
  2. Using either 1) flutter_webview_plugin or 2) webview_flutter:

    • Option 1:

      class __WebWidget extends StatelessWidget {
       @override
       Widget build(BuildContext context) {
         return WebviewScaffold(
           url: url + "?token=${userManager.token}",
           appBar: new AppBar(
             title: Text('Space'),
           ),
           withZoom: true,
           withLocalStorage: true,
           hidden: true,
           initialChild: Container(
             color: Colors.blueAccent,
             child: const Center(
               child: Text('Loading....'),
             ),
           ),
         );
       }
      }
      

      Option 2:

      class WebWidget extends StatelessWidget {
        WebWidget({this.url});
      
        final String url;
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text('Votre Espace '),
            ),
            body: WebView(
              initialUrl: url + "?token=${userManager.token}",
              javascriptMode: JavascriptMode.unrestricted,
            ),
          );
        }
      }
      
    • Send a token as GET params to the backend server

    • Authenticate the user and redirect back. Unfortunately, this method is not working!

I suspect that the webview does not support cookies!

In my backend setup, I am using Laravel (PHP7) and Apache server.

Thank you in advance.

Answer №1

If you're developing with flutter, I suggest utilizing the Flutter Web Auth plugin to authenticate users with a web service. This plugin supports various web flows that can redirect to a custom scheme.

The plugin will automatically redirect users back to your app with authenticated tokens. For Android apps, make sure to update the AndroidManifest.xml following the instructions provided in the README.md file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

SQL Error 1064, 42000: Your SQL syntax is throwing an error, and the issue is proving tricky to pinpoint

RESOLVED: The function was missing one argument being passed. I encountered an issue while attempting to update a database record using PDO in PHP. The query execution resulted in error code 1604. [42000,1064,"You have an error in your SQL syntax; check ...

Issue encountered while running foreach loop on Array:

I need some assistance to correct the mistakes in this code. I was attempting to identify the available flavors of ice cream. <?php $flavors = array(); $flavors[]=array("name" => "CD" , "in_stock" => true); $flavors[]=array("name" => "V" , ...

The issue with CKEDITOR is that it fails to send data through ajax upon the initial submission

When using CKEDITOR, I am experiencing an issue where my forms do not send data to the server on the first submit. If I click the submit button once, empty fields are sent without any input from me. However, when I submit the form a second time, only then ...

What is the optimal method for saving and organizing data in SQL?

I currently have a MySQL table containing data that is displayed in an HTML table. Using JavaScript and drag & drop functionality, I am able to locally sort this table. My question is, what is the most effective method for saving these sorting changes? W ...

How to retrieve the complete file path of an image during the image upload process

I am having trouble uploading an image as it only takes the file name with extension, not the full path. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">< ...

The ongoing calculation consistently yields a result of zero

This content belongs to the index.php file if(isset($_POST['submit'])) { echo header('Location:answer.php'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> ...

Looking up information using PHP and MySQL

I've been working on a website project for my class, focusing on creating a search functionality that interacts with a MySQL database table to retrieve matching results. Despite my best efforts, the code I have written isn't functioning properly ...

Achieving a URL structure similar to a blog post (such as www.abc.com/how-to-learn-php) in Laravel 5.5 - Step by

I am looking to create a personalized profile URL for users resembling a blog post format (www.abc.com/profile/tom-cruise-brad-pit). Every user in the database has their own unique identification, but I prefer not to display this ID in the URL. ...

The array is failing to pass through ajax requests

Here is the JavaScript code snippet for making an AJAX call. In this scenario, the code variable is used to store a C program code and then pass it on to the compiler.php page. function insert(){ var code = document.getElementById("file_cont").val ...

One simple click to auto-fill the form

I have encountered a problem that has been discussed before, but my lack of coding knowledge is making it difficult for me to find a suitable solution that matches the code on my website. The issue at hand is as follows: I need my form to populate car mak ...

how to properly utilize the makeHidden method in Laravel

Having an issue with pagination. Everything is working fine without any errors, but the problem arises when I use makeHidden with my code. It alters the structure of my JSON pagination result. This is the code snippet causing the issue: $result = Job::w ...

Using PDO's FETCH_CLASS option allows for passing results directly to the constructor as parameters

Is it possible to pass the results of PDO as parameters of the constructor in a specific way? Let's consider the example of the following class: class Test { private $value1; private $value2; function __construct($val1, $val2) { ...

What is the best way to set up a session using jQuery?

I've been troubleshooting my code and I can't seem to figure out why the jquery.session.js file isn't working. Can someone help me find a solution? $.session.set('rmng_time', remaining_seconds); alert("j session "+$.sessi ...

The power trio: Form, AJAX, and PHP

I am encountering an issue with my Subscribe form where the email address is not getting inserted into the MySQL db despite successfully creating a date and time record using AJAX. Can anyone identify what might be causing this problem? Here is the code i ...

What is the best way to pass a variable in PHP and create an HTML link to a specific element ID simultaneously?

Formulating the question may be challenging, but the concept is quite straightforward: I'd like to pass the variable 'count' using GET and simultaneously direct to the media section. On the page http://page.org.mx/comunicados#media I&apo ...

Transferring data from Unity to PHP: Passing a variable's value

My PHP file is used for displaying a PDF file that contains variables retrieved from the following PHP file. $areaOfCircle; ... ... $pdf->WriteFixedPosHTML($areaOfCircle, 20, 50, 20, 50, 'auto'); In addition, I have included my Unity script b ...

Is the response of the $.ajax function null?

I am facing an issue with my JavaScript code. Here is the snippet: $.ajax({ type: 'GET', url: 'http://133.333.33.33/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+& ...

Establishing fixed values in an angular2 component

Is it possible in angular 2 to initialize values within the HTML view of a component that has already been rendered in PHP? //html page: <dropdown [(options)]="['Available', 'Busy', 'Away', 'Offline']"></dr ...

Bitwise Permission Calculator for Discord

I need my tool/script to check if a logged-in user has the necessary permission to use it. However, Discord is returning a permission integer. It seems like I have to use a bitwise operator to check permissions, but all the information I've found so ...

Merging Two Form Variables in PHP

I am encountering an issue with combining two variables into a single variable. Each variable comes with two sets of conditions and I can't seem to get the syntax right. I attempted to use || as OR but it resulted in a syntax error. $JobType = ($Repa ...