Last active
February 2, 2024 17:25
-
-
Save wonderkidshihab/1c8a91bec6e579e9e974bdeddac57d2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```dart | |
part of 'user.m.dart'; | |
extension UserExt on User? { | |
Future<void> save() async { | |
try { | |
await prefs.setString('user', this!.toRawJson()); | |
} catch (e) { | |
log('Error: $e'); | |
} | |
log('user saved: ${this!.toJson()}'); | |
} | |
String? getUserFromSharedPref() => prefs.getString('user'); | |
Future<Map<String, String>?> get headers async { | |
final token = await this?.tokens?.getUpdatedToken; | |
return { | |
'Content-Type': 'application/json', | |
'x-api-key': xApiKey, | |
if (token != null) 'Authorization': 'Bearer $token', | |
}; | |
} | |
} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```dart | |
import 'dart:convert'; | |
import 'dart:developer'; | |
part 'user.m.ext.dart'; | |
class UserAfterLoginModel { | |
String? userId; | |
String? phoneNumber; | |
String? userType; | |
Tokens? tokens; | |
bool? isComplete; | |
String? completeStep; | |
User? user; | |
UserAfterLoginModel({ | |
this.userId, | |
this.phoneNumber, | |
this.userType, | |
this.tokens, | |
this.isComplete, | |
this.completeStep, | |
this.user, | |
}); | |
UserAfterLoginModel copyWith({ | |
String? userId, | |
String? phoneNumber, | |
String? userType, | |
Tokens? tokens, | |
bool? isComplete, | |
String? completeStep, | |
User? user, | |
}) => | |
UserAfterLoginModel( | |
userId: userId ?? this.userId, | |
phoneNumber: phoneNumber ?? this.phoneNumber, | |
userType: userType ?? this.userType, | |
tokens: tokens ?? this.tokens, | |
isComplete: isComplete ?? this.isComplete, | |
completeStep: completeStep ?? this.completeStep, | |
user: user ?? this.user, | |
); | |
factory UserAfterLoginModel.fromRawJson(String str) => UserAfterLoginModel.fromJson(json.decode(str)); | |
String toRawJson() => json.encode(toJson()); | |
factory UserAfterLoginModel.fromJson(Map<String, dynamic> json) => UserAfterLoginModel( | |
userId: json['user_id'], | |
phoneNumber: json['phone_number'], | |
userType: json['user_type'], | |
tokens: json['tokens'] == null ? null : Tokens.fromJson(json['tokens']), | |
isComplete: json['is_complete'], | |
completeStep: json['complete_step'], | |
user: json['driver_data'] == null ? null : User.fromJson(json['driver_data']), | |
); | |
Map<String, dynamic> toJson() => { | |
'user_id': userId, | |
'phone_number': phoneNumber, | |
'user_type': userType, | |
'tokens': tokens?.toJson(), | |
'is_complete': isComplete, | |
'complete_step': completeStep, | |
'driver_data': user?.toJson(), | |
}; | |
} | |
class User { | |
String? userId; | |
User({ | |
this.userId, | |
}); | |
factory User.fromRawJson(String str) => User.fromJson(json.decode(str)); | |
factory User.fromJson(Map<String, dynamic> json) => User( | |
userId: json[_Json.userId], | |
); | |
String toRawJson() => json.encode(toJson()); | |
Map<String, dynamic> toJson() => { | |
_Json.userId: userId, | |
}..removeWhere((key, value) => value == null); | |
static User? get current { | |
final userJson = prefs.getString('user'); | |
if (userJson != null) return User.fromRawJson(userJson); | |
return null; | |
} | |
User copyWith({ | |
String? userId, | |
}) { | |
return User( | |
userId: userId ?? this.userId, | |
); | |
} | |
} | |
class _Json { | |
static const userId = 'user_id'; | |
} | |
class _JsonToken { | |
static const accessToken = 'access'; | |
static const refreshToken = 'refresh'; | |
} | |
class Tokens { | |
String? refresh; | |
String? access; | |
Tokens({ | |
this.refresh, | |
this.access, | |
}); | |
factory Tokens.fromRawJson(String str) => Tokens.fromJson(json.decode(str)); | |
factory Tokens.fromJson(Map<String, dynamic> json) => Tokens( | |
refresh: json[_JsonToken.refreshToken], | |
access: json[_JsonToken.accessToken], | |
); | |
String toRawJson() => json.encode(toJson()); | |
Map<String, dynamic> toJson() => { | |
_JsonToken.refreshToken: refresh, | |
_JsonToken.accessToken: access, | |
}; | |
Future<String?> get getUpdatedToken async { | |
try { | |
if (access == null || refresh == null) return null; | |
final expired = Jwt.isExpired(access!) || | |
Jwt.getExpiryDate(access!)!.isBefore(DateTime.now().toUtc().add(const Duration(minutes: 1))); | |
if (!expired) return access; | |
final res = await post( | |
accessToRefreshUri, | |
headers: {'Content-Type': 'application/json', 'x-api-key': xApiKey}, | |
body: jsonEncode({'refresh': refresh}), | |
); | |
if (res.isSuccessful) { | |
final token = res.json['data'][_JsonToken.accessToken]; | |
log('NewToken: $token'); | |
if (token == null) return null; | |
final user = User.current!; | |
await user.copyWith(tokens: User.current?.tokens?.copyWith(access: token)).save(); | |
return User.current!.tokens!.access; | |
} else { | |
await prefs.clear(); | |
// ignore: use_build_context_synchronously | |
navigatorKey.currentContext!.push(const SplashScreen()); | |
} | |
} catch (e) { | |
log(e.toString()); | |
} | |
return null; | |
} | |
Tokens copyWith({ | |
String? refresh, | |
String? access, | |
}) { | |
return Tokens( | |
refresh: refresh ?? this.refresh, | |
access: access ?? this.access, | |
); | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment