Skip to content

Commit

Permalink
last.fm authentication with username
Browse files Browse the repository at this point in the history
  • Loading branch information
HemantKArya committed Oct 5, 2024
1 parent 40d7ffa commit 5acec6a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
35 changes: 24 additions & 11 deletions lib/blocs/lastdotfm/lastdotfm_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ class LastdotfmCubit extends Cubit<LastdotfmState> {
});
}

void initialize(String apiKey, String apiSecret, String sessionKey) {
emit(LastdotfmIntialized(
apiKey: apiKey, apiSecret: apiSecret, sessionKey: sessionKey));
}

Future<void> initializeFromDB() async {
log("Getting Last.FM Keys from DB", name: "Last.FM");
final username =
await BloomeeDBService.getApiTokenDB(GlobalStrConsts.lFMUsername);
final apiKey =
await BloomeeDBService.getApiTokenDB(GlobalStrConsts.lFMApiKey);
final apiSecret =
Expand All @@ -103,15 +100,21 @@ class LastdotfmCubit extends Cubit<LastdotfmState> {

if (apiKey != null &&
apiSecret != null &&
username != null &&
apiKey.isNotEmpty &&
username.isNotEmpty &&
apiSecret.isNotEmpty) {
LastFmAPI.setAPIKey(apiKey);
LastFmAPI.setAPISecret(apiSecret);
if (session != null && session.isNotEmpty) {
LastFmAPI.sessionKey = session;
LastFmAPI.username = username;
LastFmAPI.initialized = true;
emit(LastdotfmIntialized(
apiKey: apiKey, apiSecret: apiSecret, sessionKey: session));
apiKey: apiKey,
apiSecret: apiSecret,
sessionKey: session,
username: username));
}
}
startUpCheck();
Expand All @@ -123,17 +126,25 @@ class LastdotfmCubit extends Cubit<LastdotfmState> {
required String secret,
required String apiKey}) async {
try {
final session = await LastFmAPI.fetchSessionKey(token);
final sessionMap = await LastFmAPI.fetchSessionKey(token);
final session = sessionMap["key"]!;
final name = sessionMap["name"]!;
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMUsername, name, "0");
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMSecret, secret, "0");
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMApiKey, apiKey, "0");
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMSession, session, "0");
log('Session Key: $session', name: 'LastFM API');

if (session.isNotEmpty && apiKey.isNotEmpty && secret.isNotEmpty) {
LastFmAPI.sessionKey = session;
LastFmAPI.username = name;
LastFmAPI.initialized = true;
emit(LastdotfmIntialized(
apiKey: apiKey, apiSecret: secret, sessionKey: session));
apiKey: apiKey,
apiSecret: secret,
sessionKey: session,
username: name,
));
}
} catch (e) {
log("Error: $e", name: "Last.FM");
Expand All @@ -155,13 +166,15 @@ class LastdotfmCubit extends Cubit<LastdotfmState> {

Future<void> remove() async {
LastFmAPI.initialized = false;
LastFmAPI.sessionKey = "";
LastFmAPI.apiKey = "";
LastFmAPI.apiSecret = "";
LastFmAPI.sessionKey = null;
LastFmAPI.apiKey = null;
LastFmAPI.apiSecret = null;
LastFmAPI.username = null;
emit(LastdotfmInitial());
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMSecret, "", "0");
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMApiKey, "", "0");
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMSession, "", "0");
BloomeeDBService.putApiTokenDB(GlobalStrConsts.lFMUsername, "", "0");
}

startUpCheck() async {
Expand Down
4 changes: 3 additions & 1 deletion lib/blocs/lastdotfm/lastdotfm_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ final class LastdotfmIntialized extends LastdotfmState {
final String apiKey;
final String apiSecret;
final String sessionKey;
final String username;

const LastdotfmIntialized({
required this.apiKey,
required this.apiSecret,
required this.sessionKey,
required this.username,
});

@override
List<Object> get props => [apiKey, apiSecret, sessionKey];
List<Object> get props => [apiKey, apiSecret, sessionKey, username];
}

final class LastdotfmFailed extends LastdotfmState {
Expand Down
15 changes: 13 additions & 2 deletions lib/repository/LastFM/lastfmapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class LastFmAPI {
static String? apiKey;
static String? apiSecret;
static String? sessionKey;
static String? username;
static bool initialized = false;
static const String apiUrl = 'http://ws.audioscrobbler.com/2.0/';

Expand Down Expand Up @@ -129,6 +130,12 @@ class LastFmAPI {
}
}

static void setUsername(String name) {
if (name.isNotEmpty) {
username = name;
}
}

static String generateApiSig(Map<String, String> params, String apiSecret) {
List<String> sortedParams = params.keys.toList()..sort();
String sortedParamsString =
Expand Down Expand Up @@ -218,7 +225,7 @@ class LastFmAPI {
return 'http://www.last.fm/api/auth/?api_key=$apiKey&token=$token';
}

static Future<String> fetchSessionKey(String token) async {
static Future<Map<String, String>> fetchSessionKey(String token) async {
if (apiKey == null || apiSecret == null) {
throw Exception("LastFM API not initialized.");
}
Expand All @@ -238,8 +245,12 @@ class LastFmAPI {

if (responseData.containsKey('session')) {
setSession(responseData['session']['key']);
setUsername(responseData['session']['name']);
initialized = true;
return responseData['session']['key'];
return {
'name': responseData['session']['name'],
'key': responseData['session']['key'],
};
} else {
throw Exception('Failed to retrieve session key');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/routes_and_consts/global_str_consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class GlobalStrConsts {
static const String lFMSecret = "lastFMSecret";
static const String lFMUISetting = "lastFMUI";
static const String lFMScrobbleSetting = "lastFMScrobble";
static const String lFMUsernames = "lastFMUsernames";
static const String lFMUsername = "lastFMUsernames";
static const String lFMTrackedCache = "lastFMTrackedCacheForFutureScrobble";
}
14 changes: 9 additions & 5 deletions lib/screens/screen/home_views/setting_views/lastfm_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class _LastDotFMState extends State<LastDotFM> {
final TextEditingController apiKeyController = TextEditingController();
final TextEditingController apiSecretController = TextEditingController();
bool authBtnClicked = false;
String? username;
bool getBtnVisible = false;
String? token;
@override
Expand Down Expand Up @@ -49,6 +50,8 @@ class _LastDotFMState extends State<LastDotFM> {

Future<void> getKeysFromDB() async {
log("Getting Last.FM Keys from DB", name: "Last.FM");
username =
await BloomeeDBService.getApiTokenDB(GlobalStrConsts.lFMUsername);
final apiKey =
await BloomeeDBService.getApiTokenDB(GlobalStrConsts.lFMApiKey);
final apiSecret =
Expand All @@ -59,6 +62,7 @@ class _LastDotFMState extends State<LastDotFM> {
if (apiSecret != null) {
apiSecretController.text = apiSecret;
}

log("Last.FM Keys from DB: $apiKey, $apiSecret", name: "Last.FM");
setState(() {});
}
Expand Down Expand Up @@ -185,12 +189,12 @@ class _LastDotFMState extends State<LastDotFM> {
top: 16,
),
child: Text(
'Last.FM is Authenticated.',
'Hi, ${state.username},\nLast.FM API is Authenticated.',
style: TextStyle(
color:
Default_Theme.successColor.withOpacity(0.7),
fontSize: 12)
.merge(Default_Theme.secondoryTextStyleMedium),
color: Default_Theme.successColor.withOpacity(0.7),
fontSize: 12,
fontFamily: 'Unageo',
),
),
);
} else if (state is LastdotfmFailed) {
Expand Down

0 comments on commit 5acec6a

Please sign in to comment.