From eef47f6ee4d591db75538302a876d05e33aa56a8 Mon Sep 17 00:00:00 2001 From: Reimar Date: Wed, 21 Aug 2024 10:19:02 +0200 Subject: [PATCH] Implement registering --- Mobile/lib/login.dart | 2 -- Mobile/lib/register.dart | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index ac48bbd..c5181bb 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -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; diff --git a/Mobile/lib/register.dart b/Mobile/lib/register.dart index 2432a14..6c72e4b 100644 --- a/Mobile/lib/register.dart +++ b/Mobile/lib/register.dart @@ -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 { + final usernameInput = TextEditingController(); + final emailInput = TextEditingController(); + final passwordInput = TextEditingController(); + + Future _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 { 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 { ) ); } + + @override + void dispose() { + usernameInput.dispose(); + emailInput.dispose(); + passwordInput.dispose(); + super.dispose(); + } }