diff --git a/AGENTS.md b/AGENTS.md index 9f8e43278..1c6a3aaec 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -196,7 +196,7 @@ For integration testing, you may want to point to a local Supabase instance: ```dart await Supabase.initialize( url: 'http://localhost:54321', - anonKey: 'your-local-anon-key', + publishableKey: 'your-local-supabase-key', ); ``` diff --git a/packages/functions_client/lib/src/functions_client.dart b/packages/functions_client/lib/src/functions_client.dart index 3f54b79bb..a5f1e6c2f 100644 --- a/packages/functions_client/lib/src/functions_client.dart +++ b/packages/functions_client/lib/src/functions_client.dart @@ -83,7 +83,7 @@ class FunctionsClient { /// final fetchClient = FetchClient(mode: RequestMode.cors); /// await Supabase.initialize( /// url: supabaseUrl, - /// anonKey: supabaseKey, + /// publishableKey: supabaseKey, /// httpClient: fetchClient, /// ); /// ``` diff --git a/packages/gotrue/lib/src/gotrue_admin_api.dart b/packages/gotrue/lib/src/gotrue_admin_api.dart index 42b5a79e5..5e25ec8bf 100644 --- a/packages/gotrue/lib/src/gotrue_admin_api.dart +++ b/packages/gotrue/lib/src/gotrue_admin_api.dart @@ -59,7 +59,7 @@ class GoTrueAdminApi { /// Creates a new user. /// - /// This function should only be called on a server. Never expose your `service_role` key on the client. + /// This function should only be called on a server. Never expose your `secret` key on the client. /// /// Requires either an email or phone Future createUser(AdminUserAttributes attributes) async { @@ -75,11 +75,11 @@ class GoTrueAdminApi { return UserResponse.fromJson(response); } - /// Delete a user. Requires a `service_role` key. + /// Delete a user. Requires a `secret` key. /// /// [id] is the user id of the user you want to remove. /// - /// This function should only be called on a server. Never expose your `service_role` key on the client. + /// This function should only be called on a server. Never expose your `secret` key on the client. Future deleteUser(String id) async { validateUuid(id); final options = GotrueRequestOptions(headers: _headers); @@ -92,7 +92,7 @@ class GoTrueAdminApi { /// Get a list of users. /// - /// This function should only be called on a server. Never expose your `service_role` key on the client. + /// This function should only be called on a server. Never expose your `secret` key on the client. /// /// The result is paginated. Use the [page] and [perPage] parameters to paginate the result. Future> listUsers({int? page, int? perPage}) async { diff --git a/packages/gotrue/lib/src/gotrue_admin_oauth_api.dart b/packages/gotrue/lib/src/gotrue_admin_oauth_api.dart index afb07891a..e9a27a2ce 100644 --- a/packages/gotrue/lib/src/gotrue_admin_oauth_api.dart +++ b/packages/gotrue/lib/src/gotrue_admin_oauth_api.dart @@ -65,7 +65,7 @@ class GoTrueAdminOAuthApi { /// Lists all OAuth clients with optional pagination. /// Only relevant when the OAuth 2.1 server is enabled in Supabase Auth. /// - /// This function should only be called on a server. Never expose your `service_role` key in the browser. + /// This function should only be called on a server. Never expose your `secret` key in the browser. Future listClients({ int? page, int? perPage, @@ -88,7 +88,7 @@ class GoTrueAdminOAuthApi { /// Creates a new OAuth client. /// Only relevant when the OAuth 2.1 server is enabled in Supabase Auth. /// - /// This function should only be called on a server. Never expose your `service_role` key in the browser. + /// This function should only be called on a server. Never expose your `secret` key in the browser. Future createClient( CreateOAuthClientParams params, ) async { @@ -107,7 +107,7 @@ class GoTrueAdminOAuthApi { /// Gets details of a specific OAuth client. /// Only relevant when the OAuth 2.1 server is enabled in Supabase Auth. /// - /// This function should only be called on a server. Never expose your `service_role` key in the browser. + /// This function should only be called on a server. Never expose your `secret` key in the browser. Future getClient(String clientId) async { validateUuid(clientId); @@ -125,7 +125,7 @@ class GoTrueAdminOAuthApi { /// Updates an existing OAuth client. /// Only relevant when the OAuth 2.1 server is enabled in Supabase Auth. /// - /// This function should only be called on a server. Never expose your `service_role` key in the browser. + /// This function should only be called on a server. Never expose your `secret` key in the browser. Future updateClient( String clientId, UpdateOAuthClientParams params, @@ -147,7 +147,7 @@ class GoTrueAdminOAuthApi { /// Deletes an OAuth client. /// Only relevant when the OAuth 2.1 server is enabled in Supabase Auth. /// - /// This function should only be called on a server. Never expose your `service_role` key in the browser. + /// This function should only be called on a server. Never expose your `secret` key in the browser. Future deleteClient(String clientId) async { validateUuid(clientId); @@ -165,7 +165,7 @@ class GoTrueAdminOAuthApi { /// Regenerates the secret for an OAuth client. /// Only relevant when the OAuth 2.1 server is enabled in Supabase Auth. /// - /// This function should only be called on a server. Never expose your `service_role` key in the browser. + /// This function should only be called on a server. Never expose your `secret` key in the browser. Future regenerateClientSecret(String clientId) async { validateUuid(clientId); diff --git a/packages/supabase/example/web/main.dart b/packages/supabase/example/web/main.dart index c1f2d9e71..fd4edc2e1 100644 --- a/packages/supabase/example/web/main.dart +++ b/packages/supabase/example/web/main.dart @@ -5,7 +5,7 @@ import 'package:web/web.dart' as web; void main() { const supabaseUrl = 'YOUR_SUPABASE_URL'; - const supabaseKey = 'YOUR_ANON_KEY'; + const supabaseKey = 'YOUR_SUPABASE_KEY'; final supabase = SupabaseClient(supabaseUrl, supabaseKey); final element = web.document.querySelector('#output') as web.HTMLDivElement; @@ -16,10 +16,8 @@ void main() { void exampleUsage(SupabaseClient supabase) async { // query data - final data = await supabase - .from('countries') - .select() - .order('name', ascending: true); + final data = + await supabase.from('countries').select().order('name', ascending: true); print(data); // insert data @@ -76,15 +74,13 @@ void exampleUsage(SupabaseClient supabase) async { print('upload response : $storageResponse'); // Get download url - final urlResponse = await supabase.storage - .from('public') - .createSignedUrl('example.txt', 60); + final urlResponse = + await supabase.storage.from('public').createSignedUrl('example.txt', 60); print('download url : $urlResponse'); // Download text file - final fileResponse = await supabase.storage - .from('public') - .download('example.txt'); + final fileResponse = + await supabase.storage.from('public').download('example.txt'); print('downloaded file : ${String.fromCharCodes(fileResponse)}'); // Delete file diff --git a/packages/supabase/lib/src/supabase_client.dart b/packages/supabase/lib/src/supabase_client.dart index 8a30b1206..5796c940a 100644 --- a/packages/supabase/lib/src/supabase_client.dart +++ b/packages/supabase/lib/src/supabase_client.dart @@ -14,6 +14,8 @@ import 'counter.dart'; /// Creates a Supabase client to interact with your Supabase instance. /// /// [supabaseUrl] and [supabaseKey] can be found on your Supabase dashboard. +/// Pass the `publishable` (anon) key for client-side usage or the `secret` +/// key for trusted server-side environments. /// /// You can access none public schema by passing different [schema]. /// diff --git a/packages/supabase_flutter/README.md b/packages/supabase_flutter/README.md index 047545a5b..cc948dabd 100644 --- a/packages/supabase_flutter/README.md +++ b/packages/supabase_flutter/README.md @@ -27,7 +27,7 @@ void main() async { await Supabase.initialize( url: SUPABASE_URL, - anonKey: SUPABASE_ANON_KEY, + publishableKey: SUPABASE_KEY, ); runApp(MyApp()); diff --git a/packages/supabase_flutter/example/lib/main.dart b/packages/supabase_flutter/example/lib/main.dart index 6cd466188..3ff369fe8 100644 --- a/packages/supabase_flutter/example/lib/main.dart +++ b/packages/supabase_flutter/example/lib/main.dart @@ -2,7 +2,8 @@ import 'package:flutter/material.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; Future main() async { - await Supabase.initialize(url: 'SUPABASE_URL', anonKey: 'SUPABASE_ANON_KEY'); + await Supabase.initialize( + url: 'SUPABASE_URL', publishableKey: 'SUPABASE_PUBLISHABLE_KEY'); runApp(const MyApp()); } diff --git a/packages/supabase_flutter/lib/src/supabase.dart b/packages/supabase_flutter/lib/src/supabase.dart index 41d1dc588..c2ce4e3c3 100644 --- a/packages/supabase_flutter/lib/src/supabase.dart +++ b/packages/supabase_flutter/lib/src/supabase.dart @@ -52,7 +52,9 @@ class Supabase { /// This must be called only once. If called more than once, an /// [AssertionError] is thrown /// - /// [url] and [anonKey] can be found on your Supabase dashboard. + /// [url] and [publishableKey] can be found on your Supabase dashboard. + /// Use the `publishable` (anon) key here — never the secret key in a + /// Flutter app. /// /// You can access none public schema by passing different [schema]. /// @@ -76,7 +78,11 @@ class Supabase { /// If [debug] is set to `true`, debug logs will be printed in debug console. Default is `kDebugMode`. static Future initialize({ required String url, - required String anonKey, + String? publishableKey, + @Deprecated( + 'Use publishableKey instead. anonKey will be removed in a future major version.', + ) + String? anonKey, Map? headers, Client? httpClient, RealtimeClientOptions realtimeClientOptions = const RealtimeClientOptions(), @@ -86,6 +92,12 @@ class Supabase { Future Function()? accessToken, bool? debug, }) async { + assert( + publishableKey != null || anonKey != null, + 'Either publishableKey or anonKey must be provided.', + ); + final effectiveKey = publishableKey ?? anonKey!; + if (_instance._isInitialized) { _log.info('Supabase is already initialized. Skipping reinitialization.'); return _instance; @@ -120,7 +132,7 @@ class Supabase { } _instance._init( url, - anonKey, + effectiveKey, httpClient: httpClient, customHeaders: headers, realtimeClientOptions: realtimeClientOptions, @@ -193,7 +205,7 @@ class Supabase { void _init( String supabaseUrl, - String supabaseAnonKey, { + String supabaseKey, { Client? httpClient, Map? customHeaders, required RealtimeClientOptions realtimeClientOptions, @@ -208,7 +220,7 @@ class Supabase { }; client = SupabaseClient( supabaseUrl, - supabaseAnonKey, + supabaseKey, httpClient: httpClient, headers: headers, realtimeClientOptions: realtimeClientOptions, diff --git a/packages/supabase_flutter/test/auth_test.dart b/packages/supabase_flutter/test/auth_test.dart index dd53fb313..8fd422900 100644 --- a/packages/supabase_flutter/test/auth_test.dart +++ b/packages/supabase_flutter/test/auth_test.dart @@ -46,7 +46,7 @@ void main() { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: mockStorage, @@ -67,7 +67,7 @@ void main() { () async { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockEmptyLocalStorage(), @@ -94,7 +94,7 @@ void main() { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: corruptedStorage, @@ -112,7 +112,7 @@ void main() { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: emptyStorage, diff --git a/packages/supabase_flutter/test/deep_link_test.dart b/packages/supabase_flutter/test/deep_link_test.dart index 0db027ac7..666b55d38 100644 --- a/packages/supabase_flutter/test/deep_link_test.dart +++ b/packages/supabase_flutter/test/deep_link_test.dart @@ -22,7 +22,7 @@ void main() { ); await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, httpClient: pkceHttpClient, authOptions: FlutterAuthClientOptions( diff --git a/packages/supabase_flutter/test/initialization_test.dart b/packages/supabase_flutter/test/initialization_test.dart index 7fceeb8cb..1cf9b14fb 100644 --- a/packages/supabase_flutter/test/initialization_test.dart +++ b/packages/supabase_flutter/test/initialization_test.dart @@ -28,7 +28,7 @@ void main() { test('initialize successfully with default options', () async { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, ); expect(Supabase.instance, isNotNull); @@ -41,7 +41,7 @@ void main() { final localStorage = MockLocalStorage(); await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, authOptions: FlutterAuthClientOptions( localStorage: localStorage, ), @@ -54,7 +54,7 @@ void main() { test('handles initialization with expired session in storage', () async { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: true, authOptions: FlutterAuthClientOptions( localStorage: MockExpiredStorage(), @@ -71,7 +71,7 @@ void main() { test('initialize successfully with PKCE auth flow', () async { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, authOptions: const FlutterAuthClientOptions( authFlowType: AuthFlowType.pkce, ), @@ -87,7 +87,7 @@ void main() { final httpClient = PkceHttpClient(); await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, httpClient: httpClient, ); @@ -98,7 +98,7 @@ void main() { test('initialize successfully with custom access token', () async { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, accessToken: () async => 'custom-access-token', ); @@ -118,7 +118,7 @@ void main() { // First initialization await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, ); expect(Supabase.instance, isNotNull); @@ -132,7 +132,7 @@ void main() { // Re-initialize should work without errors await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, ); expect(Supabase.instance, isNotNull); @@ -141,7 +141,7 @@ void main() { test('handles multiple initializations correctly', () async { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockLocalStorage(), @@ -157,7 +157,7 @@ void main() { await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: true, authOptions: FlutterAuthClientOptions( localStorage: MockEmptyLocalStorage(), diff --git a/packages/supabase_flutter/test/lifecycle_test.dart b/packages/supabase_flutter/test/lifecycle_test.dart index edf89fb28..d94cb3c44 100644 --- a/packages/supabase_flutter/test/lifecycle_test.dart +++ b/packages/supabase_flutter/test/lifecycle_test.dart @@ -81,7 +81,7 @@ void main() { readyCompleters = []; await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockEmptyLocalStorage(), diff --git a/packages/supabase_flutter/test/supabase_flutter_test.dart b/packages/supabase_flutter/test/supabase_flutter_test.dart index d487032e6..74ae10df8 100644 --- a/packages/supabase_flutter/test/supabase_flutter_test.dart +++ b/packages/supabase_flutter/test/supabase_flutter_test.dart @@ -14,7 +14,7 @@ void main() { // Initialize the Supabase singleton await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockLocalStorage(), @@ -34,7 +34,7 @@ void main() { await Supabase.instance.dispose(); await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockLocalStorage(), @@ -50,7 +50,7 @@ void main() { test('with custom access token', () async { final supabase = await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseUrl, + publishableKey: supabaseUrl, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockLocalStorage(), @@ -73,7 +73,7 @@ void main() { mockAppLink(); await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockExpiredStorage(), @@ -97,7 +97,7 @@ void main() { mockAppLink(); await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: MockEmptyLocalStorage(), @@ -123,7 +123,7 @@ void main() { // Initialize the Supabase singleton await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, debug: false, authOptions: FlutterAuthClientOptions( localStorage: localStorage, diff --git a/packages/supabase_flutter/test/widget_test.dart b/packages/supabase_flutter/test/widget_test.dart index 098efbded..5160d7ff3 100644 --- a/packages/supabase_flutter/test/widget_test.dart +++ b/packages/supabase_flutter/test/widget_test.dart @@ -17,7 +17,7 @@ void main() { // Initialize the Supabase singleton await Supabase.initialize( url: supabaseUrl, - anonKey: supabaseKey, + publishableKey: supabaseKey, authOptions: FlutterAuthClientOptions( localStorage: MockLocalStorage(), pkceAsyncStorage: MockAsyncStorage(),