diff --git a/bookshop/app/vue/app.js b/bookshop/app/vue/app.js
index c96797d5..18e4ef23 100644
--- a/bookshop/app/vue/app.js
+++ b/bookshop/app/vue/app.js
@@ -10,7 +10,7 @@ const books = Vue.createApp ({
list: [],
book: undefined,
order: { quantity:1, succeeded:'', failed:'' },
- user: {}
+ user: undefined
}
},
@@ -42,19 +42,25 @@ const books = Vue.createApp ({
}
},
- async fetchUserInfo() {
+ async login() {
try {
- const { data } = await axios.get('/user/me')
- books.user = data
+ const { data:user } = await axios.post('/user/login',{})
+ if (user.id !== 'anonymous') books.user = user
} catch (err) { books.user = { id: err.message } }
- }
+ },
+
+ async getUserInfo() {
+ try {
+ const { data:user } = await axios.get('/user/me')
+ if (user.id !== 'anonymous') books.user = user
+ } catch (err) { books.user = { id: err.message } }
+ },
}
}).mount("#app")
-// initially fill list of books
-books.fetch()
+books.getUserInfo()
+books.fetch() // initially fill list of books
-books.fetchUserInfo()
document.addEventListener('keydown', (event) => {
// hide user info on request
if (event.key === 'u') books.user = undefined
diff --git a/bookshop/app/vue/index.html b/bookshop/app/vue/index.html
index 36aff1d7..9795a65a 100644
--- a/bookshop/app/vue/index.html
+++ b/bookshop/app/vue/index.html
@@ -18,11 +18,17 @@
-
-
User: {{ user.id || 'anonymous' }}
-
Locale: {{ user.locale }}
-
Tenant: {{ user.tenant }}
-
+
Capire Books
diff --git a/bookshop/srv/cat-service.js b/bookshop/srv/cat-service.js
index 9fa85cca..eb314db5 100644
--- a/bookshop/srv/cat-service.js
+++ b/bookshop/srv/cat-service.js
@@ -2,7 +2,7 @@ const cds = require('@sap/cds')
class CatalogService extends cds.ApplicationService { init(){
- const { Books } = cds.entities ('sap.capire.bookshop')
+ const { Books } = this.entities ('sap.capire.bookshop')
// Reduce stock of ordered books if available stock suffices
this.on ('submitOrder', async req => {
diff --git a/bookshop/srv/user-service.cds b/bookshop/srv/user-service.cds
index 1897f60e..47f58971 100644
--- a/bookshop/srv/user-service.cds
+++ b/bookshop/srv/user-service.cds
@@ -1,9 +1,7 @@
/**
* Exposes user information
*/
-@requires: 'authenticated-user'
service UserService {
-
/**
* The current user
*/
@@ -13,4 +11,5 @@ service UserService {
tenant : String;
}
+ action login() returns me;
}
diff --git a/bookshop/srv/user-service.js b/bookshop/srv/user-service.js
index 61f95fd8..2ecb68d9 100644
--- a/bookshop/srv/user-service.js
+++ b/bookshop/srv/user-service.js
@@ -1,4 +1,9 @@
const cds = require('@sap/cds')
-module.exports = cds.service.impl((srv) => {
- srv.on('READ', 'me', ({ tenant, user, locale }) => ({ id: user.id, locale, tenant }))
-})
+module.exports = class UserService extends cds.Service { init(){
+ this.on('READ', 'me', ({ tenant, user, locale }) => ({ id: user.id, locale, tenant }))
+ this.on('login', (req) => {
+ if (req.user._is_anonymous)
+ req._.res.set('WWW-Authenticate','Basic realm="Users"').sendStatus(401)
+ else return this.read('me')
+ })
+}}