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 { 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) const Login = ({ getCodeOnly }: any) => { const location = useLocation() 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 {} }) if (loggedIn) { appContext.setLoggedIn?.(loggedIn) appContext.setUsername?.(user.username) appContext.setDisplayName?.(user.displayName) } } if (displayCode) { return (

Authorization Code

{displayCode}
) } return ( :not(style)': { m: 1, width: '25ch' } }} >

Welcome to SASjs Server!

{getCodeOnly && (

Provide credentials to get authorization code.

)}
setUsername(e.target.value)} required /> setPassword(e.target.value)} required /> {errorMessage && {errorMessage}}
) } Login.propTypes = { getCodeOnly: PropTypes.bool } export default Login