From a634f07f2b4f55d6c766ae2c163b1c7290d514c4 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 3 Jul 2023 22:11:04 +0800 Subject: [PATCH] Added login functionality --- src/components/Api/Api.tsx | 8 +++--- src/components/ParseError/ParseError.tsx | 11 +++++++- src/routes/Login/Login.tsx | 33 ++++++++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/components/Api/Api.tsx b/src/components/Api/Api.tsx index 5056087..797499a 100644 --- a/src/components/Api/Api.tsx +++ b/src/components/Api/Api.tsx @@ -40,11 +40,13 @@ export function UserLogin(user: LoginParams) { .post("/api/v1/accounts/token/login/", user) .then(async (response) => { AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token)); - return true; + return [true]; }) .catch((error) => { - console.log("Login Failed: " + error); - return false; + console.log( + "Login Failed: " + error.response.status + " " + error.response.data + ); + return [false, error.response.data]; }); } diff --git a/src/components/ParseError/ParseError.tsx b/src/components/ParseError/ParseError.tsx index 6a39e77..defb5fd 100644 --- a/src/components/ParseError/ParseError.tsx +++ b/src/components/ParseError/ParseError.tsx @@ -2,8 +2,17 @@ export default function ParseError(text: string) { return text .replaceAll(/[{}()"]/g, " ") .replaceAll(/,/g, "\n") - .replaceAll(":", "") .replaceAll("[", "") .replaceAll("]", "") .replaceAll(".", ""); } + +export function ParseLoginError(text: string) { + return text + .replaceAll(/[{}()"]/g, " ") + .replaceAll(/,/g, "\n") + .replaceAll("[", "") + .replaceAll("]", "") + .replaceAll(".", "") + .replaceAll("non_field_errors", ""); +} diff --git a/src/routes/Login/Login.tsx b/src/routes/Login/Login.tsx index 0b5e4fe..4d4510e 100644 --- a/src/routes/Login/Login.tsx +++ b/src/routes/Login/Login.tsx @@ -19,13 +19,15 @@ import LoginIcon from "../../icons/LoginIcon/LoginIcon"; import Button from "../../components/Button/Button"; import { useNavigation } from "@react-navigation/native"; import { RootDrawerParamList } from "../../interfaces/Interfaces"; +import { UserLogin } from "../../components/Api/Api"; +import { ParseLoginError } from "../../components/ParseError/ParseError"; export default function Login() { const navigation = useNavigation(); // const dispatch = useDispatch(); // const creds = useSelector((state: RootState) => state.auth.creds); const [user, setUser] = useState({ - email: "", + username: "", password: "", error: "", }); @@ -49,13 +51,14 @@ export default function Login() { ): void => { - setUser({ ...user, email: e.nativeEvent.text }); + setUser({ ...user, username: e.nativeEvent.text }); }} /> @@ -71,8 +74,28 @@ export default function Login() { setUser({ ...user, password: e.nativeEvent.text }); }} /> + + {user.error} -