Bookshop showing user info
Show current user name, locale and tenant (if available). Useful in deployments w/ real auth and tenant contexts.
This commit is contained in:
committed by
Christian Georgi
parent
5fc86d45ad
commit
bb55c432c9
@@ -9,7 +9,8 @@ const books = Vue.createApp ({
|
|||||||
return {
|
return {
|
||||||
list: [],
|
list: [],
|
||||||
book: undefined,
|
book: undefined,
|
||||||
order: { quantity:1, succeeded:'', failed:'' }
|
order: { quantity:1, succeeded:'', failed:'' },
|
||||||
|
user: {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -37,12 +38,24 @@ const books = Vue.createApp ({
|
|||||||
book.stock = res.data.stock
|
book.stock = res.data.stock
|
||||||
books.order = { quantity, succeeded: `Successfully ordered ${quantity} item(s).` }
|
books.order = { quantity, succeeded: `Successfully ordered ${quantity} item(s).` }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
books.order = { quantity, failed: e.response.data.error.message }
|
books.order = { quantity, failed: e.response.data.error ? e.response.data.error.message : e.response.data }
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
|
async fetchUserInfo() {
|
||||||
|
try {
|
||||||
|
books.user = (await axios.get('/user/Me')).data
|
||||||
|
if (!books.user.ID) books.user.ID = 'anonymous'
|
||||||
|
} catch (err) { books.user = {}; books.user.ID = err.message }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).mount("#app")
|
}).mount("#app")
|
||||||
|
|
||||||
// initially fill list of books
|
// initially fill list of books
|
||||||
books.fetch()
|
books.fetch()
|
||||||
|
|
||||||
|
books.fetchUserInfo()
|
||||||
|
document.addEventListener('keydown', (event) => {
|
||||||
|
// hide user info on request
|
||||||
|
if (event.key === 'u') books.user = undefined
|
||||||
|
})
|
||||||
|
|||||||
@@ -11,12 +11,19 @@
|
|||||||
.rating-stars { color:teal }
|
.rating-stars { color:teal }
|
||||||
.succeeded { color:teal }
|
.succeeded { color:teal }
|
||||||
.failed { color:red }
|
.failed { color:red }
|
||||||
|
.user {text-align: end; color: grey;}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="small-container", style="margin-top: 70px;">
|
<body class="small-container", style="margin-top: 70px;">
|
||||||
<div id='app'>
|
<div id='app'>
|
||||||
|
|
||||||
|
<div v-if="user" class="user">
|
||||||
|
<div>User: {{ user.ID || 'anonymous' }}</div>
|
||||||
|
<div>Locale: {{ user.locale }}</div>
|
||||||
|
<div v-if="user.tenant">Tenant: {{ user.tenant }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<h1> Capire Books </h1>
|
<h1> Capire Books </h1>
|
||||||
|
|
||||||
<input type="text" placeholder="Search..." @input="search">
|
<input type="text" placeholder="Search..." @input="search">
|
||||||
|
|||||||
17
bookshop/srv/user-service.cds
Normal file
17
bookshop/srv/user-service.cds
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* Exposes user information
|
||||||
|
*/
|
||||||
|
@requires : 'authenticated-user'
|
||||||
|
service UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current user
|
||||||
|
*/
|
||||||
|
@odata.singleton
|
||||||
|
entity Me {
|
||||||
|
ID : String;
|
||||||
|
locale : String;
|
||||||
|
tenant : String;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
bookshop/srv/user-service.js
Normal file
11
bookshop/srv/user-service.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const cds = require('@sap/cds');
|
||||||
|
|
||||||
|
module.exports = cds.service.impl((srv) => {
|
||||||
|
srv.on('READ', 'Me', ({ user }) => {
|
||||||
|
return {
|
||||||
|
ID: user.id,
|
||||||
|
locale: user.locale,
|
||||||
|
tenant: user.tenant,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
const cds = require('@sap/cds/lib')
|
const cds = require('@sap/cds/lib')
|
||||||
const { GET, expect } = cds.test ('@capire/bookshop')
|
const { GET, expect, axios } = cds.test ('@capire/bookshop')
|
||||||
if (cds.User.default) cds.User.default = cds.User.Privileged // hard core monkey patch
|
axios.defaults.auth = { username: 'alice', password: 'admin' }
|
||||||
else cds.User = cds.User.Privileged // hard core monkey patch for older cds releases
|
|
||||||
|
|
||||||
describe('OData Protocol', () => {
|
describe('OData Protocol', () => {
|
||||||
|
|
||||||
@@ -76,3 +75,18 @@ describe('OData Protocol', () => {
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('Misc', () => {
|
||||||
|
|
||||||
|
it('serves user info', async () => {
|
||||||
|
{
|
||||||
|
const { data } = await GET (`/user/Me`)
|
||||||
|
expect(data).to.containSubset({ ID: 'alice', locale:'en', tenant: null })
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const { data } = await GET (`/user/Me`, {auth: { username: 'joe' }})
|
||||||
|
expect(data).to.containSubset({ ID: 'joe', locale:'en', tenant: null })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user