Skip to content

Commit

Permalink
Revert "Revert "alterações 29/05""
Browse files Browse the repository at this point in the history
This reverts commit 06acda7.
  • Loading branch information
Kauannery committed Jun 3, 2024
1 parent 06acda7 commit 9ddbccd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 124 deletions.
7 changes: 5 additions & 2 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
path('v1/cors/test', view_cors.cors_test, name='cors_test'),
]

50 changes: 0 additions & 50 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
72 changes: 0 additions & 72 deletions frontend/src/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import axios from 'axios';

// const apiCalendar = new ApiCalendar(config);
export default function Login() {

<GoogleOAuthProvider clientId="470653035644-rkr19rof1eclp7f7gmd4044jt110hf9g.apps.googleusercontent.com"></GoogleOAuthProvider>;
Expand Down Expand Up @@ -49,77 +48,6 @@ export default function Login() {
onSuccess={handleLogin}
/>
</GoogleOAuthProvider>)
=======
// // 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 (<button onClick={() => teste()}> oiasodiaosio</button>)
return (<button onClick={() => login()}> Fazer login</button>)

// return(<GoogleLogin onSuccess={credentialResponse => {
// console.log(credentialResponse);
// }}
// onError={() => {
// console.log('Login Failed');
// }}
// />)
>>>>>>> da576de (tests code)
}


Expand Down

0 comments on commit 9ddbccd

Please sign in to comment.