mirror of
https://github.com/creativetimofficial/vue-material-kit.git
synced 2025-05-24 22:04:21 +08:00
Работает аутентификация через жопу
This commit is contained in:
parent
bdcbdf818e
commit
4b9f81c484
@ -2,6 +2,7 @@
|
|||||||
import { onMounted } from "vue";
|
import { onMounted } from "vue";
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { computed } from "vue";
|
||||||
|
|
||||||
// example components
|
// example components
|
||||||
import DefaultNavbar from "@/examples/navbars/NavbarDefault.vue";
|
import DefaultNavbar from "@/examples/navbars/NavbarDefault.vue";
|
||||||
@ -14,6 +15,8 @@ const username = ref('');
|
|||||||
const password = ref('');
|
const password = ref('');
|
||||||
const errorMessage = ref('');
|
const errorMessage = ref('');
|
||||||
|
|
||||||
|
const isAuthenticated = computed(() => !!sessionStorage.getItem('access_token')); // Computed property to check if the user is authenticated
|
||||||
|
|
||||||
|
|
||||||
const login = async () => {
|
const login = async () => {
|
||||||
if (!username.value || !password.value) {
|
if (!username.value || !password.value) {
|
||||||
@ -27,9 +30,11 @@ const login = async () => {
|
|||||||
username: username.value,
|
username: username.value,
|
||||||
password: password.value,
|
password: password.value,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(url, body, { headers });
|
const response = await axios.post(url, body, { headers });
|
||||||
errorMessage.value = `Request:\nPOST ${url}\nHeaders: ${JSON.stringify(headers)}\nBody: ${JSON.stringify(body)}\n\nResponse:\nStatus: ${response.status}\nHeaders: ${JSON.stringify(response.headers)}\nBody: ${JSON.stringify(response.data)}`;
|
errorMessage.value = `Request:\nPOST ${url}\nHeaders: ${JSON.stringify(headers)}\nBody: ${JSON.stringify(body)}\n\nResponse:\nStatus: ${response.status}\nHeaders: ${JSON.stringify(response.headers)}\nBody: ${JSON.stringify(response.data)}`;
|
||||||
|
sessionStorage.setItem('access_token', response.data.access); // Save the access token in sessionStorage (new line)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
// The request was made and the server responded with a status code that falls out of the range of 2xx
|
// The request was made and the server responded with a status code that falls out of the range of 2xx
|
||||||
@ -45,6 +50,10 @@ const login = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const logout = () => { // Method to logout the user by clearing the session storage (new function)
|
||||||
|
sessionStorage.removeItem('access_token');
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
setMaterialInput();
|
setMaterialInput();
|
||||||
@ -106,6 +115,15 @@ export default {
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form role="form" class="text-start">
|
<form role="form" class="text-start">
|
||||||
<div>
|
<div>
|
||||||
|
<div v-if="isAuthenticated">
|
||||||
|
<!-- This will only be displayed if the user is authenticated -->
|
||||||
|
<p>Опять Ты!</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-else>
|
||||||
|
<!-- This will be displayed if the user is not authenticated -->
|
||||||
|
<p>Я вас не знаю, идите нафиг</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<input v-model="username" type="text" placeholder="Имя пользователя" />
|
<input v-model="username" type="text" placeholder="Имя пользователя" />
|
||||||
@ -124,6 +142,7 @@ export default {
|
|||||||
>
|
>
|
||||||
Войти
|
Войти
|
||||||
</button>
|
</button>
|
||||||
|
<button @click="logout">Logout</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
@ -133,9 +152,6 @@ export default {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p class="mt-4 text-sm text-center">
|
<p class="mt-4 text-sm text-center">
|
||||||
Нет аккаунта?
|
Нет аккаунта?
|
||||||
<a
|
<a
|
||||||
|
Loading…
x
Reference in New Issue
Block a user