User mixin and toggle login popup after action.

This commit is contained in:
iamBadgers
2026-05-10 14:01:31 -07:00
parent ee437d047f
commit 12093456d1
5 changed files with 72 additions and 43 deletions

View File

@@ -8,58 +8,47 @@
</Form> </Form>
<div v-if="userData.authenticated"> <div v-if="userData.authenticated">
<div>Logged in as: {{ userData.is_admin ? "Admin" : "" }} {{ userData.username }}</div> <div>Logged in as: {{ userData.is_admin ? 'Admin' : '' }} {{ userData.username }}</div>
<Form v-slot="$form" @submit="onResetPassword"> <Form v-slot="$form" @submit="onResetPassword">
<Password name="password" placeholder="Password" :feedback="false" fluid /> <Password name="password" placeholder="Password" :feedback="false" fluid />
<Button type="submit" label="Reset Password" fluid /> <Button type="submit" label="Reset Password" fluid />
</Form> </Form>
<Button @click=logout() label="Log Out" fluid /> <Button @click="onLogout()" label="Log Out" fluid />
</div> </div>
</template> </template>
<style></style> <style></style>
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref } from "vue" import { onMounted, ref, defineEmits } from 'vue'
import { Form } from '@primevue/forms' import { Form } from '@primevue/forms'
import InputText from 'primevue/inputtext' import InputText from 'primevue/inputtext'
import Button from 'primevue/button' import Button from 'primevue/button'
import Password from 'primevue/password'; import Password from 'primevue/password'
import axios from 'axios' import axios from 'axios'
import { useUserData, login, logout, resetPassword } from './user'
const userData = ref({ const emit = defineEmits<{
id: -1, (e: 'onAction')
username: "NONE", }>()
authenticated: false,
is_admin: false
})
onMounted(() => { const userData = useUserData()
axios.get('/api/auth/user').then((resp) => {
userData.value = resp.data
})
})
function onFormSubmit(form: any) { function onFormSubmit(form: any) {
const username = form.values.username const username = form.values.username
const password = form.values.password const password = form.values.password
login(username, password)
axios.post('/api/auth/login', { username, password }).then((resp) => { emit('onAction')
userData.value = resp.data
})
} }
function onResetPassword(form: any) { function onResetPassword(form: any) {
const username = userData.value.username
const password = form.values.password const password = form.values.password
resetPassword(password)
axios.post(`/api/auth/user/${userData.value.id}`, { username, password }); emit('onAction')
} }
function logout() { function onLogout() {
axios.post('/api/auth/logout').then((resp) => { logout()
userData.value = resp.data emit('onAction')
})
} }
</script> </script>

View File

@@ -1,12 +1,12 @@
<template> <template>
<Toolbar> <Toolbar>
<template #end> <template #end>
<Button label="User" @click="visible = true"/> <Button :label="displayUsername()" @click="visible = true" />
</template> </template>
</Toolbar> </Toolbar>
<RouterView /> <RouterView />
<Dialog v-model:visible="visible" modal header="Log In" position="topright"> <Dialog v-model:visible="visible" modal header="Log In" position="topright">
<Login /> <Login @onAction="loginCallback" />
</Dialog> </Dialog>
</template> </template>
@@ -16,7 +16,22 @@ import Button from 'primevue/button'
import Dialog from 'primevue/dialog' import Dialog from 'primevue/dialog'
import Login from './Login.vue' import Login from './Login.vue'
import { useUserData } from './user'
import { ref } from 'vue' import { ref } from 'vue'
const userRef = useUserData()
const visible = ref(false) const visible = ref(false)
function displayUsername(): string {
const authenticated = userRef.value.authenticated
if (authenticated) {
return userRef.value.username
}
return 'Login'
}
function loginCallback(): void {
visible.value = false;
}
</script> </script>

View File

@@ -24,7 +24,10 @@
<template #subtitle> <template #subtitle>
<div> <div>
Status: Status:
<Chip :class="table.active ? 'active' : 'inactive'" :label="table.active ? 'active' : 'inactive'" /> <Chip
:class="table.active ? 'active' : 'inactive'"
:label="table.active ? 'active' : 'inactive'"
/>
</div> </div>
</template> </template>
<template #content> <template #content>
@@ -45,8 +48,20 @@
<template #title> Edit Details </template> <template #title> Edit Details </template>
<template #content> <template #content>
<div class="flex flex-column"> <div class="flex flex-column">
<InputText class="button-box" name="tableName" type="text" placeholder="Table Name" v-model="tableName" /> <InputText
<InputText class="button-box" name="tableLink" type="text" placeholder="Table Link" v-model="tableLink" /> class="button-box"
name="tableName"
type="text"
placeholder="Table Name"
v-model="tableName"
/>
<InputText
class="button-box"
name="tableLink"
type="text"
placeholder="Table Link"
v-model="tableLink"
/>
<Dropdown class="button-box" v-model="version" :options="availableVersion"></Dropdown> <Dropdown class="button-box" v-model="version" :options="availableVersion"></Dropdown>
</div> </div>
</template> </template>
@@ -77,7 +92,8 @@
margin: 5px; margin: 5px;
} }
.active-foundry-stats {} .active-foundry-stats {
}
.inactive-foundry-stats { .inactive-foundry-stats {
display: none; display: none;
@@ -114,9 +130,9 @@ const foundryStatus = reactive({
}) })
const currentUser = ref({ const currentUser = ref({
username: "None", username: 'None',
authenticated: "False", authenticated: 'False',
is_admin: "False" is_admin: 'False',
}) })
const version = ref(12) const version = ref(12)
@@ -135,7 +151,7 @@ onMounted(() => {
loadTable() loadTable()
axios.get('/api/auth/user').then((resp) => currentUser.value = resp.data) axios.get('/api/auth/user').then((resp) => (currentUser.value = resp.data))
hostname.value = location.host hostname.value = location.host
}) })
@@ -225,7 +241,7 @@ function stopTable() {
}) })
} }
function deleteTable() { } function deleteTable() {}
function isTableLoading() { function isTableLoading() {
return table.active && !foundryStatus.running return table.active && !foundryStatus.running

View File

@@ -14,7 +14,7 @@ const routes = [
{ name: 'home', path: '/', component: TableManager }, { name: 'home', path: '/', component: TableManager },
{ name: 'edit', path: '/edit/:id', component: TableEditor }, { name: 'edit', path: '/edit/:id', component: TableEditor },
{ name: 'new', path: '/new', component: CreateTable }, { name: 'new', path: '/new', component: CreateTable },
{ name: 'login', path: '/login', component: Login} { name: 'login', path: '/login', component: Login },
] ]
const router = createRouter({ const router = createRouter({

View File

@@ -1,5 +1,5 @@
import axios from 'axios' import axios from 'axios'
import { ref, computed, onMounted } from "vue" import { ref, computed, onMounted } from 'vue'
export interface User { export interface User {
username: string username: string
@@ -7,7 +7,7 @@ export interface User {
authenticated: boolean authenticated: boolean
} }
const $current_user = ref({ username: "username", is_admin: false, authenticated: false }) const $current_user = ref({ username: 'username', is_admin: false, authenticated: false })
export const current_user = computed((): User => { export const current_user = computed((): User => {
return $current_user.value return $current_user.value
}) })
@@ -18,13 +18,15 @@ function loadCurrentUser(): void {
}) })
} }
export function userUserData(): void { export function useUserData(): void {
onMounted(() => { onMounted(() => {
loadCurrentUser() loadCurrentUser()
}) })
return $current_user
} }
export function loginUser(username: string, password: string): void { export function login(username: string, password: string): void {
axios.post('/api/auth/login', { username, password }).then((resp) => { axios.post('/api/auth/login', { username, password }).then((resp) => {
$current_user.value = resp.data $current_user.value = resp.data
}) })
@@ -37,3 +39,10 @@ export function logout(): void {
}) })
} }
} }
export function resetPassword(password: string): void {
axios.post(`/api/auth/user/${$current_user.value.id}`, {
username: $current_user.value.username,
password,
})
}