Skip to content

Commit

Permalink
feat: Add registration functionality to LoginPage
Browse files Browse the repository at this point in the history
  • Loading branch information
Polabiel committed Jul 30, 2024
1 parent 84615ed commit 478c263
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
11 changes: 9 additions & 2 deletions lib/src/app/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class _LoginPageState extends State<LoginPage> {
onPressed: () async {
FocusScopeNode currentFocus = FocusScope.of(context);
if (_formKey.currentState!.validate()) {
String? isLogged = await UserRepository.authenticate(
_formKey, _emailController, _passwordController);
bool isLogged = await UserRepository.registerUser(
_emailController, _passwordController);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
Expand All @@ -180,6 +180,13 @@ class _LoginPageState extends State<LoginPage> {
),
);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Invalid email or password'),
backgroundColor: Colors.red,
),
);
}
},
child: const Text(
Expand Down
45 changes: 24 additions & 21 deletions lib/src/app/register_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:gohealth/src/app/home/home_page.dart';
import 'package:gohealth/src/components/custom_input_field.dart';
import 'package:gohealth/src/database/repositories/user.repository.dart';
import 'package:gohealth/src/app/login_page.dart';

class RegisterPage extends StatefulWidget {
const RegisterPage({super.key});
Expand All @@ -14,6 +15,7 @@ class RegisterPageState extends State<RegisterPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _nameController = TextEditingController();

@override
Widget build(BuildContext context) {
Expand All @@ -27,20 +29,22 @@ class RegisterPageState extends State<RegisterPage> {
const SizedBox(height: 50),
const FlutterLogo(size: 100),
const SizedBox(height: 50),
const Column(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
const Text(
"Name",
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
),
SizedBox(height: 10),
TextField(
obscureText: true,
decoration: InputDecoration(
const SizedBox(height: 10),
TextFormField(
obscureText: false,
keyboardType: TextInputType.text,
controller: _nameController,
decoration: const InputDecoration(
hintText: "Enter your Name",
hintStyle: TextStyle(
color: Colors.grey,
Expand Down Expand Up @@ -77,7 +81,7 @@ class RegisterPageState extends State<RegisterPage> {
),
SizedBox(height: 10),
TextField(
obscureText: true,
obscureText: false,
decoration: InputDecoration(
hintText: "Enter your Email",
hintStyle: TextStyle(
Expand Down Expand Up @@ -119,7 +123,7 @@ class RegisterPageState extends State<RegisterPage> {
foregroundColor: const Color.fromRGBO(
0, 90, 226, 0.85), // Cor do texto
),
child: const Text('Esqueceu a senha?'),
child: const Text('Forget your Password?'),
),
],
),
Expand All @@ -145,21 +149,21 @@ class RegisterPageState extends State<RegisterPage> {
),
),
onPressed: () {
final Future<String> token = UserRepository.authenticate(
_formKey,
_emailController,
_passwordController,
);
if (_formKey.currentState!.validate()) {
final Future<bool> token = UserRepository.authenticate(
_emailController, _passwordController, _nameController);

if (token != '') {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const Homepage()),
);
if (token != '' || token != null) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const Homepage()),
);
}
}
},
child: const Text(
'Login',
'Register',
style: TextStyle(
fontSize: 18.0, // Tamanho do texto
fontWeight: FontWeight.w900, // Deixa o texto em negrito
Expand All @@ -174,8 +178,7 @@ class RegisterPageState extends State<RegisterPage> {
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const RegisterPage()),
MaterialPageRoute(builder: (context) => const LoginPage()),
);
},
child: const Text('Log in to your existing account'),
Expand Down
29 changes: 22 additions & 7 deletions lib/src/database/repositories/user.repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

final String baseUrl = dotenv.env['BASE_URL'] ?? 'https://api.example.com';
const String baseUrl = 'http://localhost:3000';

class UserRepository {
static Future<String> authenticate(GlobalKey<FormState> credentialsFormState,
TextEditingController email, TextEditingController password) async {
static Future<bool> authenticate(
TextEditingController email, TextEditingController password, TextEditingController nameController) async {
final response = await http.post(
Uri.parse('$baseUrl/user/authenticate'),
body: {
Expand All @@ -20,10 +20,25 @@ class UserRepository {
// Autenticação bem-sucedida, retornar o token JWT
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('token', response.body);
return response.body;
return true;
} else {
return false;
}
}

static Future<bool> registerUser(TextEditingController email,
TextEditingController name, TextEditingController password) async {
final response =
await http.post(Uri.parse('$baseUrl/user/register'), body: {
'email': email.text,
'name': name.text,
'password': password.text,
});

if (response.statusCode == 200) {
return true;
} else {
// Autenticação falhou, lançar uma exceção ou retornar null
throw Exception('Falha na autenticação');
return false;
}
}

Expand All @@ -40,7 +55,7 @@ class UserRepository {
if (prefs.getString('token') != null) {
return prefs.getString('token')!;
}
throw Exception('Token not found');
return '';
}

static Future<User> getUserProfile(String token) async {
Expand Down

0 comments on commit 478c263

Please sign in to comment.