Skip to content

Commit

Permalink
feat: implement real login and store user token
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Jul 17, 2021
1 parent c1fa1b6 commit eaaa413
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions firstfm/Data/Entities/API/LoginResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// LoginResponse.swift
// firstfm
//
// Created by Stanislas Lange on 17/07/2021.
//

import SwiftUI

struct LoginResponse: Codable {
let session: Session
}

struct Session: Codable {
let subscriber: Int
let name, key: String
}
17 changes: 17 additions & 0 deletions firstfm/Data/ViewModel/AuthViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,28 @@

import Foundation
import SwiftUI
import NotificationBannerSwift

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

func isLoggedIn() -> Bool {
return storedUsername != nil
}

func login(username: String, password: String) {
LastFMAPI.request(lastFMMethod: "auth.getMobileSession", args: ["username": username,"password": password]) { (data: LoginResponse?, error) -> Void in
if error != nil {
DispatchQueue.main.async {
FloatingNotificationBanner(title: "Failed to login", subtitle: error?.localizedDescription, style: .danger).show()
}
}

if let data = data {
print(data)
self.storedToken = data.session.key
}
}
}
}
1 change: 1 addition & 0 deletions firstfm/Views/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct LoginView : View {
.padding(.bottom, 20)
Button(action: {
storedUsername = username
auth.login(username: username, password: password)
presentationMode.wrappedValue.dismiss()
print("login clicked")

Expand Down

0 comments on commit eaaa413

Please sign in to comment.