快速UserDefaults中缺少登录凭据 [英] Login credentials missing in swift UserDefaults

查看:122
本文介绍了快速UserDefaults中缺少登录凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用rest api进行身份验证的应用程序.我现在面临的问题是我也将用户令牌保存在UserDefaults和username中,因为这是获取用户详细信息所需的两个主要参数.因此,如果用户关闭了该应用程序,则当他重新打开该应用程序时,他仍然应该能够查看其个人资料视图,但该个人资料会返回空的详细信息.这是我拥有的UserDefaults代码

I have an application which uses a rest api for authentication. The problem I am facing now is that I save user's token in my UserDefaults and username too because those are the two main parameters needed to get user details. so if the application is closed by the user he should still be able to view the view his profile when he opens the application back but instead the profile returns empty details. this is the UserDefaults codes that I have

let defaults = UserDefaults.standard

var isLoggedIn : Bool {

    get {
        return defaults.bool(forKey: LOGGED_IN_KEY)
    }
    set {
        defaults.set(newValue, forKey: LOGGED_IN_KEY)
    }
}

//Auth Token

var authToken: String {
    get {
        return defaults.value(forKey: TOKEN_KEY) as? String ?? ""
    }
    set {
        defaults.set(newValue, forKey: TOKEN_KEY)
    }
}

var userUsername: String {
    get {
        return defaults.value(forKey: USERNAME_KEY) as? String ?? ""
    }
    set {
        defaults.set(newValue, forKey: USERNAME_KEY)
    }
}

我不知道为什么它不检索用户数据.

I have no idea why it isnt retrieving the user data.

我的第二个问题是,当我注销用户时,所有用户的详细信息都会按预期清除,但是当我尝试以其他用户身份登录时,新用户的authToken和详细信息会在控制台中打印出来,但是用户个人资料会返回前一个人的个人资料.这不应该是.我的代码如下所示

My second question is when I logout the user, all the users details are cleared as expected but the moment I try loging in with a different user, the new user's authToken and details gets printed in the console but the user profile returns the profile of the previous person. which is not supposed to be. my code is shown below

func logoutUser() -> Void {
    pk = 0
    username = ""
    email = ""
    firstName = ""
    lastName = ""
    AuthService.instance.isLoggedIn = false
    AuthService.instance.authToken = ""
    AuthService.instance.userUsername = ""

}

@IBAction func logoutPressed(_ sender: Any) {
    UserDataService.instance.logoutUser()
    dismiss(animated: true, completion: nil)
}

我还要补充一点,当我使用邮递员运行api时,得到的响应是"detail": "Signature has expired.",因此我必须在标头中输入新令牌,以便再次显示用户详细信息

I would also like to add that when i run the api using postman i get a response that "detail": "Signature has expired." so i had to input the new token in the header so it displays the user details again

推荐答案

enum SettingKeys: String {
    case authToken
    //...
}

struct Settings {

    static var authToken: String? {
        get { return UserDefaults.standard.string(forKey: SettingKeys.authToken.rawValue) }
        set(value) { UserDefaults.standard.set(value, forKey: SettingKeys.authToken.rawValue) }
    }

    static func deleteAll(exclude: [SettingKeys] = []) {
        var saveKeys = exclude.map({ $0.rawValue })
        for key in UserDefaults.standard.dictionaryRepresentation().keys {
            if !saveKeys.contains(key) {
                UserDefaults.standard.removeObject(forKey: key)
            }
        }
    }
}

我建议将密钥存储为Enum,因为这样您就可以像这样使用它:

I recommend storing keys as Enum, because then u can use it like that:

//Read
if let token = Settings.authToken {
    //do something
}

//Write
Settings.authToken = "123456"

//Delete settings
Settings.deleteAll()

//or choose what to leave
Settings.deleteAll(exclude: [.authToken])

值得一提的是defaults.synchronize()已过时.

这篇关于快速UserDefaults中缺少登录凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆