prefabs_user 0.1.0 copy "prefabs_user: ^0.1.0" to clipboard
prefabs_user: ^0.1.0 copied to clipboard

Authentication helper library for flutter application

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:prefabs_user/user.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  PrefabsTechUser.initialize(
    apiBaseUrl: '<your-api-base-url>', // Use your API base URL here
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: const HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String authStatus = 'Not authenticated';

  final SuperTokensAuthProvider superTokensAuth = SuperTokensAuthProvider(
    baseURL: '<your-api-base-url>', // Use your API base URL here
    appOriginURL: '<your-app-url>', // Use your app origin URL here
  );

  Future<BaseLoginSignUpResponse> login(String email, String password) async {
    final response = await superTokensAuth.loginWithCredentials(
      email: email,
      password: password,
    );

    if (response.user != null) {
      setState(() {
        authStatus = 'Authenticated';
      });
    }

    return response;
  }

  Future<BaseLoginSignUpResponse> signUp({
    required String email,
    required String password,
  }) async {
    final response = await superTokensAuth.signUpWithCredentials(
      email: email,
      password: password,
    );

    if (response.user != null) {
      setState(() {
        authStatus = 'Authenticated';
      });
    }

    return response;
  }

  Future<void> logout() async {
    try {
      await superTokensAuth.logout();

      setState(() {
        authStatus = 'Not authenticated';
      });
    } catch (e) {
      print('Logout failed: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Auth example')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            ElevatedButton(
              onPressed: () => login('test1@example.com', '123456'),
              child: const Text('Login'),
            ),
            ElevatedButton(
              onPressed:
                  () => signUp(email: 'test2@example.com', password: '123456'),
              child: const Text('Sign Up'),
            ),
            ElevatedButton(onPressed: logout, child: const Text('Logout')),
            const SizedBox(height: 24),
            Text(
              authStatus,
              style: const TextStyle(fontSize: 16),
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
40
points
184
downloads

Publisher

verified publisherprefabs.tech

Weekly Downloads

Authentication helper library for flutter application

Repository (GitHub)

License

BSD-3-Clause (license)

Dependencies

dio, flutter, supertokens_flutter

More

Packages that depend on prefabs_user