Skip to content

Commit

Permalink
feat: pro connect url
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeSlain committed Nov 12, 2024
1 parent e009bf6 commit 55cd07d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 101 deletions.
167 changes: 83 additions & 84 deletions src/pages/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,89 @@
import { ButtonsGroup } from '@codegouvfr/react-dsfr/ButtonsGroup'
import { initButtonsLogin } from '@constants/connexion'
import { loginFields } from '@constants/inputFields'
import { useAuth } from 'react-oidc-context'
import {
type Dispatch,
type SetStateAction,
useCallback,
useEffect,
useState,
} from 'react'
import { LoginFields } from '../components/Auth/LoginFields'
import { ButtonInformation } from '../components/Global/ButtonInformation'
import { useNavigate } from 'react-router-dom'
// import { ButtonsGroup } from '@codegouvfr/react-dsfr/ButtonsGroup'
// import { initButtonsLogin } from '@constants/connexion'
// import { loginFields } from '@constants/inputFields'
// import {
// type Dispatch,
// type SetStateAction,
// useCallback,
// useEffect,
// useState,
// } from 'react'
// import { LoginFields } from '../components/Auth/LoginFields'
// import { ButtonInformation } from '../components/Global/ButtonInformation'
// import { useNavigate } from 'react-router-dom'

interface LoginProps {
authFailed: boolean
setAuthFailed: Dispatch<SetStateAction<boolean>>
}
// interface LoginProps {
// authFailed: boolean
// setAuthFailed: Dispatch<SetStateAction<boolean>>
// }

export function Login({ authFailed, setAuthFailed }: LoginProps) {
const [isDisable, setIsDisable] = useState(true)
const [password, setPassword] = useState('')
const [id, setId] = useState('')
const { login, isAuthenticated } = useAuth()
const navigate = useNavigate()
useEffect(() => {
if (isAuthenticated) {
navigate('/meeting', { replace: true })
}
}, [isAuthenticated, navigate])
useEffect(() => {
checkIfCompletedFields()
}, [password, id])
// export function Login({ authFailed, setAuthFailed }: LoginProps) {
// const [isDisable, setIsDisable] = useState(true)
// const [password, setPassword] = useState('')
// const [id, setId] = useState('')
// // { login, isAuthenticated } = useAuth()
// const navigate = useNavigate()
// // useEffect(() => {
// // if (isAuthenticated) {
// // navigate('/meeting', { replace: true })
// // }
// // }, [isAuthenticated, navigate])
// useEffect(() => {
// checkIfCompletedFields()
// }, [password, id])

const checkIfCompletedFields = useCallback(() => {
setIsDisable(!(password.length && id.length))
}, [password, id])
// const checkIfCompletedFields = useCallback(() => {
// setIsDisable(!(password.length && id.length))
// }, [password, id])

const handleChange = (e) => {
e.preventDefault()
if (e.target.name === 'username') {
setId(e.target.value)
}
if (e.target.name === 'password') {
setPassword(e.target.value)
}
}
// const handleChange = (e) => {
// e.preventDefault()
// if (e.target.name === 'username') {
// setId(e.target.value)
// }
// if (e.target.name === 'password') {
// setPassword(e.target.value)
// }
// }

const handleSubmit = async () => {
setAuthFailed(false)
try {
const success = await login(id, password)
if (!success) {
setAuthFailed(true)
}
} catch (error) {
setAuthFailed(true)
}
}
// const handleSubmit = async () => {
// setAuthFailed(false)
// // try {
// // const success = await login(id, password)
// // if (!success) {
// // setAuthFailed(true)
// // }
// // } catch (error) {
// // setAuthFailed(true)
// // }
// // }

return (
<div className="fr-container fr-mb-8w">
<div className="fr-grid-row">
<div className="fr-col fr-col-md-6">
<h1 className="fr-text-title--blue-france fr-mt-5w fr-mb-2w">Se connecter</h1>
<p className="fr-mb-4w">
Ce service est à destination des France services participant à
l'expérimentation Albert France services.
</p>
<LoginFields
fields={loginFields}
handleChange={handleChange}
handleSubmit={handleSubmit}
selectedValue={undefined}
setSelectedValue={undefined}
matricule={undefined}
setMatricule={undefined}
/>
{authFailed && (
<ButtonInformation>
Nom d'utilisateur ou mot de passe invalide.
</ButtonInformation>
)}
<ButtonsGroup buttons={initButtonsLogin(handleSubmit, isDisable)} />
</div>
</div>
</div>
)
}
// return (
// <div className="fr-container fr-mb-8w">
// <div className="fr-grid-row">
// <div className="fr-col fr-col-md-6">
// <h1 className="fr-text-title--blue-france fr-mt-5w fr-mb-2w">Se connecter</h1>
// <p className="fr-mb-4w">
// Ce service est à destination des France services participant à
// l'expérimentation Albert France services.
// </p>
// <LoginFields
// fields={loginFields}
// handleChange={handleChange}
// handleSubmit={handleSubmit}
// selectedValue={undefined}
// setSelectedValue={undefined}
// matricule={undefined}
// setMatricule={undefined}
// />
// {authFailed && (
// <ButtonInformation>
// Nom d'utilisateur ou mot de passe invalide.
// </ButtonInformation>
// )}
// <ButtonsGroup buttons={initButtonsLogin(handleSubmit, isDisable)} />
// </div>
// </div>
// </div>
// )
// }
8 changes: 4 additions & 4 deletions src/utils/context/authContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
const pathname = useLocation().pathname
const navigate = useNavigate()

const loginPage = import.meta.env.VITE_PRO_CONNECT_LOGIN_URL // A mettre à jour vers la vraie page de login
console.log('uisAuthenticated', isAuthenticated)
const loginPage = `${import.meta.env.VITE_API_URL}/login`

useEffect(() => {
checkUserInfo()
}, [])

const checkUserInfo = async (): Promise<void> => {
try {
const response = await fetch('http://localhost:8000/api/v2/userinfo', {
const response = await fetch(`${import.meta.env.VITE_API_URL}/userinfo`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -61,7 +61,7 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>

const logout = () => {
removeSession()
window.location.replace('http://localhost:8000/api/v2/prepare-logout')
window.location.replace(`${import.meta.env.VITE_API_URL}/prepare-logout`)
}

useEffect(() => {
Expand Down
12 changes: 0 additions & 12 deletions src/utils/getUser.ts

This file was deleted.

2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default () => {
},
host: true,
strictPort: true,
port: 4172,
port: 4173,
},
base: '/',
})
Expand Down

0 comments on commit 55cd07d

Please sign in to comment.