Move login to service

This commit is contained in:
SebClem 2022-05-27 20:13:41 +02:00
parent f59c3f6bda
commit 157f4c615c
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
4 changed files with 40 additions and 33 deletions

1
.eslintcache Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,36 @@
import { useUserStore } from "@/stores/user";
import axios from "axios";
async function login(code: string): Promise<boolean> {
const userStore = useUserStore();
const baseApi = import.meta.env.VITE_API_BASE_URL;
const baseUrl = window.location.origin;
try {
const response = await axios.post(baseApi + "auth/discord", {
redirectUri: baseUrl + "/oauth2/callback",
code: code,
});
userStore.token = response.data.token;
const payload = userStore.getTokenPayload;
userStore.userName = payload.sub as string;
userStore.discordId = payload.discord_id as string;
userStore.discriminator = payload.discriminator as string;
userStore.avatar = payload.avatar as string;
userStore.loginFail = false;
console.log("Loggin success !");
return true;
} catch (reason) {
console.log("Loggin fail !");
console.log(reason);
userStore.token = "";
userStore.userName = "";
userStore.discordId = "";
userStore.loginFail = true;
return false;
}
}
export { login };

View File

@ -31,38 +31,7 @@ export const useUserStore = defineStore("user", {
return jose.decodeJwt(state.token);
},
},
actions: {
async login(code: string) {
const baseApi = import.meta.env.VITE_API_BASE_URL;
const baseUrl = window.location.origin;
try {
const response = await axios.post(baseApi + "auth/discord", {
redirectUri: baseUrl + "/oauth2/callback",
code: code,
});
this.token = response.data.token;
const payload = this.getTokenPayload;
this.userName = payload.sub as string;
this.discordId = payload.discord_id as string;
this.discriminator = payload.discriminator as string;
this.avatar = payload.avatar as string;
this.loginFail = false;
console.log("Loggin success !");
return true;
} catch (reason) {
console.log("Loggin fail !");
console.log(reason);
this.token = "";
this.userName = "";
this.discordId = "";
this.loginFail = true;
return false;
}
},
},
actions: {},
persist: {
storage: cookiesStorage,
},

View File

@ -12,6 +12,7 @@
</template>
<script setup lang="ts">
import { login } from "@/services/authService";
import { useUserStore } from "@/stores/user";
import { useRoute, useRouter } from "vue-router";
@ -19,7 +20,7 @@ const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
if (route.query.code) {
userStore.login(route.query.code as string).then(() => {
login(route.query.code as string).then(() => {
router.push("/");
});
} else {