Implement registering

This commit is contained in:
Reimar 2024-08-21 10:19:02 +02:00
parent debbddd05b
commit eef47f6ee4
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
2 changed files with 32 additions and 6 deletions

View File

@ -1,6 +1,4 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'register.dart';
import 'api.dart' as api;

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'login.dart';
import 'api.dart' as api;
class RegisterPage extends StatefulWidget {
const RegisterPage({super.key, required this.title});
@ -11,6 +12,25 @@ class RegisterPage extends StatefulWidget {
}
class _RegisterPageState extends State<RegisterPage> {
final usernameInput = TextEditingController();
final emailInput = TextEditingController();
final passwordInput = TextEditingController();
Future<void> _register() async {
final result = await api.request(context, api.ApiService.auth, 'POST', '/api/Users', {
'username': usernameInput.text,
'email': emailInput.text,
'password': passwordInput.text,
});
if (result == null) return;
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully registered, please login')));
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Log ind')));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -25,15 +45,15 @@ class _RegisterPageState extends State<RegisterPage> {
children: [
const SizedBox(height: 80),
const Text('Brugernavn'),
const TextField(),
TextField(controller: usernameInput),
const SizedBox(height: 30),
const Text('Email'),
const TextField(),
TextField(controller: emailInput),
const SizedBox(height: 30),
const Text('Password'),
const TextField(obscureText: true, enableSuggestions: false, autocorrect: false),
TextField(controller: passwordInput, obscureText: true, enableSuggestions: false, autocorrect: false),
const SizedBox(height: 30),
ElevatedButton(child: const Text('Registrer'), onPressed: () => Navigator.pop(context)),
ElevatedButton(onPressed: _register, child: const Text('Registrer')),
const SizedBox(height: 10),
TextButton(
child: const Text('Log ind'),
@ -45,4 +65,12 @@ class _RegisterPageState extends State<RegisterPage> {
)
);
}
@override
void dispose() {
usernameInput.dispose();
emailInput.dispose();
passwordInput.dispose();
super.dispose();
}
}