chore: implemented login version (#4)
Reviewed-on: #4 Co-authored-by: Janis <janis.e.20@gmx.de> Co-committed-by: Janis <janis.e.20@gmx.de>
This commit is contained in:
1028
package-lock.json
generated
1028
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -19,7 +19,12 @@
|
||||
"@quasar/extras": "^1.17.0",
|
||||
"quasar": "^2.18.6",
|
||||
"vue": "^3.5.25",
|
||||
"vue-router": "^4.6.3"
|
||||
"vue-router": "^4.6.3",
|
||||
"tsparticles": "~3.9.1",
|
||||
"@tsparticles/vue3": "~3.0.1",
|
||||
"axios": "^1.13.2",
|
||||
"vue-axios": "^3.5.2",
|
||||
"pinia": "^3.0.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@quasar/vite-plugin": "^1.10.0",
|
||||
|
||||
BIN
public/logo.png
Normal file
BIN
public/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
@@ -5,14 +5,13 @@ import HelloWorld from './components/HelloWorld.vue'
|
||||
|
||||
<template>
|
||||
<header>
|
||||
<img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />
|
||||
|
||||
<div class="wrapper">
|
||||
<HelloWorld msg="You did it!" />
|
||||
|
||||
<nav>
|
||||
<RouterLink to="/">Home</RouterLink>
|
||||
<RouterLink to="/about">About</RouterLink>
|
||||
<RouterLink to="/login">Login</RouterLink>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
21
src/main.ts
21
src/main.ts
@@ -3,9 +3,30 @@ import './assets/main.css'
|
||||
import { createApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
import Particles from "@tsparticles/vue3";
|
||||
import { loadFull } from "tsparticles";
|
||||
import { Quasar, Notify } from 'quasar'
|
||||
import '@quasar/extras/material-icons/material-icons.css'
|
||||
import 'quasar/dist/quasar.css'
|
||||
import { createPinia } from 'pinia'
|
||||
import axios from 'axios'
|
||||
import VueAxios from 'vue-axios'
|
||||
|
||||
const app = createApp(App)
|
||||
const pinia = createPinia()
|
||||
|
||||
app.use(router)
|
||||
app.use(Quasar, {
|
||||
plugins: {
|
||||
Notify
|
||||
},
|
||||
})
|
||||
app.use(pinia)
|
||||
app.use(VueAxios, axios)
|
||||
app.use(Particles, {
|
||||
init: async engine => {
|
||||
await loadFull(engine);
|
||||
},
|
||||
})
|
||||
|
||||
app.mount('#app')
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import HomeView from '../views/HomeView.vue'
|
||||
import LoginView from '../views/LoginView.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
@@ -9,6 +10,11 @@ const router = createRouter({
|
||||
name: 'home',
|
||||
component: HomeView,
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: LoginView,
|
||||
},
|
||||
{
|
||||
path: '/about',
|
||||
name: 'about',
|
||||
|
||||
60
src/stores/auth.ts
Normal file
60
src/stores/auth.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import {type credentials, type token, type user} from '@/types/authTypes'
|
||||
import { defineStore } from 'pinia'
|
||||
import {ref, computed, type Ref} from 'vue'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const user: Ref<user | null> = ref(null)
|
||||
const token = ref(localStorage.getItem('token') || null)
|
||||
|
||||
const isAuthenticated = computed(() => !!token.value)
|
||||
|
||||
async function login(credentials: credentials) {
|
||||
const response = await fakeLoginApi(credentials)
|
||||
|
||||
token.value = response.token
|
||||
user.value = response.user
|
||||
|
||||
localStorage.setItem('token', token.value)
|
||||
}
|
||||
|
||||
function setToken(newToken: string) {
|
||||
token.value = newToken
|
||||
localStorage.setItem('token', newToken)
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = null
|
||||
user.value = null
|
||||
localStorage.removeItem('token')
|
||||
}
|
||||
|
||||
async function fetchUser() {
|
||||
if (!token.value) return
|
||||
|
||||
const response = await fakeFetchUserApi(token.value)
|
||||
user.value = response.user
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
token,
|
||||
isAuthenticated,
|
||||
login,
|
||||
setToken,
|
||||
logout,
|
||||
fetchUser,
|
||||
}
|
||||
})
|
||||
|
||||
async function fakeLoginApi(credentials: credentials): Promise<token> {
|
||||
return {
|
||||
token: 'abc123',
|
||||
user: { id: 1, name: 'John Doe' }
|
||||
}
|
||||
}
|
||||
|
||||
async function fakeFetchUserApi(token: string): Promise<{ user: user }> {
|
||||
return {
|
||||
user: { id: 1, name: 'John Doe' }
|
||||
}
|
||||
}
|
||||
16
src/types/authTypes.ts
Normal file
16
src/types/authTypes.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
type token = {
|
||||
token: string
|
||||
user: user
|
||||
}
|
||||
type user = {
|
||||
id: number
|
||||
name: string
|
||||
}
|
||||
type credentials = {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export type { token, user, credentials }
|
||||
|
||||
222
src/views/LoginView.vue
Normal file
222
src/views/LoginView.vue
Normal file
@@ -0,0 +1,222 @@
|
||||
<template>
|
||||
<div class="login-box q-col-gutter-sm">
|
||||
<div class="text-h3 text-center row">
|
||||
<img src="/logo.png" alt="Logo" style="height: 60px; margin-right: 10px;" />
|
||||
</div>
|
||||
<q-form @submit.prevent="onSubmit"
|
||||
class="q-gutter-md row justify-center"
|
||||
style="width: 100%">
|
||||
|
||||
<div class="row q-col-gutter-sm" style="width: 100%">
|
||||
<q-input
|
||||
style="width: 100%"
|
||||
filled
|
||||
dark
|
||||
label-color="white"
|
||||
color="white"
|
||||
v-model="username"
|
||||
label="Username *"
|
||||
|
||||
:error="!!loginError"
|
||||
:error-message="loginError"
|
||||
|
||||
lazy-rules
|
||||
:rules="[ val => val && val.length > 0 || 'Username is required']"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="row q-col-gutter-sm" style="width: 100%">
|
||||
<q-input
|
||||
style="width: 100%"
|
||||
filled
|
||||
dark
|
||||
label-color="white"
|
||||
color="white"
|
||||
v-model="password"
|
||||
label="Password *"
|
||||
:error="!!loginError"
|
||||
:error-message="loginError"
|
||||
|
||||
lazy-rules
|
||||
:rules="[
|
||||
val => !!val || 'Password is required'
|
||||
]"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="row justify-center">
|
||||
<q-btn type="submit" label="Login" push color="dark" />
|
||||
</div>
|
||||
</q-form>
|
||||
</div>
|
||||
<vue-particles
|
||||
id="particles-js"
|
||||
:options='options'
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
|
||||
import { useQuasar } from 'quasar'
|
||||
import { ref } from 'vue'
|
||||
import axios from "axios";
|
||||
import {useAuthStore} from "@/stores/auth.ts";
|
||||
import router from "@/router";
|
||||
|
||||
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
||||
|
||||
const $q = useQuasar()
|
||||
|
||||
const username = ref(null)
|
||||
const password = ref(null)
|
||||
const inProgress = ref(false)
|
||||
const loginError = ref('')
|
||||
|
||||
const onSubmit = () => {
|
||||
if (inProgress.value) return
|
||||
inProgress.value = true
|
||||
loginError.value = ''
|
||||
axios.post(`${api}/login`, {username: username.value, password: password.value}).then(response => {
|
||||
const auth = useAuthStore()
|
||||
auth.setToken(response.data.token)
|
||||
router.push("/")
|
||||
}).catch(() => {
|
||||
loginError.value = 'Invalid username or password'
|
||||
}).finally(() =>
|
||||
inProgress.value = false
|
||||
)
|
||||
}
|
||||
|
||||
const options = {
|
||||
"particles": {
|
||||
"number": {
|
||||
"value": 80,
|
||||
"density": {
|
||||
"enable": true,
|
||||
"area": 800
|
||||
}
|
||||
},
|
||||
"color": {
|
||||
"value": "#ffffff"
|
||||
},
|
||||
"shape": {
|
||||
"type": "circle",
|
||||
"stroke": {
|
||||
"width": 0,
|
||||
"color": "#000000"
|
||||
},
|
||||
"polygon": {
|
||||
"sides": 5
|
||||
},
|
||||
"image": {
|
||||
"src": "img/github.svg",
|
||||
"width": 100,
|
||||
"height": 100
|
||||
}
|
||||
},
|
||||
"opacity": {
|
||||
"value": 0.5,
|
||||
"random": false,
|
||||
"animation": {
|
||||
"enable": false,
|
||||
"speed": 1,
|
||||
"minimumValue": 0.1,
|
||||
"sync": false
|
||||
}
|
||||
},
|
||||
"size": {
|
||||
"value": 3,
|
||||
"random": true,
|
||||
"animation": {
|
||||
"enable": false,
|
||||
"speed": 40,
|
||||
"minimumValue": 0.1,
|
||||
"sync": false
|
||||
}
|
||||
},
|
||||
"links": {
|
||||
"enable": true,
|
||||
"distance": 150,
|
||||
"color": "#ffffff",
|
||||
"opacity": 0.4,
|
||||
"width": 1
|
||||
},
|
||||
"move": {
|
||||
"enable": true,
|
||||
"speed": 1,
|
||||
"direction": "none",
|
||||
"random": false,
|
||||
"straight": false,
|
||||
"outModes": {
|
||||
"default": "out"
|
||||
},
|
||||
"attract": {
|
||||
"enable": false,
|
||||
"rotate": {
|
||||
"x": 600,
|
||||
"y": 1200
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"interactivity": {
|
||||
"detectsOn": "canvas",
|
||||
"events": {
|
||||
"onHover": {
|
||||
"enable": false,
|
||||
"mode": "repulse"
|
||||
},
|
||||
"onClick": {
|
||||
"enable": false,
|
||||
"mode": "push"
|
||||
},
|
||||
"resize": true
|
||||
},
|
||||
"modes": {
|
||||
"grab": {
|
||||
"distance": 400,
|
||||
"links": {
|
||||
"opacity": 1
|
||||
}
|
||||
},
|
||||
"bubble": {
|
||||
"distance": 400,
|
||||
"size": 40,
|
||||
"duration": 2,
|
||||
"opacity": 8,
|
||||
"speed": 3
|
||||
},
|
||||
"repulse": {
|
||||
"distance": 200,
|
||||
"duration": 0.4
|
||||
},
|
||||
"push": {
|
||||
"quantity": 4
|
||||
},
|
||||
"remove": {
|
||||
"quantity": 2
|
||||
}
|
||||
}
|
||||
},
|
||||
"detectRetina": true
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.login-box {
|
||||
position: fixed;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
display: flex;
|
||||
width: 100%;
|
||||
max-width: 420px;
|
||||
padding: 1rem;
|
||||
z-index: 2;
|
||||
background-color: var(--color-background);
|
||||
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
</style>
|
||||
@@ -2,15 +2,17 @@ import { fileURLToPath, URL } from 'node:url'
|
||||
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import { quasar, transformAssetUrls } from '@quasar/vite-plugin'
|
||||
import vueJsx from '@vitejs/plugin-vue-jsx'
|
||||
import vueDevTools from 'vite-plugin-vue-devtools'
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
vue(),
|
||||
vue({ template: { transformAssetUrls } }),
|
||||
vueJsx(),
|
||||
vueDevTools(),
|
||||
quasar(),
|
||||
],
|
||||
resolve: {
|
||||
alias: {
|
||||
|
||||
Reference in New Issue
Block a user