This article demonstrates how users can reset their password within the app using Supabase authentication, even without sending a reset password link via email. The solution targets users who are already logged in and wish to change their password for security or other reasons.
Supabase is an open-source Backend-as-a-Service (BaaS) platform that offers a scalable backend for building web and mobile applications. It provides developers with tools to set up a complete backend without manually configuring databases or authentication systems. Supabase is often described as an alternative to Google Firebase, but with a stronger focus on open-source technologies and relational databases like PostgreSQL.
Follow the steps below to set up your Supabase project:
Project URL
and API Key
(usually the anon
public key). You'll need these to configure the client-side.FlutterFlow is a low-code development platform built on top of Flutter, Google's open-source UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase.
Follow the steps below to set up your FlutterFlow project:
// Automatic FlutterFlow imports
import '/backend/supabase/supabase.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
Future<bool> resetPassword(
BuildContext context,
String email,
String apiUrl,
String serviceRoleKey,
String newPassword,
String oldPassword,
) async {
// Add your function code here!
final supabase = SupabaseClient(apiUrl, serviceRoleKey);
try {
// Step 1: Authenticate user with old password
final response = await supabase.auth.signInWithPassword(
email: email,
password: oldPassword,
);
// Step 2: Update password if authentication is successful
await supabase.auth.updateUser(
UserAttributes(password: newPassword),
);
_showAlert(context, 'Success', 'Password updated successfully');
return true;
} on AuthException catch (e) {
_showAlert(context, 'Error',
e.statusCode == '400' ? 'Invalid old password' : e.message);
return false;
}
}
void _showAlert(BuildContext context, String title, String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
TextButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
),
],
);
},
);
}
signInWithPassword
method.updateUser
method.This method ensures that the password reset occurs directly within the app, providing a smooth and secure experience without requiring external email communication.
By leveraging Supabase authentication and FlutterFlow, this guide provides a seamless and secure way for users to reset their passwords directly within the app, without relying on email-based reset links. The outlined approach ensures that users can authenticate with their existing password before updating it, maintaining security and preventing unauthorized changes.
With a structured setup, clear UI flow, and robust error handling, this method enhances user experience while simplifying password management in your application. Implementing this approach will help maintain a secure and efficient authentication system tailored to your app's needs.