Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
ale_biagi
Product and Topic Expert
Product and Topic Expert
7,362

Prerequisite


To follow-up with this blog post you must have read and completed the post: Setup project for development.

Configure the User API Service


The first approach to get the authenticated user information is the simplest as it does not rely on any backend service to be consumed and can be utilized directly in an HTML5 application implemented as the front-end (UI) of the solution.

That's because the application router (AppRouter) exposes a user API that returns the details of the users who are logged in to the application.

You can learn the details of such API in this page from the official SAP BTP documentation.

Now, let's make use of it to achieve our goal. For that, we just need to add a new route to the xs-app.json file (prepared via the instructions in the setup project for development post.

Add the following route configuration as the second route in the routes list of your xs-app.json file:
        {
"source": "^/user-api(.*)",
"target": "$1",
"service": "sap-approuter-userapi"
},

By doing so, your xs-app.json file should now look like this:


Figure 1 - User API Service added to xs-app.json



Adjust the Index Page


The User API Service provides two endpoints:

  • /currentUser

  • /attributes


So, let's just set them as links in the index.html page by modifying these two lines:
                    <li><a href="https://blogs.sap.com/user-api/currentUser">1a. Using the <b>/currentUser</b> endpoint</a></li>
<li><a href="https://blogs.sap.com/user-api/attributes">1b. Using the <b>/attributes</b> endpoint</a></li>

By doing so, your index.html file should now look like this:


Figure 2 - New links in index.html



Test the Approach


In the Terminal move to the app directory (cd app), if not yet positioned in there, and run the command:
npm run start

After a few seconds you should see a pop-up in the bottom-right corner of Business Application Studio with a button to open the application in a new tab:


Figure 3 - Pop-up to open app in new tab


Click on that button to access the application's index page: at this point the AppRouter will execute the OAuth 2.0 authentication flow (login) and display the index.html page:


Figure 4 - Application index page


Click on the first link (the /currentUser endpoint) and the user information should be displayed in JSON format like demonstrated below:


Figure 5 - Information fetched by the /currentUser endpoint


The "name" attribute is the "username" utilized for login and the "scopes" are basically the "roles" that are assigned to the user in the context of the application. In this case  "openid, uaa.user" just means that the user is a valid platform user that's authenticated. Those scopes have practically no effect in terms of authorization for an application as every valid user in the platform has them assigned by default.

Now, use your browser's back button to go back to the index page and click on the second link (the /attributes endpoint) and another set of information should be displayed in JSON format like demonstrated below:


Figure 6 - Information fetched by the /attributes endpoint


You'll notice that the only attribute that's not fetched by that endpoint is the "displayName". So, if, in your application, you don't need that attribute you can use this endpoint instead of the first.

And that concludes the steps to get the authenticated user information directly from an HTML5 app.

Conclusion


After having gone through the steps of this blog post you should have successfully fetched authenticated user information in an HTML5 app leveraging the User API Service from the application router. The next step would be to try one of the other different approaches proposed in this blog posts series (if not yet done).

Please, do not hesitate to submit your questions in SAP Community through the Q&A tag link: https://answers.sap.com/index.html

Other blog posts in this series


8 Comments
Ahmedkhan29789
Participant
0 Kudos
Damn, Its look quite easy now to find the logged in user by addy just 5 lines of code, Thanks a lot for sharing this.
ale_biagi
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ahmed,

Glad that you liked the approach!

Best Regards,

Alessandro

aimnhsni
Explorer
0 Kudos

Hi, how can I resolve this issue when I clcik the link? im new to SAP, thank you so much!.

ale_biagi
Product and Topic Expert
Product and Topic Expert
0 Kudos

It's hard to tell without knowing exactly what you did, so I assume you should have missed some step of the instructions.

Please, make sure you have properly added this snippet to your xs-app.json in the right place for correct approuter configuration:

        {
"source": "^/user-api(.*)",
"target": "$1",
"service": "sap-approuter-userapi"
},

The complete file must look exactly like this:

Otherwise the /user-api won't be recognized by the approuter.

former_member851551
Discoverer
0 Kudos
Hi alessandro.biagi ,

I am trying to follow the same steps and added the required route in xs-app.json. Our application is a UI5 HTML application that uses managed app-router, so no custom app-router config or files in the repository.

We notice the following issue: When we try to call the end point "/user-api/currentUser", the application is reaching out to the url https://<cloudfoundry-org-url>/user-api/currentuser and returns a 404.

Manually if I try to edit this url to https://<cloudfoundry-org-url>/<app-id>/user-api/currentuser and do a GET to this modified url, I am able to get a response. Any idea what I might be missing for the router to build the full url ?

 

Thanks,

Akhil
ale_biagi
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi akhilrajk,

Does your route to the managed approuter root content look like this?

    {
"source": "^(.*)$",
"target": "$1",
"service": "html5-apps-repo-rt",
"authenticationType": "xsuaa"
}

Configuring routes in xs-app.json can really be a puzziling task that usually makes one dizzy... But in general the trick lies in the way you set the regex in source and target properties.

You can refer to these sources to get a deeper understanding of the routing configuration file (xs-app.json) in more detail:

Hope those can be helpful.

Cheers,

Alessandro

mbasak
Newcomer
0 Kudos

Hi Alessandro.biagi ,

I am trying to follow the same steps and added the required route in xs-app.json. Our application is a UI5 HTML application that uses managed app-router, so no custom app-router config or files in the repository.

We notice the following issue: When we try to call the end point "/user-api/currentUser", the application is reaching out to the url /user-api/currentuser  and returns a 404.

 

xs-app.json
------
{
  "welcomeFile": "/index.html",
  "authenticationMethod": "route",
  "routes": [
    {
      "source": "^(.*)$",
      "target": "$1",
      "service": "html5-apps-repo-rt",
      "authenticationType": "xsuaa"
    },
    {
      "source": "^/user-api(.*)",
      "target": "$1",
      "service": "sap-approuter-userapi"
    }
  ]
}
----
xs-security.json
----
{
  "xsappname": "project1",
  "tenant-mode": "dedicated",
  "description": "Security profile of called application",
  "scopes": [  {
    "name": "uaa.user",
    "description": "UAA"
  },
  {
    "name": "$XSAPPNAME.View1",
    "description": "View1"
  },{
    "name": "$XSAPPNAME.View2",
    "description": "View2"
  }],
  "role-templates": [ {
    "name": "Token_Exchange",
    "description": "UAA",
    "scope-references": [
      "uaa.user"
    ]
  },
  {
    "name": "CustomRole",
    "description": "generated",
    "scope-references": [
      "$XSAPPNAME.View1"
    ]
  },{
    "name": "CustomRole1",
    "description": "generated",
    "scope-references": [
      "$XSAPPNAME.View2"
    ]
  }]
}

 

Regards,

Mriganka FIORI_APP_FLDR_Structure.jpg

stiffy89
Discoverer
0 Kudos

For anyone who is struggling with the 404 error, when you call the endpoint "/user-api/currentUser", try removing the leading forward slash so "user-api/currentUser". The leading forward slash references an absolute path (subaccount) rather than a relative path (application)