Browse Source

added store getter and setter

master
TheNils 3 months ago
parent
commit
88cc4341a3
  1. 6
      lib/api/api.dart
  2. 31
      lib/store/user.dart
  3. 7
      lib/views/account.dart
  4. 2
      lib/views/login/signInForm.dart

6
lib/api/api.dart

@ -20,8 +20,6 @@ class Api {
var client = http.Client();
void setHeaders(String jwt, String refreshToken, int expires) {
print('setting new refresh token');
print(refreshToken);
this.apiHeaders = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer $jwt',
@ -165,13 +163,9 @@ class Api {
.then((res) {
if (res.statusCode == 200) {
var json = jsonDecode(res.body);
print(res.body);
setHeaders(json['data']['access_token'],
json['data']['refresh_token'], json['data']['expires']);
print('refreshed : ');
print(json['data']['refresh_token']);
return true;
} else {
return false;

31
lib/store/user.dart

@ -8,11 +8,12 @@ class UserController extends GetxController {
final localStorage = new LocalStorage('user-store');
final api = Api();
final user = Rx<Map<dynamic, dynamic>>({}).obs;
final _user = Rx<Map<dynamic, dynamic>>({}).obs;
get user => _user().value;
set user(payload) {
user(Rx({...user().value, ...payload}));
localStorage.setItem('user', jsonEncode(payload));
_user(Rx({..._user().value, ...payload}));
localStorage.setItem('user', jsonEncode(user));
}
Future<Map<dynamic, dynamic>> authenticate(
@ -20,8 +21,8 @@ class UserController extends GetxController {
var res = await api.authenticate(payload);
if (!res.containsKey('error')) {
this.user = res;
this.fetch();
user = res;
fetch();
}
return res;
}
@ -32,26 +33,30 @@ class UserController extends GetxController {
var localUser = await localStorage.getItem('user');
if (localUser != null) {
this.user = jsonDecode(localUser);
var canResume = await api.resumeSession(
user().value['access_token'], user().value['refresh_token']);
user = jsonDecode(localUser);
var canResume =
await api.resumeSession(user['access_token'], user['refresh_token']);
if (canResume) {
this.user = {'refresh_token': this.api.credentials['refreshToken']};
this.fetch();
user = {'refresh_token': api.credentials['refreshToken']};
fetch();
} else {
await this.localStorage.deleteItem('user');
}
}
// fetchApi();
super.onInit();
}
Future<Map<dynamic, dynamic>> fetch() async {
var res = await api.get('/users/me');
if (!res.containsKey('error')) {
this.user = {...user().value, ...res};
user = res;
}
return user().value;
return user;
}
void changeUser() {
user = {'first_name': 'testeur', 'last_name': 'testeur last name'};
}
// Future<

7
lib/views/account.dart

@ -6,6 +6,7 @@ class Account extends StatelessWidget {
final UserController userController = Get.put(UserController());
void test() {
userController.changeUser();
print(userController.user());
}
@ -19,8 +20,10 @@ class Account extends StatelessWidget {
child: Obx(
() => (Column(
children: [
Text('${userController.user().value['id']}'),
Text('${userController.user().value['first_name']}'),
Text('${userController.user['id']}'),
Text('${userController.user['first_name']}'),
Text('${userController.user['last_name']}'),
Text('${userController.user['email']}'),
// Obx(() => Text('${userController.user().lastName}')),
// Obx(() => Text('${userController.user().email}')),
// Obx(() => Text('${userController.user().avatar}')),

2
lib/views/login/signInForm.dart

@ -84,7 +84,7 @@ class SignInFormState extends State<SignInForm> {
child: Text('Sign In'),
),
Text(serverError),
Obx(() => Text('${userController.user().value['first_name']}'))
Obx(() => Text('${userController.user['first_name']}'))
// Text('${userController.get().firstName}')
],
),

Loading…
Cancel
Save