Skip to content

Commit

Permalink
feat: store user token in Keychain with Valet
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Jul 19, 2021
1 parent ed46f91 commit 1d18214
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
21 changes: 21 additions & 0 deletions firstfm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
825F0FA026A5EC82007BA84B /* Session.swift in Sources */ = {isa = PBXBuildFile; fileRef = 825F0F9F26A5EC82007BA84B /* Session.swift */; };
825F0FA126A5EDBB007BA84B /* ArtistSearchResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82F0D54E2697AC66007CEA98 /* ArtistSearchResponse.swift */; };
825F0FA926A5F122007BA84B /* LogoutButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 825F0FA826A5F122007BA84B /* LogoutButton.swift */; };
825F0FAF26A60E5C007BA84B /* Valet in Frameworks */ = {isa = PBXBuildFile; productRef = 825F0FAE26A60E5C007BA84B /* Valet */; };
825F0FB126A60FD0007BA84B /* Keychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 825F0FB026A60FD0007BA84B /* Keychain.swift */; };
826CBA992678EB8800B11170 /* ArtistView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 826CBA982678EB8800B11170 /* ArtistView.swift */; };
82844E012673991F00578DD4 /* FirstfmApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82844E002673991F00578DD4 /* FirstfmApp.swift */; };
82844E052673992200578DD4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 82844E042673992200578DD4 /* Assets.xcassets */; };
Expand Down Expand Up @@ -111,6 +113,7 @@
825F0F9D26A5EAE7007BA84B /* SpotifyAlbum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpotifyAlbum.swift; sourceTree = "<group>"; };
825F0F9F26A5EC82007BA84B /* Session.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Session.swift; sourceTree = "<group>"; };
825F0FA826A5F122007BA84B /* LogoutButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoutButton.swift; sourceTree = "<group>"; };
825F0FB026A60FD0007BA84B /* Keychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keychain.swift; sourceTree = "<group>"; };
826CBA982678EB8800B11170 /* ArtistView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArtistView.swift; sourceTree = "<group>"; };
82844DFD2673991F00578DD4 /* firstfm.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = firstfm.app; sourceTree = BUILT_PRODUCTS_DIR; };
82844E002673991F00578DD4 /* FirstfmApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstfmApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -152,6 +155,7 @@
buildActionMask = 2147483647;
files = (
82E80C39269777C60098DC3C /* SwiftUIRefresh in Frameworks */,
825F0FAF26A60E5C007BA84B /* Valet in Frameworks */,
8253DC0C26A0E10100F5F426 /* NotificationBannerSwift in Frameworks */,
825F0F8426A48597007BA84B /* FancyScrollView in Frameworks */,
82505CE42675178B00CCCB58 /* Kingfisher in Frameworks */,
Expand All @@ -165,6 +169,7 @@
isa = PBXGroup;
children = (
4A8C296026983B7300F55ECC /* LastFMAPI.swift */,
825F0FB026A60FD0007BA84B /* Keychain.swift */,
);
path = Service;
sourceTree = "<group>";
Expand Down Expand Up @@ -392,6 +397,7 @@
82E80C38269777C60098DC3C /* SwiftUIRefresh */,
8253DC0B26A0E10100F5F426 /* NotificationBannerSwift */,
825F0F8326A48597007BA84B /* FancyScrollView */,
825F0FAE26A60E5C007BA84B /* Valet */,
);
productName = firstfm;
productReference = 82844DFD2673991F00578DD4 /* firstfm.app */;
Expand Down Expand Up @@ -426,6 +432,7 @@
8253DC0A26A0E10100F5F426 /* XCRemoteSwiftPackageReference "NotificationBanner" */,
825F0F8226A48597007BA84B /* XCRemoteSwiftPackageReference "FancyScrollView" */,
825F0F8526A48C9C007BA84B /* XCRemoteSwiftPackageReference "SwiftUI-Introspect" */,
825F0FAD26A60E5C007BA84B /* XCRemoteSwiftPackageReference "Valet" */,
);
productRefGroup = 82844DFE2673991F00578DD4 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -486,6 +493,7 @@
82505CE12675074E00CCCB58 /* ContentView.swift in Sources */,
8226AB2426998FAB007ECE5F /* TopCountryArtistsResponse.swift in Sources */,
82505CE92675368400CCCB58 /* LastFMImage.swift in Sources */,
825F0FB126A60FD0007BA84B /* Keychain.swift in Sources */,
82A006BA267963010009BD71 /* TopTrackResponse.swift in Sources */,
82FBAE4C2674BCB7000D8E29 /* TopArtistsResponse.swift in Sources */,
825F0F9326A5E90C007BA84B /* Tag.swift in Sources */,
Expand Down Expand Up @@ -770,6 +778,14 @@
minimumVersion = 0.1.3;
};
};
825F0FAD26A60E5C007BA84B /* XCRemoteSwiftPackageReference "Valet" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/square/Valet.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.0.0;
};
};
82E80C37269777C50098DC3C /* XCRemoteSwiftPackageReference "SwiftUIRefresh" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/timbersoftware/SwiftUIRefresh.git";
Expand All @@ -796,6 +812,11 @@
package = 825F0F8226A48597007BA84B /* XCRemoteSwiftPackageReference "FancyScrollView" */;
productName = FancyScrollView;
};
825F0FAE26A60E5C007BA84B /* Valet */ = {
isa = XCSwiftPackageProductDependency;
package = 825F0FAD26A60E5C007BA84B /* XCRemoteSwiftPackageReference "Valet" */;
productName = Valet;
};
82E80C38269777C60098DC3C /* SwiftUIRefresh */ = {
isa = XCSwiftPackageProductDependency;
package = 82E80C37269777C50098DC3C /* XCRemoteSwiftPackageReference "SwiftUIRefresh" */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
"revision": "fa8fac7b5eb5c729983a8bef65f094b5e0d12014",
"version": "0.0.3"
}
},
{
"package": "Valet",
"repositoryURL": "https://github.com/square/Valet.git",
"state": {
"branch": null,
"revision": "2bf3329055f5d71d42a12801dd69d1d770fafa5e",
"version": "4.1.2"
}
}
]
},
Expand Down
13 changes: 13 additions & 0 deletions firstfm/Service/Keychain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Keychain.swift
// firstfm
//
// Created by Stanislas Lange on 19/07/2021.
//

import Foundation
import Valet

func getValet() -> Valet {
return Valet.valet(with: Identifier(nonEmpty: "firstfm")!, accessibility: .whenUnlocked)
}
5 changes: 3 additions & 2 deletions firstfm/ViewModel/AuthViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import Foundation
import SwiftUI
import NotificationBannerSwift
import Valet

class AuthViewModel: ObservableObject {
@AppStorage("lastfm_username") var storedUsername: String?
@AppStorage("lastfm_sk") var storedToken: String?
let myValet = getValet()

func isLoggedIn() -> Bool {
return storedUsername != nil
Expand All @@ -27,7 +28,7 @@ class AuthViewModel: ObservableObject {

if let data = data {
DispatchQueue.main.async {
self.storedToken = data.session.key
try? self.myValet.setString(data.session.key, forKey: "sk")
FloatingNotificationBanner(title: "Successfully logged in", subtitle: "You can now browse your profile", style: .success).show()
}
}
Expand Down
7 changes: 6 additions & 1 deletion firstfm/ViewModel/ScobbledTrackViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import Foundation
import SwiftUI
import NotificationBannerSwift
import Valet

struct Nothing: Codable {}

class ScrobbledTrackViewModel {
@AppStorage("lastfm_sk") var storedToken: String?
let myValet = getValet()

func loveTrack(track: ScrobbledTrack) {
let storedToken = try? myValet.string(forKey: "sk")

LastFMAPI.request(lastFMMethod: "track.love", args: ["artist": track.artist.name, "track": track.name, "sk": storedToken ?? ""]) { (_: Nothing?, error) -> Void in
if error != nil {
DispatchQueue.main.async {
Expand All @@ -25,6 +28,8 @@ class ScrobbledTrackViewModel {
}

func unloveTrack(track: ScrobbledTrack) {
let storedToken = try? myValet.string(forKey: "sk")

LastFMAPI.request(lastFMMethod: "track.unlove", args: ["artist": track.artist.name, "track": track.name, "sk": storedToken ?? ""]) { (_: Nothing?, error) -> Void in
if error != nil {
DispatchQueue.main.async {
Expand Down
4 changes: 3 additions & 1 deletion firstfm/ViewModel/ScrobblesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import Foundation
import SwiftUI
import NotificationBannerSwift
import Valet

class ScrobblesViewModel: ObservableObject {
@Published var scrobbles: [ScrobbledTrack] = []
@AppStorage("lastfm_username") var storedUsername: String?
@AppStorage("lastfm_sk") var storedToken: String?
let myValet = getValet()
var isLoading = true

func getUserScrobbles() {
self.isLoading = true
let storedToken = try? myValet.string(forKey: "sk")

LastFMAPI.request(lastFMMethod: "user.getRecentTracks", args: [
"limit": "30",
Expand Down

0 comments on commit 1d18214

Please sign in to comment.