From 5888f04e08a32c6d2c7bcfcbc3a1d32425bff3b3 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Wed, 11 May 2022 21:01:59 +0500 Subject: [PATCH] fix(web): seperate container for auth code --- web/src/App.tsx | 6 +-- web/src/components/login.tsx | 57 ++---------------------- web/src/containers/AuthCode/index.tsx | 63 +++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 58 deletions(-) create mode 100644 web/src/containers/AuthCode/index.tsx diff --git a/web/src/App.tsx b/web/src/App.tsx index 1f13e31..92cb547 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -10,6 +10,7 @@ import Drive from './containers/Drive' import Studio from './containers/Studio' import { AppContext } from './context/appContext' +import AuthCode from './containers/AuthCode' function App() { const appContext = useContext(AppContext) @@ -20,9 +21,6 @@ function App() {
- - - @@ -47,7 +45,7 @@ function App() { - + diff --git a/web/src/components/login.tsx b/web/src/components/login.tsx index cca13e2..11451c9 100644 --- a/web/src/components/login.tsx +++ b/web/src/components/login.tsx @@ -1,56 +1,27 @@ import axios from 'axios' import React, { useState, useContext } from 'react' -import { useLocation } from 'react-router-dom' import PropTypes from 'prop-types' -import { CssBaseline, Box, TextField, Button, Typography } from '@mui/material' +import { CssBaseline, Box, TextField, Button } from '@mui/material' import { AppContext } from '../context/appContext' -const getAuthCode = async (credentials: any) => - axios.post('/SASjsApi/auth/authorize', credentials).then((res) => res.data) - const login = async (payload: { username: string; password: string }) => - axios.post('/login', payload).then((res) => res.data) + axios.post('/SASLogon/login', payload).then((res) => res.data) -const Login = ({ getCodeOnly }: any) => { - const location = useLocation() +const Login = () => { const appContext = useContext(AppContext) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [errorMessage, setErrorMessage] = useState('') - let error: boolean - const [displayCode, setDisplayCode] = useState(null) const handleSubmit = async (e: any) => { - error = false setErrorMessage('') e.preventDefault() - if (getCodeOnly) { - const params = new URLSearchParams(location.search) - const responseType = params.get('response_type') - if (responseType === 'code') { - const clientId = params.get('client_id') - - const { code } = await getAuthCode({ - clientId, - username, - password - }).catch((err: any) => { - error = true - setErrorMessage(err.response.data) - return {} - }) - if (!error) return setDisplayCode(code) - return - } - } - const { loggedIn, user } = await login({ username, password }).catch((err: any) => { - error = true setErrorMessage(err.response.data) return {} }) @@ -62,21 +33,6 @@ const Login = ({ getCodeOnly }: any) => { } } - if (displayCode) { - return ( - - -
-

Authorization Code

- - {displayCode} - - -
-
- ) - } - return ( {

Welcome to SASjs Server!

- {getCodeOnly && ( -

- Provide credentials to get authorization code. -

- )} -
- + axios.post('/SASLogon/authorize', credentials).then((res) => res.data) + +const AuthCode = () => { + const location = useLocation() + const [displayCode, setDisplayCode] = useState(null) + const [errorMessage, setErrorMessage] = useState('') + + useEffect(() => { + requestAuthCode() + }, []) + + const requestAuthCode = async () => { + setErrorMessage('') + + const params = new URLSearchParams(location.search) + + const responseType = params.get('response_type') + if (responseType !== 'code') + return setErrorMessage('response type is not support') + + const clientId = params.get('client_id') + if (!clientId) return setErrorMessage('clientId is not provided') + + setErrorMessage('Fetching auth code... ') + const { code } = await getAuthCode({ + clientId + }) + .then((res) => { + setErrorMessage('') + return res + }) + .catch((err: any) => { + setErrorMessage(err.response.data) + return { code: null } + }) + return setDisplayCode(code) + } + + return ( + + +
+

Authorization Code

+ {displayCode && ( + + {displayCode} + + )} + {errorMessage && {errorMessage}} + +
+
+ ) +} + +export default AuthCode