Move login to service
This commit is contained in:
parent
f59c3f6bda
commit
157f4c615c
1
.eslintcache
Normal file
1
.eslintcache
Normal file
File diff suppressed because one or more lines are too long
36
src/services/authService.ts
Normal file
36
src/services/authService.ts
Normal 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 };
|
@ -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,
|
||||
},
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user