diff --git a/api/urls.py b/api/urls.py index 6a33b4f..cd1757a 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,7 +1,10 @@ from django.urls import path -from api import views +from api import views, view_cors 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'), -] \ No newline at end of file + path('v1/cors/test', view_cors.cors_test, name='cors_test'), +] + diff --git a/api/views.py b/api/views.py index fe6bfd9..143c2b1 100644 --- a/api/views.py +++ b/api/views.py @@ -63,55 +63,5 @@ 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 d8c2ade..cd63d87 100644 --- a/frontend/src/Login.jsx +++ b/frontend/src/Login.jsx @@ -1,7 +1,6 @@ import axios from 'axios'; -// const apiCalendar = new ApiCalendar(config); export default function Login() { ; @@ -49,77 +48,6 @@ 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) }