auth work

This commit is contained in:
Romulus21
2024-07-17 07:09:04 +02:00
parent 6be48846b0
commit a34eadb51f
13 changed files with 1125 additions and 1042 deletions

View File

@@ -30,8 +30,9 @@ export const AuthUserProvider = ({children}: PropsWithChildren) => {
} catch (error) {
// @ts-expect-error 401 error to redirect
if (error.response.status === 401) {
console.info('no user login')
if (!['/connexion', '/sinscrire'].includes(window.location.pathname)) {
const page = window.location.pathname.split('/')[1]
if (!['connexion', 'sinscrire', 'reset'].includes(page)) {
console.info('no user login')
window.location.href = '/connexion'
}
}

View File

@@ -14,9 +14,13 @@ const useAxiosTools = (isLoading = false) => {
const errorCatch = (error: Error|AxiosError|unknown) => {
if (axios.isAxiosError(error)) {
(error.response?.status === 422)
? displayFormErrors(error)
: setError(error.response?.data.message || error.message)
if (error.response?.status === 422) {
displayFormErrors(error)
} else if (error.response?.status === 401) {
console.log('not authorise')
} else {
setError(error.response?.data.message || error.message)
}
} else if (error instanceof Error) {
setError(error.message)
}

View File

@@ -14,7 +14,7 @@ interface TrackerProps {
export const TrackerProvider = ({children}: PropsWithChildren) => {
const [currentTimeTracker, setCurrentTimeTracker] = useState<timeTracker|null>(null)
const {axiosGet, axiosPost, axiosDelete} = useAxiosTools()
const {axiosGet, axiosPost, axiosDelete, errorCatch} = useAxiosTools()
useEffect(() => {
fetchCurrentTimeTracker()
@@ -25,7 +25,7 @@ export const TrackerProvider = ({children}: PropsWithChildren) => {
const res = await axiosGet(`/api/time-trackers/user`)
setCurrentTimeTracker(res.data)
} catch (error) {
console.error(error)
errorCatch(error)
}
}

View File

@@ -34,19 +34,24 @@ const Login = () => {
</h1>
{errorLabel()}
<Field type="email"
name="email"
placeholder="Email"
value={email}
onChange={event => setEmail(event.target.value)}
autoFocus>Email</Field>
<Field type="password"
name="password"
placeholder="******"
value={password}
onChange={event => setPassword(event.target.value)}>Mot de passe</Field>
<button type="submit" className="btn-primary mt-5 block w-full text-lg">Valider</button>
<Link to="/mot-de-passe-oubliee" className="mt-2 inline-block">Mot de passe oublié ?</Link>
<Field type="email"
name="email"
placeholder="Email"
value={email}
onChange={event => setEmail(event.target.value)}
autoFocus>Email</Field>
<Field type="password"
name="password"
placeholder="******"
value={password}
onChange={event => setPassword(event.target.value)}>Mot de passe</Field>
<Field type="hidden"
value={1}
name="form_info" onChange={() => setPassword('')}/>
<button type="submit" className="btn-primary mt-5 block w-full text-lg">Valider</button>
<Link to="/mot-de-passe-oubliee" className="mt-2 inline-block">Mot de passe oublié ?</Link>
</form>
</Card>
</div>

View File

@@ -3,12 +3,10 @@ import Field from "../../components/Field"
import axios from "axios"
import {useNavigate} from "react-router-dom"
import Card from "../../components/Card"
import useAuthUser from "../../hooks/AuthUser"
const Register = () => {
const navigate = useNavigate()
const {setAuthUser} = useAuthUser()
const [name, setName] = useState('')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
@@ -17,9 +15,8 @@ const Register = () => {
event.preventDefault()
try {
await axios.get('/sanctum/csrf-cookie')
const res = await axios.post('/api/register', {name, email, password})
setAuthUser(res.data)
navigate('/')
await axios.post('/api/register', {name, email, password})
navigate('/connexion')
} catch (e) {
console.error(e)
}

View File

@@ -10,6 +10,8 @@ import Register from "./Auth/Register"
import ToDoShow from "./ToDos/ToDoShow"
import TimeTrackersIndex from "./TimeTrackersIndex"
import {env} from "../utilities/env"
import Reset from "./Auth/Reset"
import ForgotPassword from "./Auth/ForgotPassword"
const Router = () => {
@@ -26,6 +28,8 @@ const Router = () => {
<Route path="/connexion" element={<Login />} />
{env.VITE_REGISTER_DISABLED === 'false' && <Route path="/sinscrire" element={<Register />} />}
<Route path="/todos/:id" element={<ToDoShow />} />
<Route path="/mot-de-passe-oubliee" element={<ForgotPassword />} />
<Route path="/reset/:token" element={<Reset />} />
<Route path="/times" element={<TimeTrackersIndex />} />
</Routes>
</Suspense>