forked from ReiMerc/skantravels
Add logout button
This commit is contained in:
parent
eef47f6ee4
commit
8e42e54c98
@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
@ -50,4 +51,31 @@ Future<String?> request(BuildContext context, ApiService service, String method,
|
|||||||
}
|
}
|
||||||
|
|
||||||
return response.body;
|
return response.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> isLoggedIn(BuildContext context) async {
|
||||||
|
final messenger = ScaffoldMessenger.of(context);
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
|
final token = prefs.getString('token');
|
||||||
|
if (token == null) return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
String base64 = token.split('.')[1];
|
||||||
|
base64 += List.filled(4 - base64.length % 4, '=').join();
|
||||||
|
|
||||||
|
final payload = jsonDecode(String.fromCharCodes(base64Decode(base64)));
|
||||||
|
|
||||||
|
if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) {
|
||||||
|
messenger.showSnackBar(const SnackBar(content: Text('Token expired, please sign in again')));
|
||||||
|
prefs.remove('token');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again')));
|
||||||
|
prefs.remove('token');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -52,11 +52,11 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
const Text('Password'),
|
const Text('Password'),
|
||||||
TextField(controller: passwordInput, obscureText: true, enableSuggestions: false, autocorrect: false),
|
TextField(controller: passwordInput, obscureText: true, enableSuggestions: false, autocorrect: false),
|
||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
ElevatedButton(onPressed: _login, child: const Text('Log ind')),
|
ElevatedButton(onPressed: _login, child: const Text('Login')),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
TextButton(
|
TextButton(
|
||||||
child: const Text('Registrer konto'),
|
child: const Text('Register account'),
|
||||||
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Registrer'))),
|
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Register'))),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_map/flutter_map.dart';
|
import 'package:flutter_map/flutter_map.dart';
|
||||||
import "package:latlong2/latlong.dart";
|
import 'package:latlong2/latlong.dart';
|
||||||
import 'package:mobile/register.dart';
|
import 'package:mobile/register.dart';
|
||||||
import "login.dart";
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'login.dart';
|
||||||
|
import 'api.dart' as api;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -33,7 +35,9 @@ class MyHomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||||
int _selectedIndex = 0;
|
int _selectedIndex = 0;
|
||||||
|
bool _isLoggedIn = false;
|
||||||
|
|
||||||
void _onItemTapped(int index) {
|
void _onItemTapped(int index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -41,9 +45,40 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _logout() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
|
prefs.remove('token');
|
||||||
|
setState(() => _isLoggedIn = false);
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged out')));
|
||||||
|
Navigator.pop(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _postNavigationCallback(dynamic _) async {
|
||||||
|
final isLoggedIn = await api.isLoggedIn(context);
|
||||||
|
setState(() => _isLoggedIn = isLoggedIn);
|
||||||
|
|
||||||
|
// Close sidebar
|
||||||
|
if (mounted && _scaffoldKey.currentState?.isDrawerOpen == true) {
|
||||||
|
Navigator.pop(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
|
||||||
|
api.isLoggedIn(context)
|
||||||
|
.then((value) => setState(() => _isLoggedIn = value));
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
key: _scaffoldKey,
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||||
title: Text(widget.title),
|
title: Text(widget.title),
|
||||||
@ -61,13 +96,11 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
),
|
),
|
||||||
floatingActionButton: Row(
|
floatingActionButton: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
children: [
|
children: _isLoggedIn ? [] : [
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.push(
|
Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage(title: "Login")))
|
||||||
context,
|
.then(_postNavigationCallback);
|
||||||
MaterialPageRoute(
|
|
||||||
builder: (context) => const LoginPage(title: "Login")));
|
|
||||||
},
|
},
|
||||||
tooltip: 'Login',
|
tooltip: 'Login',
|
||||||
child: const Icon(Icons.login),
|
child: const Icon(Icons.login),
|
||||||
@ -125,36 +158,42 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
thickness: 2,
|
thickness: 2,
|
||||||
indent: 40,
|
indent: 40,
|
||||||
),
|
),
|
||||||
ListTile(
|
...(
|
||||||
title: const Text('Register'),
|
_isLoggedIn ? [
|
||||||
leading: const Icon(Icons.add_box_outlined),
|
ListTile(
|
||||||
selected: _selectedIndex == 3,
|
title: const Text('Log out'),
|
||||||
onTap: () {
|
leading: const Icon(Icons.logout),
|
||||||
// Update the state of the app
|
selected: false,
|
||||||
_onItemTapped(3);
|
onTap: _logout,
|
||||||
// Then close the drawer
|
)
|
||||||
Navigator.push(
|
] : [
|
||||||
context,
|
ListTile(
|
||||||
MaterialPageRoute(
|
title: const Text('Register'),
|
||||||
builder: (context) =>
|
leading: const Icon(Icons.add_box_outlined),
|
||||||
const RegisterPage(title: "Register")));
|
selected: _selectedIndex == 3,
|
||||||
},
|
onTap: () {
|
||||||
),
|
// Update the state of the app
|
||||||
ListTile(
|
_onItemTapped(3);
|
||||||
title: const Text('Login'),
|
// Then close the drawer
|
||||||
leading: const Icon(Icons.login),
|
Navigator.push(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'Register')))
|
||||||
selected: _selectedIndex == 4,
|
.then(_postNavigationCallback);
|
||||||
onTap: () {
|
},
|
||||||
// Update the state of the app
|
),
|
||||||
_onItemTapped(4);
|
ListTile(
|
||||||
// Then close the drawer
|
title: const Text('Login'),
|
||||||
Navigator.push(
|
leading: const Icon(Icons.login),
|
||||||
context,
|
selected: _selectedIndex == 4,
|
||||||
MaterialPageRoute(
|
onTap: () {
|
||||||
builder: (context) => const LoginPage(title: "Login")));
|
// Update the state of the app
|
||||||
},
|
_onItemTapped(4);
|
||||||
),
|
// Then close the drawer
|
||||||
],
|
Navigator.push(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Login')))
|
||||||
|
.then(_postNavigationCallback);
|
||||||
|
},
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ class _RegisterPageState extends State<RegisterPage> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
const SizedBox(height: 80),
|
const SizedBox(height: 80),
|
||||||
const Text('Brugernavn'),
|
const Text('Username'),
|
||||||
TextField(controller: usernameInput),
|
TextField(controller: usernameInput),
|
||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
const Text('Email'),
|
const Text('Email'),
|
||||||
@ -53,11 +53,11 @@ class _RegisterPageState extends State<RegisterPage> {
|
|||||||
const Text('Password'),
|
const Text('Password'),
|
||||||
TextField(controller: passwordInput, obscureText: true, enableSuggestions: false, autocorrect: false),
|
TextField(controller: passwordInput, obscureText: true, enableSuggestions: false, autocorrect: false),
|
||||||
const SizedBox(height: 30),
|
const SizedBox(height: 30),
|
||||||
ElevatedButton(onPressed: _register, child: const Text('Registrer')),
|
ElevatedButton(onPressed: _register, child: const Text('Register')),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
TextButton(
|
TextButton(
|
||||||
child: const Text('Log ind'),
|
child: const Text('Login'),
|
||||||
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Log ind')))
|
onPressed: () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const LoginPage(title: 'Login')))
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user