Skip to content

Commit

Permalink
feat: ✨ handle more condtions and redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
kiro6 committed Dec 18, 2023
1 parent a3fb468 commit a245f33
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions eduacademy_frontend/src/components/landing-page/LoginForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
import facebook from '../Assets/facebook.png'
import google from '../Assets/google.png'
import axios from 'axios';
import { useNavigate } from 'react-router-dom';


const initialState = {
Expand Down Expand Up @@ -52,6 +53,7 @@ const governorates = [


const Auth = () => {
const navigate = useNavigate();
const [form, setForm] = useState(initialState);
const [isSignup, setIsSignup] = useState(true);

Expand Down Expand Up @@ -80,18 +82,22 @@ const Auth = () => {
user_role: form.user_role,
birth_date: form.birth_date

}, {maxRedirects: 0 })
if (response) {
} , {maxRedirects: 0 })


if (response.status === 200) {
alert('Message: signup sent successfully');
//Route to Home or complete_profile
setIsSignup(false)
}
setIsSignup(false)


} catch (error) {
if (error.response) {
if (error.response.status === 400 ) {
alert(`Message: Your information is badly formatted OR there is user with the same username OR email address PLease try again`)
} else if (error.response.status === 403){
alert('Message: You Already Loged In');
} else {
alert('Message: Unkown error');
alert('Message: Unkown/Netwrok error');
}
}

Expand All @@ -102,18 +108,38 @@ const Auth = () => {
response = await axios.post('/api/login/', {
username: form.username,
password: form.password,
}, { maxRedirects: 0 });

if (response) {
alert('Message: Loged in successfully');
//Route to Home or complete_profile
} , );


if (response.status === 200) {
alert('Message: Logged in successfully');

let route = response.request.responseURL;
let user_role = response.data.user_role;

console.log('Route:', route);

if (route.includes("complete_profile")) {
// Use history to navigate with query parameters
navigate({
pathname: '/CompleteProfile',
search: `?role=${user_role}`, // Pass user_role as a query parameter
});
}else{
navigate({
pathname: '/home',
search: `?role=${user_role}`, // Pass user_role as a query parameter
});
}
}

} catch (error) {
if (error.response) {
if (error.response.status === 404 ) {
alert('Message:' + error.response.data);
} else {
alert('Message: Unkown error');
} else if (error.response.status === 403) {
alert('Message: You Already Loged In');
}else{
alert('Message: Unkown/Netwrok error');
}
}

Expand Down

0 comments on commit a245f33

Please sign in to comment.