-1

Have problem to read data from json. My login.json file

{
    "Users": [
    {
      "login" : "admin",
      "password" : "admin"
      }
    ]
}

Here is my login.xml.view

<core:View
    controllerName="sap.ui.demo.myFiori.view.login"
    xmlns="sap.m"
    xmlns:l="sap.ui.layout"
    xmlns:core="sap.ui.core" >      
    <Page
    title="{i18n>LoginIn}">
    <VBox       
        class="marginBoxContent" >
  <items>
        <Label text="User" />
      <Input

        id="nameInput"
        type="Text"
        placeholder="..." />
        <Label text="password" />
    <Input
        id="passwInput"
        type="Password"
        placeholder=" ..." />
    <Text id="description" class="marginOnlyTop" />
<Button text="Login"   press="handleContinue" />
        </items>
    </VBox>
    </Page>
</core:View>

and login.controller.js

jQuery.sap.require("sap.ui.demo.myFiori.util.Formatter");

sap.ui.controller("sap.ui.demo.myFiori.view.login", {

    handleContinue : function (evt) {

        var name = this.getView().byId("nameInput").getValue(); 
        var paswd = this.getView().byId("passwInput").getValue(); 

    if (name == "log" && paswd == "pass") {
            var context = evt.getSource().getBindingContext();
            this.nav.to("Master", context); 
}
else {
        jQuery.sap.require("sap.m.MessageToast");
        sap.m.MessageToast.show("Wrong login");

        }

}
});

This login screen works, but I can't get login and password from json file and currently these data are taken from if sentence, which is not good. Any suggestions?

mangus
  • 1
  • 1
  • 2

2 Answers2

0

First, I am assuming that this is just a test app, as having the correct plain text auth credentials actually in the client app is rather bad practice ...

The crux of your question seems to be: "How can I access JSON data from a file?"

There is a Q&A already on SO with that theme: How to get data from a json file? but if you want to use a model, which is a common practice in UI5 apps, then you could:

1) Create a JSON model, specifying the name of the JSON file; the JSONModel mechanism will take care of the loading; assign this to the view

this.getView().setModel(new sap.ui.model.json.JSONModel('login.json'), "login");

2) Access the data in the JSON model when you need to check, using the getData() method

// Note - extremely bad practice - don't do this for real!
var authinfo = this.getView().getModel("login").getData().Users[0];
if (name == authinfo.login && paswd == authinfo.password) {
  ....

(I'm indexing 0 of Users as you seem to have your authinfo inside an array)

Community
  • 1
  • 1
qmacro
  • 3,025
  • 23
  • 33
0

Maybe I defined something wrong before but now it works. I changed login.controller.js. Now it looks like this:

jQuery.sap.require("sap.ui.demo.myFiori.util.Formatter");        
sap.ui.controller("sap.ui.demo.myFiori.view.login", {

    onInit: function () {
         var oModels = new sap.ui.model.json.JSONModel("login.json");   
         sap.ui.getCore().setModel(oModels);
          },
   handleContinue : function (evt) {

        var authinfo = this.getView().getModel().getData().Users[0];
        var name = this.getView().byId("nameInput").getValue(); 
        var paswd = this.getView().byId("passwInput").getValue(); 

        if (name == authinfo.login && paswd == authinfo.passw) {
            var context = evt.getSource().getBindingContext();
            this.nav.to("Master", context); 
        }
    else {
            jQuery.sap.require("sap.m.MessageToast");
            sap.m.MessageToast.show("Wrong login");       
            }       
    }
  });
mangus
  • 1
  • 1
  • 2