adding auth store
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import { createApp } from "vue";
|
import { createApp } from "vue";
|
||||||
|
import { createPinia } from "pinia";
|
||||||
import App from "./App.vue";
|
import App from "./App.vue";
|
||||||
import router from "./router";
|
import router from "./router";
|
||||||
|
|
||||||
const app = createApp(App);
|
const app = createApp(App);
|
||||||
|
|
||||||
app.use(router);
|
app.use(router);
|
||||||
|
app.use(createPinia());
|
||||||
|
|
||||||
app.mount("#app");
|
app.mount("#app");
|
||||||
|
|||||||
40
nginx/vue/src/stores/auth.js
Normal file
40
nginx/vue/src/stores/auth.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { defineStore } from "pinia";
|
||||||
|
import { computed, ref } from "vue";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
export const useAuthStore = defineStore("auth", () => {
|
||||||
|
const user = ref({});
|
||||||
|
|
||||||
|
function logOut() {
|
||||||
|
user.value = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function logIn(username, password) {
|
||||||
|
try {
|
||||||
|
const res = await axios.post("/api/auth/login", {
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshToken() {
|
||||||
|
try {
|
||||||
|
const res = await axios.post("/api/auth/refresh");
|
||||||
|
user.value = res.data;
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkToken() {
|
||||||
|
try {
|
||||||
|
const res = await axios.get("/api/auth/check");
|
||||||
|
user.value = res.data;
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user