Pārlūkot izejas kodu

add google auth api

tomoya 2 gadi atpakaļ
vecāks
revīzija
2c10d5b739

+ 2 - 2
backend/app/app/api/api_v1/endpoints/login.py

@@ -51,8 +51,8 @@ def login_access_token(
     """
     OAuth2 compatible token login, get an access token for future requests
     """
-    user = crud.user.authenticate(
-        db, email=form_data.username, password=form_data.password
+    user = crud.user.google_auth(
+        db, email=form_data.username
     )
     if not user:
         user_in = schemas.UserCreate(email=form_data.username, password=form_data.password)

+ 7 - 1
backend/app/app/crud/crud_user.py

@@ -44,7 +44,13 @@ class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
         if not verify_password(password, user.hashed_password):
             return None
         return user
-
+    
+    def google_auth(self, db: Session, *, email: str) -> Optional[User]:
+        user = self.get_by_email(db, email=email)
+        if not user:
+            return None
+        return user
+    
     def is_active(self, user: User) -> bool:
         return user.is_active
 

+ 6 - 2
frontend/src/api.ts

@@ -68,7 +68,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 googleLogInGetToken(username: string, password: string){
+    const params = new URLSearchParams();
+    params.append("username", username);
+    params.append("password", password);
+
+    return axios.post(`${apiUrl}/api/v1/login/access-token`, params);
   },
 };

+ 17 - 7
frontend/src/stores/main.ts

@@ -266,14 +266,24 @@ export const useMainStore = defineStore("MainStoreId", {
         await mainStore.checkApiError(error);
       }
     },
-    async googleLogin(access_token:string) {
-      try{
-        const response = await api.googleLogin(access_token)
-        if (response) {
-          console.log(response)
+    async googleLogin(username:string, password: string) {
+      try {
+        const response = await api.googleLogInGetToken(username, password);
+        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();
       }
     }
   }