Kaynağa Gözat

mod google login

tomoya 2 yıl önce
ebeveyn
işleme
98234203e4
2 değiştirilmiş dosya ile 22 ekleme ve 8 silme
  1. 6 2
      frontend/src/api.ts
  2. 16 6
      frontend/src/stores/main.ts

+ 6 - 2
frontend/src/api.ts

@@ -102,7 +102,11 @@ export const api = {
   async getVideos(token: string) {
     return axios.get<Video[]>(`${apiUrl}/api/v1/videos/`, authHeaders(token));
   },
-  async googleLogin(access_token: string) {
-    return axios.post(`${apiUrl}/api/v1/login/google/access-token/${access_token}`,)
+  async googleLogin(username: string) {
+    const params = new URLSearchParams();
+    params.append("username", username);
+    params.append("password", "google");
+
+    return axios.post(`${apiUrl}/api/v1/login/access-token`, params);
   },
 };

+ 16 - 6
frontend/src/stores/main.ts

@@ -362,14 +362,24 @@ export const useMainStore = defineStore("MainStoreId", {
         await mainStore.checkApiError(error);
       }
     },
-    async googleLogin(access_token: string) {
+    async googleLogin(username: string) {
       try {
-        const response = await api.googleLogin(access_token)
-        if (response) {
-          console.log(response)
+        const response = await api.googleLogin(username);
+        const token: string = response.data.access_token;
+        if (token) {
+          saveLocalToken(token);
+          this.setToken(token);
+          this.setLoggedIn(true);
+          this.setLogInError(false);
+          await this.getUserProfile();
+          await this.routeLoggedIn();
+          this.addNotification({ content: i18n.global.t("loggedIn"), color: "success" });
+        } else {
+          await this.logOut();
         }
-      } catch (error) {
-        await this.checkApiError(error)
+      } catch (err) {
+        this.setLogInError(true);
+        await this.logOut();
       }
     }
   }