profile data can now be displayed when logged in
This commit is contained in:
parent
57d52c3fd7
commit
a4103ab11e
@ -13,7 +13,7 @@ enum ApiService {
|
|||||||
|
|
||||||
Future<String?> request(BuildContext context, ApiService service, String method,
|
Future<String?> request(BuildContext context, ApiService service, String method,
|
||||||
String path, Object? body) async {
|
String path, Object? body) async {
|
||||||
final messenger = ScaffoldMessenger.of(context);
|
final messenger = ScaffoldMessenger.of(context);
|
||||||
|
|
||||||
final host = switch (service) {
|
final host = switch (service) {
|
||||||
ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'),
|
ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'),
|
||||||
|
@ -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) {
|
||||||
|
@ -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'],
|
||||||
|
@ -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');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
userData = user!;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -58,33 +76,41 @@ class _ProfilePageState extends State<ProfilePage> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
Center(
|
Center(
|
||||||
child: Column(
|
child: userData == null
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
? const CircularProgressIndicator()
|
||||||
children: [
|
: Column(
|
||||||
const Icon(
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
Icons.account_circle,
|
children: [
|
||||||
size: 100,
|
const Icon(
|
||||||
color: Colors.grey,
|
Icons.account_circle,
|
||||||
),
|
size: 100,
|
||||||
const SizedBox(height: 20),
|
color: Colors.grey,
|
||||||
const Text(
|
),
|
||||||
'Username',
|
const SizedBox(height: 20),
|
||||||
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
Text(
|
||||||
),
|
userData!.username,
|
||||||
const SizedBox(height: 10),
|
style: const TextStyle(
|
||||||
const Text(
|
fontSize: 24,
|
||||||
'email@example.com',
|
fontWeight: FontWeight.bold,
|
||||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 50),
|
const SizedBox(height: 10),
|
||||||
ElevatedButton(
|
Text(
|
||||||
onPressed: () {
|
userData!.email,
|
||||||
// Add your edit action here
|
style: const TextStyle(
|
||||||
},
|
fontSize: 16,
|
||||||
child: const Text('Edit'),
|
color: Colors.grey,
|
||||||
),
|
),
|
||||||
],
|
),
|
||||||
),
|
const SizedBox(height: 50),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
// Add your edit action here
|
||||||
|
},
|
||||||
|
child: const Text('Edit'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user