From 06acda7a9d2d7e61edef6b1082bcdfdb3c69056c Mon Sep 17 00:00:00 2001 From: kauan nery <113049473+Kauannery@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:49:33 -0300 Subject: [PATCH] =?UTF-8?q?Revert=20"altera=C3=A7=C3=B5es=2029/05"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 6e39335bca301b16e0ad24ff96f9994980df9909. --- api/urls.py | 7 ++-- api/views.py | 50 +++++++++++++++++++++++++++++ frontend/src/Login.jsx | 72 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/api/urls.py b/api/urls.py index cd1757a..6a33b4f 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,10 +1,7 @@ from django.urls import path -from api import views, view_cors +from api import views urlpatterns = [ path('v1/calendar/init/', views.calendar_init_view, name='calendar_init'), - # path('v1/calendar/flow/', views.calendar_flow_view, name='calendar_flow'), path('v1/calendar/token/', views.calendar_token, name='calendar_token'), - path('v1/cors/test', view_cors.cors_test, name='cors_test'), -] - +] \ No newline at end of file diff --git a/api/views.py b/api/views.py index 143c2b1..fe6bfd9 100644 --- a/api/views.py +++ b/api/views.py @@ -63,5 +63,55 @@ def calendar_token(request): except Exception as e: print(f"Error: {e}") return JsonResponse({"error": str(e)}) +======= + flow = Flow.from_client_config(config,scopes=SCOPES,redirect_uri="http://127.0.0.1:3000") + code = request.data['code'] + print('code', code) + # flow.fetch_token(code) + # 4/0AdLIrYdjDBOqa7mxm9bGUUdXo_lyOu1YgKIiDh6_UhBCmfZNI_JMkRDLvg33YTHPSaWe2A + try: + credentials = flow.fetch_token(code=code) + print('crendentials:', credentials) + except Exception as e: + print(f"Error: {e}") + return JsonResponse({"error": str(e)}) + + userinfo_service = googleapiclient.discovery.build("oauth2", "v2", credentials=credentials) + user_info = userinfo_service.userinfo().get().execute() + + email = user_info.get("email") + User = get_user_model() + user, created = User.objects.get_or_create(username=email, email=email) + if created: + user.set_unusable_password() + user.save() + + if not CredentialsService.get_for(user): + saved_credentials = CredentialsService.create_for(user, credentials) + else: + saved_credentials = CredentialsService.update_for(user, credentials) + if not saved_credentials: + return redirect("api/v1/calendar/init") + + saved_credentials.user = user + saved_credentials.save(update_fields=["user"]) + + authenticated_user = authenticate(request, username=email) + if authenticated_user: + login(request, authenticated_user) + + try: + service = googleapiclient.discovery.build(API_SERVICE_NAME, API_VERSION, credentials=credentials) + + if not user.calendar_id: + calendar = {"summary": "BaheaCal", "timeZone": "America/Bahia"} + created_calendar = service.calendars().insert(body=calendar).execute() + user.calendar_id = created_calendar["id"] + user.save(update_fields=["calendar_id"]) + + service.events().list(calendarId=user.calendar_id).execute() + except Exception as e: + return JsonResponse({"error": str(e)}) +>>>>>>> da576de (tests code) else: return JsonResponse({"sucess": True}) \ No newline at end of file diff --git a/frontend/src/Login.jsx b/frontend/src/Login.jsx index cd63d87..d8c2ade 100644 --- a/frontend/src/Login.jsx +++ b/frontend/src/Login.jsx @@ -1,6 +1,7 @@ import axios from 'axios'; +// const apiCalendar = new ApiCalendar(config); export default function Login() { ; @@ -48,6 +49,77 @@ export default function Login() { onSuccess={handleLogin} /> ) +======= + // // await axios(config) + // const response = fetch('http://localhost:8000/api/v1/calendar/token/', { + // method: 'POST', + // headers: { + // 'Content-Type': 'application/json', + // 'Allow-Control-Allow-Origin': '*' + // }, + // body: data, + // }); + // console.log('response body =', response.body) + + // const hasAccess = hasGrantedAllScopesGoogle( + // response.body, + // "https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.app.created", + // "https://www.googleapis.com/auth/userinfo.email", + // "https://www.googleapis.com/auth/userinfo.profile", + // "openid" + // ); + // console.log('hasAccess = ', hasAccess); + // } + + const handleLogin = async(credentialResponse) => { + // console.log('credential Response', credentialResponse) + const response = fetch('http://localhost:8000/api/v1/calendar/token/', { + method: 'POST', + // mode: 'no-cors', + headers: { + // 'X-BLA' : 'bla!', + 'Access-Control-Allow-Headers' : '*', + 'Content-Type': 'application/json', + 'Allow-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': '*' + }, + body: credentialResponse, + }); + + // console.log('response body =', response.body) + console.log(credentialResponse) + } + // const teste = async() => { + // const resposta = fetch('http://localhost:8000/api/v1/calendar/token/', { + // method: 'GET', + // headers: { + // 'Content-Type': 'application/json', + // 'Access-Control-Allow-Headers': 'X-Requested-With', + // 'Allow-Control-Allow-Origin': '*' + // }, + // }); + // console.log('resposta = ',resposta); + // } + + const login = useGoogleLogin({ + scope: "https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.app.created https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid", + flow: 'auth-code', + access_type: 'offline', + prompt: 'consent', + onSuccess: handleLogin, + }); + + // return () + return () + + // return( { + // console.log(credentialResponse); + // }} + // onError={() => { + // console.log('Login Failed'); + // }} + // />) +>>>>>>> da576de (tests code) }