If configs apiUrl not defined fallback to origin.
Also removed dependency of login and register to redirect response from server. Now we send json data with status = 200 when successfully login/registered. Username input element also is set to not capitalize input.
This commit is contained in:
14
src/api.js
14
src/api.js
@@ -1,4 +1,4 @@
|
|||||||
const BASE_URL = __APIURL__ || "http://localhost:30030/";
|
const BASE_URL = __APIURL__ || window.location.origin;
|
||||||
|
|
||||||
const statistics = () => {
|
const statistics = () => {
|
||||||
const url = new URL('/api/purchase/statistics', BASE_URL)
|
const url = new URL('/api/purchase/statistics', BASE_URL)
|
||||||
@@ -119,17 +119,13 @@ const login = (username, password) => {
|
|||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
redirect: "follow",
|
|
||||||
body: JSON.stringify({ username, password })
|
body: JSON.stringify({ username, password })
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url.href, options)
|
return fetch(url.href, options)
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
if (resp.bodyUsed)
|
return resp.json()
|
||||||
return resp.json()
|
|
||||||
else
|
|
||||||
return resp
|
|
||||||
} else {
|
} else {
|
||||||
return handleErrors(resp)
|
return handleErrors(resp)
|
||||||
}
|
}
|
||||||
@@ -143,17 +139,13 @@ const register = (username, password) => {
|
|||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
redirect: 'follow',
|
|
||||||
body: JSON.stringify({ username, password })
|
body: JSON.stringify({ username, password })
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url.href, options)
|
return fetch(url.href, options)
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
if (resp.ok) {
|
if (resp.ok) {
|
||||||
if (resp.bodyUsed)
|
return resp.json()
|
||||||
return resp.json()
|
|
||||||
else
|
|
||||||
return resp
|
|
||||||
} else {
|
} else {
|
||||||
return handleErrors(resp)
|
return handleErrors(resp)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,20 @@
|
|||||||
<form aria-label="User registration" @submit.prevent>
|
<form aria-label="User registration" @submit.prevent>
|
||||||
<div class="label-div">
|
<div class="label-div">
|
||||||
<label>Brukernavn</label>
|
<label>Brukernavn</label>
|
||||||
<input type="text" v-model="username" placeholder="Brukernavn" @keyup.enter="login" />
|
<input type="text"
|
||||||
|
v-model="username"
|
||||||
|
placeholder="Brukernavn"
|
||||||
|
autocapitalize="none"
|
||||||
|
@keyup.enter="submit" />
|
||||||
</div>
|
</div>
|
||||||
<div class="label-div row">
|
<div class="label-div row">
|
||||||
<label>Passord</label>
|
<label>Passord</label>
|
||||||
<input type="password" v-model="password" placeholder="Passord" @keyup.enter="login" />
|
<input type="password" v-model="password" placeholder="Passord" @keyup.enter="submit" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button class="vin-button" @click="register">Registrer bruker</button>
|
<button class="vin-button" @click="submit">Registrer bruker</button>
|
||||||
|
|
||||||
<p v-if="error" class="error">{{ error }}</p>
|
<div v-if="error" class="error">{{ error }}</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -29,17 +33,10 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
register() {
|
submit() {
|
||||||
register(this.username, this.password)
|
register(this.username, this.password)
|
||||||
.then(resp => {
|
.then(resp => this.$router.push("/"))
|
||||||
if (resp.redirected) { this.$router.push("/") }
|
.catch(error => this.error = error.message || error)
|
||||||
})
|
|
||||||
.catch(this.registerErrorResponse)
|
|
||||||
|
|
||||||
},
|
|
||||||
registerErrorResponse(error) {
|
|
||||||
console.log("error", error)
|
|
||||||
this.error = error.message || error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,14 +4,18 @@
|
|||||||
<form aria-label="User signin" @submit.prevent>
|
<form aria-label="User signin" @submit.prevent>
|
||||||
<div class="label-div">
|
<div class="label-div">
|
||||||
<label>Brukernavn</label>
|
<label>Brukernavn</label>
|
||||||
<input type="text" v-model="username" placeholder="Brukernavn" @keyup.enter="login" />
|
<input type="text"
|
||||||
|
v-model="username"
|
||||||
|
placeholder="Brukernavn"
|
||||||
|
autocapitalize="none"
|
||||||
|
@keyup.enter="submit" />
|
||||||
</div>
|
</div>
|
||||||
<div class="label-div row">
|
<div class="label-div row">
|
||||||
<label>Passord</label>
|
<label>Passord</label>
|
||||||
<input type="password" v-model="password" placeholder="Passord" @keyup.enter="login" />
|
<input type="password" v-model="password" placeholder="Passord" @keyup.enter="submit" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button class="vin-button" @click="login">Logg inn</button>
|
<button class="vin-button" @click="submit">Logg inn</button>
|
||||||
|
|
||||||
<div v-if="error" class="error">{{ error }}</div>
|
<div v-if="error" class="error">{{ error }}</div>
|
||||||
</form>
|
</form>
|
||||||
@@ -29,16 +33,10 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
login() {
|
submit() {
|
||||||
login(this.username, this.password)
|
login(this.username, this.password)
|
||||||
.then(resp => {
|
.then(resp => this.$router.push("update"))
|
||||||
if (resp.redirected) { this.$router.push("update") }
|
.catch(error => this.error = error.message || error)
|
||||||
})
|
|
||||||
.catch(this.loginErrorResponse)
|
|
||||||
|
|
||||||
},
|
|
||||||
loginErrorResponse(error) {
|
|
||||||
this.error = error.message || error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user