Skip to content

Commit

Permalink
show login error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Janderson Souza Matias authored and Janderson Souza Matias committed Jun 17, 2024
1 parent 38ad02d commit 34c592a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/contexts/UserContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IRegion, IUser } from '../../types';
import { toast } from 'react-toastify';
import UserService from '@/services/user';
import { useTranslation } from 'react-i18next';
import { throws } from 'assert';

export type UserContextProps = {
login: (prop: { email: string; code: string }) => Promise<void>;
Expand Down Expand Up @@ -51,10 +52,15 @@ const UserContextProvider = ({ children }: Props) => {
}, [user]);

const handleLogin = async (props: { email: string; code: string }) => {
const response = await AuthService.login(props);
setUser(response.data);
StorageService.setUser(response.data);
StorageService.setAccessToken(response.headers.token || '');
try {
const response = await AuthService.login(props);
setUser(response.data);
StorageService.setUser(response.data);
StorageService.setAccessToken(response.headers.token || '');
} catch (err: any) {
console.log({ err });
throw err;
}
};

const handleUpdateUser = async (userToUpdate: Partial<IUser & { currentPassword?: string }>) => {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
Text,
VStack,
} from '@chakra-ui/react';
import { AxiosError } from 'axios';
import React, { useContext, useState } from 'react';
import { useForm, SubmitHandler, Controller } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { CloseButton } from 'react-toastify/dist/components';

const defaultValues = {
email: '',
Expand Down Expand Up @@ -46,7 +46,9 @@ const Login: React.FC = () => {
setShowOTP(true);
}
} catch (err: any) {
if (showOTP) {
if ((err as AxiosError).response?.status === 400) {
setError((err as AxiosError<any>).response?.data?.error || 'Error');
} else if (showOTP) {
setError(t('Login.invalid-code') || '');
} else {
setError(t('Login.invalid-email') || '');
Expand Down
12 changes: 8 additions & 4 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import StorageService from './storage/storage.service';

const config = {
Expand All @@ -15,17 +15,21 @@ _axios.interceptors.request.use(
}
return config;
},
(error) => error,
(error) => {
console.log({ error });
return error;
},
);

_axios.interceptors.response.use(
(response) => response,
(error) => {
(error: AxiosError) => {
if (error.response?.status === 401) {
StorageService.cleanStorage();
location.reload();
}
throw new Error(error);

throw error;
},
);

Expand Down

0 comments on commit 34c592a

Please sign in to comment.