157 lines
5.9 KiB
Dart
157 lines
5.9 KiB
Dart
// =================================================================
|
|
// AUTH & ROUTING
|
|
// =================================================================
|
|
|
|
import 'package:CCTV_App/device_management_page.dart';
|
|
import 'package:CCTV_App/main.dart';
|
|
import 'package:CCTV_App/provider.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
class AuthWrapper extends StatelessWidget {
|
|
const AuthWrapper({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Consumer<AppState>(
|
|
builder: (context, appState, child) {
|
|
if (appState.isLoading && appState.currentUser == null) {
|
|
return const Scaffold(
|
|
body: Center(child: CircularProgressIndicator()));
|
|
}
|
|
|
|
if (appState.currentUser == null) {
|
|
return const AuthPage();
|
|
} else {
|
|
return const DeviceManagementPage();
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class AuthPage extends StatefulWidget {
|
|
const AuthPage({super.key});
|
|
@override
|
|
State<AuthPage> createState() => _AuthPageState();
|
|
}
|
|
|
|
class _AuthPageState extends State<AuthPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
final _fullNameController = TextEditingController();
|
|
bool _isLogin = true;
|
|
bool _isLoading = false;
|
|
|
|
Future<void> _submit() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
setState(() => _isLoading = true);
|
|
try {
|
|
if (_isLogin) {
|
|
await supabase.auth.signInWithPassword(
|
|
email: _emailController.text.trim(),
|
|
password: _passwordController.text.trim(),
|
|
);
|
|
} else {
|
|
await supabase.auth.signUp(
|
|
email: _emailController.text.trim(),
|
|
password: _passwordController.text.trim(),
|
|
data: {'full_name': _fullNameController.text.trim()});
|
|
}
|
|
} on AuthException catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(e.message),
|
|
backgroundColor: Theme.of(context).colorScheme.error));
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: const Text('An unexpected error occurred.'),
|
|
backgroundColor: Theme.of(context).colorScheme.error));
|
|
}
|
|
}
|
|
if (mounted) setState(() => _isLoading = false);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Icon(CupertinoIcons.shield_lefthalf_fill,
|
|
size: 80, color: Theme.of(context).primaryColor),
|
|
const SizedBox(height: 24),
|
|
Text(_isLogin ? 'Smart Anti Theft' : 'Buat Akun',
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
|
color: Colors.white, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 8),
|
|
Text(_isLogin ? 'Sign In untuk melanjutkan' : 'Daftar Sekarang',
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.bodyLarge),
|
|
const SizedBox(height: 32),
|
|
if (!_isLogin)
|
|
TextFormField(
|
|
controller: _fullNameController,
|
|
decoration: const InputDecoration(labelText: 'Nama Lengkap'),
|
|
validator: (value) =>
|
|
value!.isEmpty ? 'Masukkan nama lengkap anda' : null,
|
|
),
|
|
if (!_isLogin) const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(labelText: 'Email'),
|
|
keyboardType: TextInputType.emailAddress,
|
|
validator: (value) =>
|
|
value!.isEmpty ? 'Masukkan email google anda' : null,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(labelText: 'Password'),
|
|
obscureText: true,
|
|
validator: (value) => value!.length < 6
|
|
? 'Password harus setidaknya 6 karakter'
|
|
: null,
|
|
),
|
|
const SizedBox(height: 24),
|
|
_isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: ElevatedButton(
|
|
onPressed: _submit,
|
|
style: ElevatedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
|
backgroundColor: Theme.of(context).primaryColor,
|
|
foregroundColor: Colors.black),
|
|
child: Text(_isLogin ? 'SIGN IN' : 'SIGN UP',
|
|
style:
|
|
const TextStyle(fontWeight: FontWeight.bold)),
|
|
),
|
|
TextButton(
|
|
onPressed: () => setState(() => _isLogin = !_isLogin),
|
|
child: Text(
|
|
_isLogin
|
|
? 'Belum memiliki akun? Daftar disini'
|
|
: 'Sudah memiliki akun? Sign In disini',
|
|
style: const TextStyle(color: Colors.white70)),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|