profile data can now be displayed when logged in

This commit is contained in:
LilleBRG 2024-08-23 15:19:15 +02:00
parent 57d52c3fd7
commit a4103ab11e
4 changed files with 95 additions and 60 deletions

View File

@ -29,17 +29,14 @@ class _LoginPageState extends State<LoginPage> {
// Assuming token is a JSON string // Assuming token is a JSON string
Map<String, dynamic> json = jsonDecode(token); Map<String, dynamic> json = jsonDecode(token);
User jsonUser = User.fromJson(json); Login jsonUser = Login.fromJson(json);
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
prefs.setString('token', jsonUser.token); prefs.setString('token', jsonUser.token);
prefs.setString('id', jsonUser.id); prefs.setString('id', jsonUser.id);
setState(() setState(()
{user = jsonUser; {loggedIn == true;});
loggedIn == true;
});
if (mounted) { if (mounted) {

View File

@ -7,18 +7,30 @@ class Favorite {
Favorite(this.id, this.userId, this.lat, this.lng); Favorite(this.id, this.userId, this.lat, this.lng);
} }
class User { class Login {
String token; String token;
String id; String id;
Login(this.token, this.id);
factory Login.fromJson(Map<String, dynamic> json) {
return Login(
json['token'],
json['id'],
);
}
}
class User {
String id;
String email; String email;
String username; String username;
DateTime createdAt; DateTime createdAt;
User(this.token, this.id, this.email, this.username, this.createdAt); User( this.id, this.email, this.username, this.createdAt);
factory User.fromJson(Map<String, dynamic> json) { factory User.fromJson(Map<String, dynamic> json) {
return User( return User(
json['token'],
json['id'], json['id'],
json['email'], json['email'],
json['username'], json['username'],

View File

@ -1,45 +1,63 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/variables.dart'; import 'package:mobile/base/variables.dart';
import 'package:mobile/models.dart'; import 'package:mobile/models.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'base/sidemenu.dart'; import 'base/sidemenu.dart';
import 'package:mobile/base/variables.dart'; import 'api.dart' as api;
class ProfilePage extends StatefulWidget { class ProfilePage extends StatefulWidget {
const ProfilePage({super.key}); const ProfilePage({super.key});
//const ProfilePage({super.key, required this.id});
@override @override
State<ProfilePage> createState() => _ProfilePageState(); State<ProfilePage> createState() => _ProfilePageState();
} }
class _ProfilePageState extends State<ProfilePage> { class _ProfilePageState extends State<ProfilePage> {
late User userData; User? userData;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
getProfile();
// Check if the user is logged in when the page is entered
if (loggedIn == false) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please log in')));
Navigator.pushReplacementNamed(context, '/login');
});
} }
Future<void> getProfile() async {
if (!loggedIn) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please log in')),
);
Navigator.pushReplacementNamed(context, '/login');
});
return;
}
if (user != null) {
setState(() { setState(() {
userData = user!; userData = user!;
}); });
} else {
final prefs = await SharedPreferences.getInstance();
String? id = prefs.getString('id');
if (id != null) {
final response = await api
.request(context, api.ApiService.auth, 'GET', '/api/users/$id', null);
if (response == null) return;
Map<String, dynamic> json = jsonDecode(response);
User jsonUser = User.fromJson(json);
setState(() {
userData = jsonUser;
user = jsonUser;
});
}
}
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -58,7 +76,9 @@ class _ProfilePageState extends State<ProfilePage> {
), ),
), ),
Center( Center(
child: Column( child: userData == null
? const CircularProgressIndicator()
: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
const Icon( const Icon(
@ -67,14 +87,20 @@ class _ProfilePageState extends State<ProfilePage> {
color: Colors.grey, color: Colors.grey,
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
const Text( Text(
'Username', userData!.username,
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
const Text( Text(
'email@example.com', userData!.email,
style: TextStyle(fontSize: 16, color: Colors.grey), style: const TextStyle(
fontSize: 16,
color: Colors.grey,
),
), ),
const SizedBox(height: 50), const SizedBox(height: 50),
ElevatedButton( ElevatedButton(