Skip to content
François Gérard edited this page Nov 21, 2019 · 3 revisions

This is a client api which is only usable in Client (Browser) Context

How to integrate Whoami with webapis.

First you need to add ps-web-apis as dependency in you application Then you have to add Rosetta as PS-Script Loader in the target-web page.

<script type="text/javascript" src="https://rosetta.prod.ps.bild.de/ps-rosetta.js"></script>

Rosetta will load the newest version of the whoami implementation.

examples

Check User is logged in

import {whoamiV1} from 'ps-web-apis'

whoamiV1().then(whoami => {
   if (whoami.isLoggedIn()) {
     // show user that he is logged in
     // provide logout button
   } 
});

Do login

import {whoamiV1} from 'ps-web-apis'

whoamiV1().then(whoami => {
    whoami.doLogin();
});

get UserInfo

import {whoamiV1} from 'ps-web-apis'

whoamiV1().then(whoami => {
    whoami.getUserInfo().then((userinfo) => { 
       // userinfo.first_name
    });
});

Usage of authenticated APIs

Authorized fetch must be used to ensure, that a valid SPRINGToken is included in BE-call

import {whoamiV1} from 'ps-web-apis'

whoamiV1().then(whoami => {
   if (whoami.isLoggedIn()) {
     whoami.authorizedFetch(...)
        .then((resp) => /*handleResp(resp)*/)
        .catch(/* handle unauthorized */)
   } else {
     fetch(...)
         .then((resp) => /*handleResp(resp)*/)
         .catch(/* handle communication error*/)
   } ).catch(() => {
    console.error('handle unavilability of whoami api')
});