Add: get token from api
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gymlink_module_web/main_mobile.dart';
|
import 'package:gymlink_module_web/main_mobile.dart';
|
||||||
import 'package:gymlink_module_web/providers/main.dart';
|
import 'package:gymlink_module_web/providers/main.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -71,11 +73,50 @@ class _ExamplePageState extends State<ExamplePage> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
Future.microtask(
|
// Future.microtask(
|
||||||
() => context.read<GymLinkProvider>().onTokenReceived('token123'));
|
// () => context.read<GymLinkProvider>().onTokenReceived('token123'));
|
||||||
Future.microtask(() => context
|
Future.microtask(() => context
|
||||||
.read<GymLinkProvider>()
|
.read<GymLinkProvider>()
|
||||||
.setTheme(ThemeData.light(useMaterial3: true)));
|
.setTheme(ThemeData.light(useMaterial3: true)));
|
||||||
|
|
||||||
|
Future.microtask(() => context.read<GymLinkProvider>().setOnError(() {
|
||||||
|
const snackBar = SnackBar(
|
||||||
|
content: Text('Ошибка подключения'),
|
||||||
|
duration: Duration(seconds: 3), // Длительность отображения Snackbar
|
||||||
|
behavior: SnackBarBehavior
|
||||||
|
.fixed, // Поведение Snackbar (fixed или floating)
|
||||||
|
);
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
||||||
|
Future.delayed(const Duration(seconds: 3))
|
||||||
|
.then((value) => _setToken());
|
||||||
|
}));
|
||||||
|
|
||||||
|
Future.microtask(() async {
|
||||||
|
await _setToken();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setToken() async {
|
||||||
|
final token = await _getToken();
|
||||||
|
if (token != '') {
|
||||||
|
context.read<GymLinkProvider>().onTokenReceived(token);
|
||||||
|
} else {
|
||||||
|
context.read<GymLinkProvider>().onError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> _getToken() async {
|
||||||
|
var url =
|
||||||
|
Uri.http('gymlink.freemyip.com:8080', 'api/auth/authorize_client');
|
||||||
|
var response = await http.post(url, body: {
|
||||||
|
'GymKey': 'eeb42dcb-8e5b-4f21-825a-3fc7ada43445',
|
||||||
|
'id': '123'
|
||||||
|
}); // Just testing token
|
||||||
|
var decodedBody = jsonDecode(response.body) as Map;
|
||||||
|
if (decodedBody['payload'] == null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return decodedBody['payload']['token'];
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ class GymLinkProvider with ChangeNotifier {
|
|||||||
bool get isLoading => _isLoading;
|
bool get isLoading => _isLoading;
|
||||||
bool _blackTheme = false;
|
bool _blackTheme = false;
|
||||||
bool get blackTheme => _blackTheme;
|
bool get blackTheme => _blackTheme;
|
||||||
|
String _token = '';
|
||||||
|
String get token => _token;
|
||||||
ThemeData _theme = myTheme;
|
ThemeData _theme = myTheme;
|
||||||
ThemeData get theme => _theme;
|
ThemeData get theme => _theme;
|
||||||
void Function() _onError = () => {};
|
void Function() _onError = () => {};
|
||||||
@@ -13,13 +15,16 @@ class GymLinkProvider with ChangeNotifier {
|
|||||||
void Function() get onError => _onError;
|
void Function() get onError => _onError;
|
||||||
|
|
||||||
void onTokenReceived(String token) {
|
void onTokenReceived(String token) {
|
||||||
if (token == 'token123') {
|
_token = token;
|
||||||
_isLoading = false;
|
_isLoading = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
} else {
|
// if (token == 'token123') {
|
||||||
_isLoading = true;
|
// _isLoading = false;
|
||||||
notifyListeners();
|
// notifyListeners();
|
||||||
}
|
// } else {
|
||||||
|
// _isLoading = true;
|
||||||
|
// notifyListeners();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void changeTheme(int color) {
|
void changeTheme(int color) {
|
||||||
|
|||||||
@@ -7,10 +7,35 @@
|
|||||||
};
|
};
|
||||||
let appState = window._appState;
|
let appState = window._appState;
|
||||||
|
|
||||||
let btn = document.getElementById('token');
|
function getToken() {
|
||||||
btn.addEventListener('click', function () {
|
fetch(
|
||||||
appState.onTokenReceived('token123');
|
'http://gymlink.freemyip.com:8080/api/auth/authorize_client',
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
GymKey: 'eeb42dcb-8e5b-4f21-825a-3fc7ada43445', // Just testing token
|
||||||
|
id: '123',
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.then(res => res.json())
|
||||||
|
.catch(e => {
|
||||||
|
console.log(e);
|
||||||
|
setTimeout(getToken, 1000);
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
if (data.payload)
|
||||||
|
appState.onTokenReceived(data.payload.token);
|
||||||
|
else {
|
||||||
|
console.log(data);
|
||||||
|
setTimeout(getToken, 1000);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let btn = document.getElementById('token');
|
||||||
|
btn.addEventListener('click', getToken);
|
||||||
|
|
||||||
let colorChangeBtnRed = document.getElementById('colorChangeBtnRed');
|
let colorChangeBtnRed = document.getElementById('colorChangeBtnRed');
|
||||||
colorChangeBtnRed.addEventListener('click', function () {
|
colorChangeBtnRed.addEventListener('click', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user