How to keep the user logged in or check if the user is logged in or not in Shopify?

最后都变了- 提交于 2019-12-12 04:29:31

问题


Hi I used the following code to login a user in Shopify.

NSArray *items = @[[BUYAccountCredentialItem itemWithEmail:email], [BUYAccountCredentialItem itemWithPassword:password]];
        BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:items];

        [self.client loginCustomerWithCredentials:credentials callback:^(BUYCustomer * customer, BUYCustomerToken * token, NSError * _Nullable error) {
            if (customer && !error) {

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                [prefs setObject:token.accessToken forKey:@"CustomerToken"];


                [prefs setInteger:[token.customerID integerValue] forKey:@"CustomerId"];
                [prefs synchronize];



                NSLog(@"Success fully loged in token %@ %@",token.accessToken,token.customerID);
                UINavigationController *navigationController = self.navigationController;
                [navigationController popViewControllerAnimated:YES];

            }else{
                [self showEror:@"LogIn Failed" message:@"Please provide valid Details"];
            }
        }];

Every time I have to login and proceed further. I stored token and customerId in local. How can I make sure that user automatically log in when app opens unless they doesn't logout. Thank you.


回答1:


As you are storing CustomerToken in your NSUserDefault, in didFinishLaunchingWithOptions check the value of CustomerToken if there is any value in it navigate the user to your Home Screen if there is not any value in it then navigate the user to your Login Screen.

Also, make sure while logging out, you will clear the value from NSUserDefault



来源:https://stackoverflow.com/questions/40905886/how-to-keep-the-user-logged-in-or-check-if-the-user-is-logged-in-or-not-in-shopi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!