RADOM JAXDA
This commit is contained in:
parent
99fc05ce72
commit
d43702a2c7
4 changed files with 96 additions and 47 deletions
|
@ -42,6 +42,11 @@ class ApiClient {
|
|||
Uri.parse('$baseUrl$path'),
|
||||
headers: headers(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Response returned status code: ${response.statusCode}');
|
||||
}
|
||||
|
||||
return jsonDecode(response.body);
|
||||
}
|
||||
|
||||
|
@ -51,6 +56,11 @@ class ApiClient {
|
|||
body: jsonEncode(body),
|
||||
headers: headers(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Response returned status code: ${response.statusCode}');
|
||||
}
|
||||
|
||||
return jsonDecode(response.body);
|
||||
}
|
||||
|
||||
|
@ -60,6 +70,11 @@ class ApiClient {
|
|||
Uri.parse('$baseUrl$path'),
|
||||
headers: headers(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Response returned status code: ${response.statusCode}');
|
||||
}
|
||||
|
||||
return jsonDecode(response.body);
|
||||
}
|
||||
|
||||
|
@ -69,6 +84,11 @@ class ApiClient {
|
|||
body: jsonEncode(body),
|
||||
headers: headers(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Response returned status code: ${response.statusCode}');
|
||||
}
|
||||
|
||||
return jsonDecode(response.body);
|
||||
}
|
||||
|
||||
|
@ -99,4 +119,22 @@ class ApiClient {
|
|||
this.refreshToken = refreshToken;
|
||||
window.localStorage['refreshToken'] = refreshToken;
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (refreshToken == null) {
|
||||
throw Exception("No valid refresh token found");
|
||||
}
|
||||
|
||||
final response = await post(
|
||||
"/token/refresh",
|
||||
{
|
||||
"refresh_token": refreshToken,
|
||||
}
|
||||
);
|
||||
|
||||
token = response['access_token'] as String;
|
||||
window.localStorage['token'] = token!;
|
||||
refreshToken = response['refresh_token'] as String;
|
||||
window.localStorage['refreshToken'] = refreshToken!;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,30 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:fooder_web/screens/main.dart';
|
||||
import 'package:fooder_web/screens/login.dart';
|
||||
import 'package:fooder_web/client.dart';
|
||||
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blueGrey,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: LoginScreen(
|
||||
apiClient: ApiClient(
|
||||
baseUrl: 'https://fooderapi.domandoman.xyz/api',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:fooder_web/screens/based.dart';
|
||||
import 'package:fooder_web/screens/main.dart';
|
||||
|
||||
|
||||
class LoginScreen extends BasedScreen {
|
||||
|
@ -42,11 +43,16 @@ class _LoginScreen extends State<LoginScreen> {
|
|||
}
|
||||
|
||||
void popMeDady() {
|
||||
Navigator.pop(context);
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => MainScreen(apiClient: widget.apiClient),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// login client when button pressed
|
||||
void _login() async {
|
||||
Future<void> _login() async {
|
||||
try {
|
||||
await widget.apiClient.login(
|
||||
usernameController.text,
|
||||
|
@ -58,13 +64,30 @@ class _LoginScreen extends State<LoginScreen> {
|
|||
showError(e.toString());
|
||||
}
|
||||
}
|
||||
@override
|
||||
void initState () {
|
||||
super.initState();
|
||||
_asyncInitState().then((value) => null);
|
||||
}
|
||||
|
||||
Future<void> _asyncInitState() async {
|
||||
super.initState();
|
||||
|
||||
try {
|
||||
await widget.apiClient.refresh();
|
||||
showText("Welcome back!");
|
||||
popMeDady();
|
||||
} on Exception catch (_) {
|
||||
showError("Session is not longer valid, please log in again");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: const Text("FOODER login"),
|
||||
title: const Text("FOODER"),
|
||||
),
|
||||
body: Center(
|
||||
child: Container(
|
||||
|
|
|
@ -1,56 +1,19 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:fooder_web/screens/based.dart';
|
||||
import 'package:fooder_web/screens/login.dart';
|
||||
import 'package:fooder_web/client.dart';
|
||||
import 'package:fooder_web/models/meal.dart';
|
||||
import 'package:fooder_web/models/entry.dart';
|
||||
import 'package:fooder_web/models/diary.dart';
|
||||
import 'package:fooder_web/widgets/diary.dart';
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
class MainScreen extends BasedScreen {
|
||||
const MainScreen({super.key, required super.apiClient});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blueGrey,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: MyHomePage(
|
||||
apiClient: ApiClient(
|
||||
baseUrl: 'https://fooderapi.domandoman.xyz/api',
|
||||
),
|
||||
title: 'FOODER',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends BasedScreen {
|
||||
final String title;
|
||||
|
||||
const MyHomePage({super.key, required super.apiClient, required this.title});
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
// login client when button pressed
|
||||
void _login() async {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => LoginScreen(apiClient: widget.apiClient),
|
||||
),
|
||||
);
|
||||
State<MainScreen> createState() => _MainScreen();
|
||||
}
|
||||
|
||||
class _MainScreen extends State<MainScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var testDiary = Diary(
|
||||
|
@ -97,7 +60,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
title: Text("FOODER"),
|
||||
),
|
||||
body: Center(
|
||||
child: Container(
|
||||
|
|
Loading…
Reference in a new issue