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
Map<String, dynamic> json = jsonDecode(token);
User jsonUser = User.fromJson(json);
Login jsonUser = Login.fromJson(json);
final prefs = await SharedPreferences.getInstance();
prefs.setString('token', jsonUser.token);
prefs.setString('id', jsonUser.id);
setState(()
{user = jsonUser;
loggedIn == true;
});
{loggedIn == true;});
if (mounted) {

View File

@ -7,18 +7,30 @@ class Favorite {
Favorite(this.id, this.userId, this.lat, this.lng);
}
class User {
class Login {
String token;
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 username;
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) {
return User(
json['token'],
json['id'],
json['email'],
json['username'],

View File

@ -1,45 +1,63 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:mobile/base/variables.dart';
import 'package:mobile/models.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'base/sidemenu.dart';
import 'package:mobile/base/variables.dart';
import 'api.dart' as api;
class ProfilePage extends StatefulWidget {
const ProfilePage({super.key});
//const ProfilePage({super.key, required this.id});
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
late User userData;
User? userData;
@override
void initState() {
super.initState();
// 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');
});
getProfile();
}
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(() {
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
Widget build(BuildContext context) {
@ -58,7 +76,9 @@ class _ProfilePageState extends State<ProfilePage> {
),
),
Center(
child: Column(
child: userData == null
? const CircularProgressIndicator()
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
@ -67,14 +87,20 @@ class _ProfilePageState extends State<ProfilePage> {
color: Colors.grey,
),
const SizedBox(height: 20),
const Text(
'Username',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
Text(
userData!.username,
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
const Text(
'email@example.com',
style: TextStyle(fontSize: 16, color: Colors.grey),
Text(
userData!.email,
style: const TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
const SizedBox(height: 50),
ElevatedButton(