From 9ec11e0b2ab072482940d4250c6057f4a2472777 Mon Sep 17 00:00:00 2001 From: ravi Date: Fri, 15 Dec 2023 03:42:38 +0530 Subject: [PATCH] oauth2 enhanced --- src/components/NavBar.vue | 72 ++++++++------------------------------- 1 file changed, 14 insertions(+), 58 deletions(-) diff --git a/src/components/NavBar.vue b/src/components/NavBar.vue index b98298f..2eb8d9f 100644 --- a/src/components/NavBar.vue +++ b/src/components/NavBar.vue @@ -112,7 +112,7 @@ client_id: "1043737632690-hogp7qi303vimd5tflakfhvduodkfjjh.apps.googleusercontent.com", scope: "email profile openid", - redirect_uri: "http://localhost:4000/auth/callback", + redirect_uri: "https://sundarbans-website.vercel.app/", callback: response => { if (response.code) { this.sendCodeToBackend(response.code); @@ -123,86 +123,42 @@ }); }, async sendCodeToBackend(code) { - try { - const response = await axios.post( - "https://oauth2.googleapis.com/token", - { - code, - client_id: - "1043737632690-hogp7qi303vimd5tflakfhvduodkfjjh.apps.googleusercontent.com", - client_secret: "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", - redirect_uri: "postmessage", - grant_type: "authorization_code" - } - ); - - const accessToken = response.data.access_token; - - // Fetch user details using the access token - const userResponse = await axios.get( - "https://www.googleapis.com/oauth2/v3/userinfo", - { - headers: { - Authorization: `Bearer ${accessToken}` + const response = await axios.post('http://35.78.186.233/login', { + 'gcode' : code + }); + console.log('POST request response:', response.data); + if(response.data.success == false){ + alert(response.data.error) } - } - ); - - if (userResponse && userResponse.data) { - const userEmail = userResponse.data.email || ''; - - // Verify user email before approving login + else { + console.log(response.data) + if (response && response.data) { + const userEmail = response.data.user_data.email || ''; if (userEmail.endsWith('@ds.study.iitm.ac.in')) { - // Set the userDetails data property to the userResponse object - this.userDetails = userResponse.data; - - // Save user details to local storage + this.userDetails = response.data.user_data; localStorage.setItem('userDetails', JSON.stringify(this.userDetails)); - - // Send user details to the specified URL using a POST request - const response = await axios.post('http://35.78.186.233/login/', { - email: this.userDetails.email, - name: this.userDetails.name, - emailUrl: this.userDetails.picture // Assuming 'picture' is the URL - }); - console.log('POST request response:', response.data); - alert(response.data.error); } else { - // User email does not match the required domain, reject login console.error('Login rejected: Invalid email domain.'); alert("Please Login using Your IIT Madras Student Google Account."); } } else { - // Handle the case where userResponse or userResponse.data is undefined console.error("Failed to fetch user details."); } - } catch (error) { - console.error("Token exchange failed:", error.response.data); } }, signOut() { - // Clear local storage localStorage.removeItem('userDetails'); - - // Set userDetails to null this.userDetails = null; - - // Reload the page location.reload(); - } }, mounted() { - // Check if userDetails is present in local storage const storedUserDetails = localStorage.getItem('userDetails'); - if (storedUserDetails) { - // Parse the stored JSON string to get the userDetails object const userDetails = JSON.parse(storedUserDetails); - - // Update the component state this.userDetails = userDetails; } - }, + } +} };