Compare commits

...

5 Commits

Author SHA1 Message Date
Daniel
518429e2a0 . 2022-05-31 15:38:54 +02:00
Daniel
02000f4a94 Merged from main 2022-05-31 14:54:11 +02:00
dependabot[bot]
8f5c33f4f5 Bump @sap/cds from 5.9.5 to 5.9.6
Bumps [@sap/cds](https://cap.cloud.sap/) from 5.9.5 to 5.9.6.

---
updated-dependencies:
- dependency-name: "@sap/cds"
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-25 14:20:28 +02:00
Daniel Hutzel
a893184736 Add login to bookshop Vue.js app (#358) 2022-05-19 12:16:11 +02:00
Daniel
9370d0544e trying esm modules 2022-05-12 01:44:09 +02:00
10 changed files with 51 additions and 35 deletions

View File

@@ -10,7 +10,7 @@ const books = Vue.createApp ({
list: [], list: [],
book: undefined, book: undefined,
order: { quantity:1, succeeded:'', failed:'' }, order: { quantity:1, succeeded:'', failed:'' },
user: {} user: undefined
} }
}, },
@@ -42,19 +42,25 @@ const books = Vue.createApp ({
} }
}, },
async fetchUserInfo() { async login() {
try { try {
const { data } = await axios.get('/user/me') const { data:user } = await axios.post('/user/login',{})
books.user = data if (user.id !== 'anonymous') books.user = user
} catch (err) { books.user = { id: err.message } } } 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") }).mount("#app")
// initially fill list of books books.getUserInfo()
books.fetch() books.fetch() // initially fill list of books
books.fetchUserInfo()
document.addEventListener('keydown', (event) => { document.addEventListener('keydown', (event) => {
// hide user info on request // hide user info on request
if (event.key === 'u') books.user = undefined if (event.key === 'u') books.user = undefined

View File

@@ -18,11 +18,17 @@
<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"> <form class="user" @submit.prevent="login">
<div>User: {{ user.id || 'anonymous' }}</div> <div v-if="user">
<div>Locale: {{ user.locale }}</div>
<div v-if="user.tenant">Tenant: {{ user.tenant }}</div> <div v-if="user.tenant">Tenant: {{ user.tenant }}</div>
<div> User: {{ user.id }}</div>
<div>Locale: {{ user.locale }}</div>
</div> </div>
<div v-else>
<input type="submit" value="Login" class="muted-button">
<!-- <a href="/user/login()">Login</a> -->
</div>
</form>
<h1> Capire Books </h1> <h1> Capire Books </h1>

View File

@@ -4,7 +4,7 @@
* currencies, if not obtained through @capire/common. * currencies, if not obtained through @capire/common.
*/ */
module.exports = async (db)=>{ export default async (db)=>{
const has_common = db.model.definitions['sap.common.Currencies'].elements.numcode const has_common = db.model.definitions['sap.common.Currencies'].elements.numcode
if (has_common) return if (has_common) return

View File

@@ -1,2 +1,2 @@
const { CatalogService } = require('./srv/cat-service') import { CatalogService } from './srv/cat-service.js'
module.exports = { CatalogService } export { CatalogService }

View File

@@ -2,6 +2,7 @@
"name": "@capire/bookshop", "name": "@capire/bookshop",
"version": "1.0.0", "version": "1.0.0",
"description": "A simple self-contained bookshop service.", "description": "A simple self-contained bookshop service.",
"type": "module",
"files": [ "files": [
"app", "app",
"srv", "srv",

View File

@@ -1,6 +1,6 @@
const cds = require('@sap/cds') import cds from '@sap/cds'
module.exports = cds.service.impl (function(){ export default cds.service.impl (function(){
this.before ('NEW','Authors', genid) this.before ('NEW','Authors', genid)
this.before ('NEW','Books', genid) this.before ('NEW','Books', genid)
}) })

View File

@@ -1,8 +1,8 @@
const cds = require('@sap/cds') import cds from '@sap/cds'
class CatalogService extends cds.ApplicationService { init(){ export 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 // Reduce stock of ordered books if available stock suffices
this.on ('submitOrder', async req => { this.on ('submitOrder', async req => {
@@ -24,5 +24,3 @@ class CatalogService extends cds.ApplicationService { init(){
return super.init() return super.init()
}} }}
module.exports = { CatalogService }

View File

@@ -1,9 +1,7 @@
/** /**
* Exposes user information * Exposes user information
*/ */
@requires: 'authenticated-user'
service UserService { service UserService {
/** /**
* The current user * The current user
*/ */
@@ -13,4 +11,5 @@ service UserService {
tenant : String; tenant : String;
} }
action login() returns me;
} }

View File

@@ -1,4 +1,10 @@
const cds = require('@sap/cds') import cds from '@sap/cds'
module.exports = cds.service.impl((srv) => {
srv.on('READ', 'me', ({ tenant, user, locale }) => ({ id: user.id, locale, tenant })) export default 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')
}) })
}}

12
package-lock.json generated
View File

@@ -1172,9 +1172,9 @@
} }
}, },
"node_modules/@sap/cds": { "node_modules/@sap/cds": {
"version": "5.9.5", "version": "5.9.6",
"resolved": "https://registry.npmjs.org/@sap/cds/-/cds-5.9.5.tgz", "resolved": "https://registry.npmjs.org/@sap/cds/-/cds-5.9.6.tgz",
"integrity": "sha512-H/LIB7WnJ3JKzywnfYd9fOLDKVQokO6/JMUUXbCx+VllYuIQFwClwZ+uYQDgWYSrHePRpuOP5TxLFuMXvhdaag==", "integrity": "sha512-zzDoRrgAbRXUQ2n+BrDErtAyylMgcJxgWhsiJvjiZVMxAucJMPp9V+WlRsaGwSm8O6STC6NHcv9PWQ7500y9EQ==",
"dependencies": { "dependencies": {
"@sap-cloud-sdk/core": "^1.41", "@sap-cloud-sdk/core": "^1.41",
"@sap-cloud-sdk/util": "^1.41", "@sap-cloud-sdk/util": "^1.41",
@@ -8086,9 +8086,9 @@
} }
}, },
"@sap/cds": { "@sap/cds": {
"version": "5.9.5", "version": "5.9.6",
"resolved": "https://registry.npmjs.org/@sap/cds/-/cds-5.9.5.tgz", "resolved": "https://registry.npmjs.org/@sap/cds/-/cds-5.9.6.tgz",
"integrity": "sha512-H/LIB7WnJ3JKzywnfYd9fOLDKVQokO6/JMUUXbCx+VllYuIQFwClwZ+uYQDgWYSrHePRpuOP5TxLFuMXvhdaag==", "integrity": "sha512-zzDoRrgAbRXUQ2n+BrDErtAyylMgcJxgWhsiJvjiZVMxAucJMPp9V+WlRsaGwSm8O6STC6NHcv9PWQ7500y9EQ==",
"requires": { "requires": {
"@sap-cloud-sdk/core": "^1.41", "@sap-cloud-sdk/core": "^1.41",
"@sap-cloud-sdk/util": "^1.41", "@sap-cloud-sdk/util": "^1.41",