145 lines
4.2 KiB
Dart
145 lines
4.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
import 'home.dart';
|
|
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
@override
|
|
_LoginPageState createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
final _displayNameController = TextEditingController();
|
|
|
|
bool _isLogin = true;
|
|
bool _isLoading = false;
|
|
|
|
final String baseUrl = ''; // Replace with your actual API base URL
|
|
|
|
Future<void> _handleAuth() async {
|
|
final email = _emailController.text.trim();
|
|
final password = _passwordController.text.trim();
|
|
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
|
|
try {
|
|
if (_isLogin) {
|
|
// Call login endpoint and save token
|
|
final token = await _login(email, password);
|
|
if (token != null) {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('token', token);
|
|
Navigator.pushReplacement(
|
|
context, MaterialPageRoute(builder: (context) => HomePage()));
|
|
}
|
|
} else {
|
|
// Call register endpoint
|
|
final uid = await _register(email, password, _displayNameController.text.trim());
|
|
if (uid != null) {
|
|
setState(() {
|
|
_isLogin = true;
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Registration successful! Please login.')));
|
|
}
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(SnackBar(content: Text(e.toString())));
|
|
} finally {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<String?> _login(String email, String password) async {
|
|
final url = Uri.parse('$baseUrl/login');
|
|
final response = await http.post(
|
|
url,
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: json.encode({'email': email, 'password': password}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final body = json.decode(response.body);
|
|
return body['uid'];
|
|
} else {
|
|
throw Exception('Login failed: ${response.body}');
|
|
}
|
|
}
|
|
|
|
Future<String?> _register(String email, String password, String displayName) async {
|
|
final url = Uri.parse('$baseUrl/register');
|
|
final response = await http.post(
|
|
url,
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: json.encode({
|
|
'email': email,
|
|
'password': password,
|
|
'display_name': displayName,
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final body = json.decode(response.body);
|
|
return body['uid'];
|
|
} else {
|
|
throw Exception('Registration failed: ${response.body}');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(_isLogin ? 'Login' : 'Register'),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
controller: _emailController,
|
|
decoration: InputDecoration(labelText: 'Email'),
|
|
),
|
|
TextField(
|
|
controller: _passwordController,
|
|
decoration: InputDecoration(labelText: 'Password'),
|
|
obscureText: true,
|
|
),
|
|
if (!_isLogin)
|
|
TextField(
|
|
controller: _displayNameController,
|
|
decoration: InputDecoration(labelText: 'Display Name'),
|
|
),
|
|
const SizedBox(height: 20),
|
|
_isLoading
|
|
? CircularProgressIndicator()
|
|
: ElevatedButton(
|
|
onPressed: _handleAuth,
|
|
child: Text(_isLogin ? 'Login' : 'Register'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_isLogin = !_isLogin;
|
|
});
|
|
},
|
|
child: Text(_isLogin
|
|
? 'Don\'t have an account? Register'
|
|
: 'Already have an account? Login'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|