Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3e234bc
Create PR for #31
matievisthekat Oct 9, 2022
f2863aa
reorganise import order
matievisthekat Oct 7, 2022
07db2c6
refactor button widget to enable all text style options
matievisthekat Oct 7, 2022
b7e7967
only set state if widget is mounted
matievisthekat Oct 7, 2022
f1d4241
go to register page from landing
matievisthekat Oct 7, 2022
7d59d70
redesign the error message widgets
matievisthekat Oct 7, 2022
5571d10
create register and login page layout
matievisthekat Oct 9, 2022
fb17c53
minor adjustments
matievisthekat Oct 9, 2022
a214b7d
remove unused imports
matievisthekat Oct 9, 2022
7670663
use new register/login page layout
matievisthekat Oct 9, 2022
1e7f336
use register/login page layout
matievisthekat Oct 9, 2022
ad2b06a
pushReplacement for CustomTextButton
matievisthekat Oct 9, 2022
135ab30
add ionicons
matievisthekat Oct 13, 2022
8fcb353
add global layout widget
matievisthekat Oct 13, 2022
7e2b2fd
style page tabs
matievisthekat Oct 13, 2022
b582ef4
add vscode debug config
matievisthekat Oct 13, 2022
cb6431f
rename `GlobalLayout` to `Homepage`
matievisthekat Oct 13, 2022
001184d
rename `Homepage` to `Home`
matievisthekat Oct 13, 2022
9d34323
rename `Home` to `Feed` and move to pages dir
matievisthekat Oct 13, 2022
8c63246
moving landing to pages dir
matievisthekat Oct 13, 2022
f0f05d9
update error message (toast)
matievisthekat Oct 13, 2022
81ad1b4
making a start
matievisthekat Oct 13, 2022
fc8f233
add `flutterMode: debug`
matievisthekat Oct 14, 2022
45365a8
add deviceId
matievisthekat Oct 14, 2022
36878b2
move bottom navigation bar to Scaffold.bottomNavigationBar
matievisthekat Oct 14, 2022
fefa6cc
remove scaffold
matievisthekat Oct 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"configurations": [
{
"name": "Flutter",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"deviceId": "Pixel_2_API_32"
}
]
}
105 changes: 94 additions & 11 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:trail_buddies/routing.dart';
import 'package:trail_buddies/user.dart';


void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
bool isLoggedIn = false;

Future<String?> getToken() async {
final prefs = await SharedPreferences.getInstance();
final prefsToken = prefs.getString('token');
return prefsToken;
}

Future<bool> verifyToken(BuildContext ctx, String token) async {
final response = await get(
Uri.parse('http://10.0.2.2:3000/api/v1/users/me'),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
);

var json = jsonDecode(response.body) as Map<String, dynamic>;

if (response.statusCode != 200) {
if (mounted) {
setState(() {
isLoggedIn = false;
});
}
} else {
final user = Provider.of<User>(ctx, listen: false);
user.setAll(
newToken: token,
newId: json['id'],
newUsername: json['username'],
newEmail: json['email'],
newVerified: json['verified'],
newAdmin: json['admin'],
);
return true;
}
return false;
}

Future<bool> checkIfLoggedIn(BuildContext ctx) async {
final token = await getToken();
if (token == null || token.isEmpty) return false;
final isValidToken = await verifyToken(ctx, token);
return isValidToken;
}

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<User>(
Expand All @@ -24,15 +80,42 @@ class MyApp extends StatelessWidget {
updatedAt: '',
createdAt: '',
),
child: MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: '/',
onGenerateRoute: (settings) {
final name = settings.name ?? '';

return getPage(name);
},
),
builder: (ctx, child) {
return FutureBuilder(
future: checkIfLoggedIn(ctx),
builder: (_, snap) {
if (snap.hasData) {
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: snap.data == true ? '/' : '/login',
onGenerateRoute: (settings) {
final name = settings.name ?? '/';

return getPage(name);
},
);
}

if (snap.hasError) {
Fluttertoast.showToast(
msg: 'Failed to login with stored credentials',
);
FlutterError.presentError(FlutterErrorDetails(
exception: snap.error ?? {},
context: ErrorDescription(
'Error while validating token in _CheckLogin'),
));
}

return const CircularProgressIndicator();
// return Container(
// alignment: Alignment.center,
// padding: const EdgeInsets.all(32),
// child: const CircularProgressIndicator(),
// );
},
);
},
);
}
}
141 changes: 0 additions & 141 deletions lib/pages/check_login/home.dart

This file was deleted.

17 changes: 9 additions & 8 deletions lib/pages/check_login/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../user.dart';
import 'home.dart';
import 'landing.dart';
import '../feed.dart';
import '../landing.dart';
import '../login.dart';

class CheckLogin extends StatefulWidget {
Expand Down Expand Up @@ -40,9 +40,11 @@ class _CheckLogin extends State<CheckLogin> {
var json = jsonDecode(response.body) as Map<String, dynamic>;

if (response.statusCode != 200) {
setState(() {
isLoggedIn = false;
});
if (mounted) {
setState(() {
isLoggedIn = false;
});
}
} else {
final user = Provider.of<User>(context, listen: false);
user.setAll(
Expand Down Expand Up @@ -71,11 +73,10 @@ class _CheckLogin extends State<CheckLogin> {
future: checkIfLoggedIn(),
builder: (context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData) {
return snapshot.data == true ? const HomePage() : const LandingPage();
return snapshot.data == true ? const FeedPage() : const LandingPage();
} else if (snapshot.hasError) {
Fluttertoast.showToast(
msg:
'Failed to validate token. Please report this incident to [email protected]',
msg: 'Failed to login with stored credentials',
);
FlutterError.presentError(FlutterErrorDetails(
exception: snapshot.error ?? {},
Expand Down
Loading