lifeto-shop/src/pages/login.vue

90 lines
1.9 KiB
Vue
Raw Normal View History

2022-07-01 01:27:18 +00:00
<template>
2022-07-03 10:25:12 +00:00
<div> {{loginString}} </div>
2022-07-01 01:27:18 +00:00
<section class="login_modal">
<div class="login_field">
<label for="userName">Username: </label>
<input
type="text"
id="login-username"
v-model="username"
required
/>
</div>
<div class="login_field">
<label for="password">Password: </label>
<input
type="password"
id="login-password"
v-model="password"
required
/>
</div>
<div class="login_field">
<button
type="button"
id="loginButton"
v-on:click="login()"
>Login</button>
2022-07-06 07:48:11 +00:00
<button
type="button"
id="logoutButton"
v-on:click="logout()"
>Logout</button>
2022-07-01 01:27:18 +00:00
</div>
2022-07-06 07:48:11 +00:00
2022-07-01 01:27:18 +00:00
</section>
</template>
<script lang="ts" setup>
2022-07-03 10:25:12 +00:00
const username = ref("")
const password = ref("")
const loginString = ref("not logged in")
2022-07-01 01:27:18 +00:00
2022-07-03 10:25:12 +00:00
const login = () => {
new LoginHelper(username.value, password.value).login()
.then((session)=>{
console.log(session, "adding to storage")
storage.AddSession(session)
2022-07-06 07:48:11 +00:00
window.location.reload()
2022-07-03 10:25:12 +00:00
}).catch((e)=>{
if(e.code == "ERR_BAD_REQUEST") {
alert("invalid username/password")
return
}
alert("unknown error, please report")
console.log(e)
})
}
2022-07-06 07:48:11 +00:00
const logout = () => {
storage.RemoveSession()
localStorage.clear()
window.location.reload()
}
2022-07-03 10:25:12 +00:00
const s = storage.GetSession()
const api = new LTOApiv0(s)
if (s != undefined) {
username.value = s.user
}
const updateLogin = () => {
api.GetLoggedin().then((res)=>{
if(res) {
loginString.value = "logged in as " + s.user
}
})
2022-07-01 01:27:18 +00:00
}
2022-07-03 10:25:12 +00:00
updateLogin()
2022-07-01 01:27:18 +00:00
</script>
<script lang="ts">
2022-07-03 10:25:12 +00:00
import { defineComponent, computed, PropType, defineProps, defineEmits, ref} from 'vue';
import { LTOApiv0 } from '../lib/lifeto';
2022-07-01 01:27:18 +00:00
import { LoginHelper, Session } from '../lib/session';
import { storage } from '../session_storage';
</script>