<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shehabin Sinad S</title>
    <description>The latest articles on DEV Community by Shehabin Sinad S (@shehabin_sinad).</description>
    <link>https://dev.to/shehabin_sinad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3990090%2Fdf821b33-fc7d-4062-944a-9a6fd6cbfea7.jpg</url>
      <title>DEV Community: Shehabin Sinad S</title>
      <link>https://dev.to/shehabin_sinad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shehabin_sinad"/>
    <language>en</language>
    <item>
      <title>The biometric authentication loop that took me a while to figure out</title>
      <dc:creator>Shehabin Sinad S</dc:creator>
      <pubDate>Thu, 18 Jun 2026 10:40:35 +0000</pubDate>
      <link>https://dev.to/shehabin_sinad/the-biometric-authentication-loop-that-took-me-a-while-to-figure-out-3egk</link>
      <guid>https://dev.to/shehabin_sinad/the-biometric-authentication-loop-that-took-me-a-while-to-figure-out-3egk</guid>
      <description>&lt;h3&gt;
  
  
  &lt;em&gt;A Flutter edge case in ChainCare — a blockchain-based medical records system&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;When we added biometric authentication to ChainCare, the basic flow worked fine. User opens the app, fingerprint prompt appears, they authenticate, they're in. Simple enough.&lt;/p&gt;

&lt;p&gt;The requirement we had was a bit more than that though. We wanted the app to lock whenever the user leaves it — like a banking app would — and require biometric re-authentication when they come back. That's when things got weird.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu39q94jgryoicxjbgzy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu39q94jgryoicxjbgzy6.png" alt=" " width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What was happening
&lt;/h2&gt;

&lt;p&gt;The loop looked like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User is in the app&lt;/li&gt;
&lt;li&gt;Fingerprint prompt appears&lt;/li&gt;
&lt;li&gt;User authenticates successfully&lt;/li&gt;
&lt;li&gt;Prompt dismisses&lt;/li&gt;
&lt;li&gt;Another fingerprint prompt immediately appears again&lt;/li&gt;
&lt;li&gt;Repeat forever&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The authentication was succeeding — it just kept re-triggering itself. One successful fingerprint scan produced another prompt, indefinitely.&lt;/p&gt;

&lt;h2&gt;
  
  
  The naive implementation that caused it
&lt;/h2&gt;

&lt;p&gt;The code was straightforward — listen to the app lifecycle, and when the app resumes, authenticate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="nd"&gt;@override&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;didChangeAppLifecycleState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AppLifecycleState&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;AppLifecycleState&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;resumed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_authenticate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// this causes the loop&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks reasonable. The problem is in what &lt;code&gt;AppLifecycleState.paused&lt;/code&gt; actually means.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I got wrong about AppLifecycleState
&lt;/h2&gt;

&lt;p&gt;I assumed &lt;code&gt;paused&lt;/code&gt; meant "the user pressed the home button and left the app." That's not what it means.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;paused&lt;/code&gt; fires any time another native layer takes full focus over the Flutter surface. Including the system biometric prompt.&lt;/p&gt;

&lt;p&gt;So here's what was actually happening:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fingerprint prompt appears → OS takes focus → Flutter fires &lt;code&gt;paused&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;User authenticates → prompt dismisses → Flutter fires &lt;code&gt;resumed&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;resumed&lt;/code&gt; handler fires &lt;code&gt;_authenticate()&lt;/code&gt; again&lt;/li&gt;
&lt;li&gt;New fingerprint prompt appears → loop restarts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From Flutter's perspective, there's no difference between the user leaving the app for 10 minutes and the biometric prompt appearing for 2 seconds. Both produce the same &lt;code&gt;paused → resumed&lt;/code&gt; sequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;The fix came from a simple observation: a biometric prompt takes 1–5 seconds. A genuine background session — user switches away, checks something, comes back — is almost always longer than that.&lt;/p&gt;

&lt;p&gt;So instead of re-locking on every resume, we only re-lock if the app was backgrounded for longer than 60 seconds. Anything shorter is treated as a transient OS pause — like the biometric prompt — and ignored.&lt;/p&gt;

&lt;p&gt;The implementation records a timestamp when the app pauses, and checks elapsed time on resume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;_pausedAt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@override&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;didChangeAppLifecycleState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AppLifecycleState&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AppLifecycleState&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;paused&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;_pausedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AppLifecycleState&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;resumed&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;_handleResume&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_handleResume&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_pausedAt&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_pausedAt&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;inSeconds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// user genuinely left — re-lock&lt;/span&gt;
      &lt;span class="n"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_showLockScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_authenticate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// under 60s — probably the biometric prompt, do nothing&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;_pausedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the core of it. Once we added the threshold check, the loop stopped completely. Successful authentication stays successful.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I actually learned from this
&lt;/h2&gt;

&lt;p&gt;The main thing: &lt;code&gt;AppLifecycleState.paused&lt;/code&gt; doesn't mean "user left the app." It means "another native layer has focus." If you're building anything that hooks into the lifecycle — lock screens, background timers, session expiry — that distinction matters a lot.&lt;/p&gt;

&lt;p&gt;The second thing: measuring elapsed time is a lot more reliable than trying to track intent with flags. Setting a &lt;code&gt;_isShowingBiometricPrompt&lt;/code&gt; flag before calling local_auth sounds reasonable, but async gaps and exceptions can corrupt it. A timestamp is just a fact — it doesn't depend on your code running in a specific order.&lt;/p&gt;

&lt;p&gt;This came up during ChainCare, our final-year project — a blockchain-based medical records system. My part was the authentication and session management module. Hitting this bug and finding the fix was probably the most interesting technical problem I worked through during the whole project.&lt;/p&gt;

&lt;p&gt;If you've hit this loop or something similar, the full ChainCare codebase is on &lt;a href="https://github.com/shehabinsinad/ChainCare" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>mobile</category>
      <category>software</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
