mirror of
https://github.com/sasjs/server.git
synced 2025-12-11 03:34:35 +00:00
fix(web): seperate container for auth code
This commit is contained in:
@@ -10,6 +10,7 @@ import Drive from './containers/Drive'
|
|||||||
import Studio from './containers/Studio'
|
import Studio from './containers/Studio'
|
||||||
|
|
||||||
import { AppContext } from './context/appContext'
|
import { AppContext } from './context/appContext'
|
||||||
|
import AuthCode from './containers/AuthCode'
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const appContext = useContext(AppContext)
|
const appContext = useContext(AppContext)
|
||||||
@@ -20,9 +21,6 @@ function App() {
|
|||||||
<HashRouter>
|
<HashRouter>
|
||||||
<Header />
|
<Header />
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route exact path="/SASjsLogon">
|
|
||||||
<Login getCodeOnly />
|
|
||||||
</Route>
|
|
||||||
<Route path="/">
|
<Route path="/">
|
||||||
<Login />
|
<Login />
|
||||||
</Route>
|
</Route>
|
||||||
@@ -47,7 +45,7 @@ function App() {
|
|||||||
<Studio />
|
<Studio />
|
||||||
</Route>
|
</Route>
|
||||||
<Route exact path="/SASjsLogon">
|
<Route exact path="/SASjsLogon">
|
||||||
<Login getCodeOnly />
|
<AuthCode />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</HashRouter>
|
</HashRouter>
|
||||||
|
|||||||
@@ -1,56 +1,27 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import React, { useState, useContext } from 'react'
|
import React, { useState, useContext } from 'react'
|
||||||
import { useLocation } from 'react-router-dom'
|
|
||||||
import PropTypes from 'prop-types'
|
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'
|
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 }) =>
|
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 Login = () => {
|
||||||
const location = useLocation()
|
|
||||||
const appContext = useContext(AppContext)
|
const appContext = useContext(AppContext)
|
||||||
const [username, setUsername] = useState('')
|
const [username, setUsername] = useState('')
|
||||||
const [password, setPassword] = useState('')
|
const [password, setPassword] = useState('')
|
||||||
const [errorMessage, setErrorMessage] = useState('')
|
const [errorMessage, setErrorMessage] = useState('')
|
||||||
let error: boolean
|
|
||||||
const [displayCode, setDisplayCode] = useState(null)
|
|
||||||
|
|
||||||
const handleSubmit = async (e: any) => {
|
const handleSubmit = async (e: any) => {
|
||||||
error = false
|
|
||||||
setErrorMessage('')
|
setErrorMessage('')
|
||||||
e.preventDefault()
|
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({
|
const { loggedIn, user } = await login({
|
||||||
username,
|
username,
|
||||||
password
|
password
|
||||||
}).catch((err: any) => {
|
}).catch((err: any) => {
|
||||||
error = true
|
|
||||||
setErrorMessage(err.response.data)
|
setErrorMessage(err.response.data)
|
||||||
return {}
|
return {}
|
||||||
})
|
})
|
||||||
@@ -62,21 +33,6 @@ const Login = ({ getCodeOnly }: any) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (displayCode) {
|
|
||||||
return (
|
|
||||||
<Box className="main">
|
|
||||||
<CssBaseline />
|
|
||||||
<br />
|
|
||||||
<h2>Authorization Code</h2>
|
|
||||||
<Typography m={2} p={3} style={{ overflowWrap: 'anywhere' }}>
|
|
||||||
{displayCode}
|
|
||||||
</Typography>
|
|
||||||
|
|
||||||
<br />
|
|
||||||
</Box>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
className="main"
|
className="main"
|
||||||
@@ -89,13 +45,6 @@ const Login = ({ getCodeOnly }: any) => {
|
|||||||
<CssBaseline />
|
<CssBaseline />
|
||||||
<br />
|
<br />
|
||||||
<h2 style={{ width: 'auto' }}>Welcome to SASjs Server!</h2>
|
<h2 style={{ width: 'auto' }}>Welcome to SASjs Server!</h2>
|
||||||
{getCodeOnly && (
|
|
||||||
<p style={{ width: 'auto' }}>
|
|
||||||
Provide credentials to get authorization code.
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
<br />
|
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
id="username"
|
id="username"
|
||||||
label="Username"
|
label="Username"
|
||||||
|
|||||||
63
web/src/containers/AuthCode/index.tsx
Normal file
63
web/src/containers/AuthCode/index.tsx
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
import React, { useEffect, useState } from 'react'
|
||||||
|
import { useLocation } from 'react-router-dom'
|
||||||
|
|
||||||
|
import { CssBaseline, Box, Typography } from '@mui/material'
|
||||||
|
|
||||||
|
const getAuthCode = async (credentials: any) =>
|
||||||
|
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 (
|
||||||
|
<Box className="main">
|
||||||
|
<CssBaseline />
|
||||||
|
<br />
|
||||||
|
<h2>Authorization Code</h2>
|
||||||
|
{displayCode && (
|
||||||
|
<Typography m={2} p={3} style={{ overflowWrap: 'anywhere' }}>
|
||||||
|
{displayCode}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
{errorMessage && <Typography>{errorMessage}</Typography>}
|
||||||
|
|
||||||
|
<br />
|
||||||
|
</Box>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default AuthCode
|
||||||
Reference in New Issue
Block a user