From b1f1b34924976c05f434a7fd7d23fc259a363947 Mon Sep 17 00:00:00 2001 From: acidumirae Date: Sun, 9 Jul 2023 07:58:31 +0800 Subject: [PATCH] API calls for authstart and authlogin; Call authlogin with code and redirect URI to do the token exchange on the backend --- .../AuthPages/AuthRedirect/Redirect.jsx | 37 +++++++++++++++++-- src/services/UsersService.js | 13 ++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/components/AuthPages/AuthRedirect/Redirect.jsx b/src/components/AuthPages/AuthRedirect/Redirect.jsx index 22a5c84..177c1a9 100644 --- a/src/components/AuthPages/AuthRedirect/Redirect.jsx +++ b/src/components/AuthPages/AuthRedirect/Redirect.jsx @@ -1,9 +1,11 @@ -import React, {useState, useEffect} from 'react' -import { useLocation, useNavigate } from 'react-router-dom' +import React, { useEffect } from 'react'; +import { useLocation, useNavigate } from 'react-router-dom'; +import usersService from '../../../services/UsersService'; function Redirect() { const location = useLocation(); const navigate = useNavigate(); + const userApi = new usersService(); const queryParams = new URLSearchParams(location?.search); const codeResponse = queryParams.get("code"); @@ -13,7 +15,36 @@ function Redirect() { navigate('/login', {replace: true}) return } - console.log(codeResponse) + console.log(codeResponse); + /* + POST /token HTTP/1.1 + Host: oauth2.googleapis.com + Content-Type: application/x-www-form-urlencoded + + code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7& + client_id=your_client_id& + client_secret=your_client_secret& + redirect_uri=https%3A//oauth2.example.com/code& + grant_type=authorization_code + */ + var reqData = { + code: codeResponse, + redirect_uri: process.env.REACT_APP_GOOGLE_REDIRECT_URL, + }; + userApi + .authStart(reqData) + .then((res) => { + console.log(res.data); + if (res.status != 200 || res.internal_return < 0) { + return; + } + // "{"message":"Endpoint not found.","URI":"http:\/\/localhost:9083\/index.php\/en\/wrench\/api\/v1\/authstart"}[]" + const data = JSON.parse(res.data.replace('[]','')); + alert(data.message); + }) + .catch((error) => { + console.log(error); + }); },[]) return (
Redirecting ...
diff --git a/src/services/UsersService.js b/src/services/UsersService.js index 8a38d16..2656169 100644 --- a/src/services/UsersService.js +++ b/src/services/UsersService.js @@ -1,4 +1,3 @@ -import React from "react"; import Axios from "axios"; class usersService { @@ -26,10 +25,22 @@ class usersService { }; return this.postAuxEnd("/dashdata", postData); } + logInUser(reqData) { localStorage.setItem("session_token", ``); return this.postAuxEnd("/userlogin", reqData); } + + authStart(reqData) { + localStorage.setItem("session_token", ``); + return this.postAuxEnd("/authstart", reqData); + } + + authLogin(reqData) { + localStorage.setItem("session_token", ``); + return this.postAuxEnd("/authlogin", reqData); + } + marketJobData(reqData) { var postData = { uuid: localStorage.getItem("uuid"),