Skip to content

Commit

Permalink
Merge pull request #17 from ateliware/feat/call-login-endpoint
Browse files Browse the repository at this point in the history
Feature :: Integração com API && Login
  • Loading branch information
roderiano authored May 9, 2024
2 parents e5ebd6f + c4e2e72 commit 79bf4d4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,1,0" />
<link rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@48,400,1,0" />
<title>Blueprint App</title>
<title>Caronas SOS</title>
</head>

<body>
Expand Down
24 changes: 8 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { AuthProvider, useAuthContext } from './contexts/AuthProvider';
import {
BrowserRouter,
Navigate,
Outlet,
Route,
Routes,
useLocation,
} from 'react-router-dom';

import {
DefaultPage,
EmailSentPage,
HomePage,
LoginPage,
Expand All @@ -27,15 +27,15 @@ import { VehicleFormPage } from '@pages/Vehicle';

function Authenticated() {
const location = useLocation();
const { user, loading } = useAuthContext();
const { token, loading } = useAuthContext();

if (loading) {
return <>Loading, please wait...</>;
}

const isAuthenticated = !!user;
const isAuthenticated = !!token;
return isAuthenticated ? (
<DefaultPage />
<Outlet />
) : (
<Navigate to="/login" replace state={{ from: location }} />
);
Expand Down Expand Up @@ -73,24 +73,16 @@ function App() {
<Route path="email_sent" element={<EmailSentPage />} />
<Route path="about" element={<>Example about page</>} />

<Route path="vehicle/add" element={<VehicleFormPage />} />
<Route path="*" element={<NotFoundPage />} />
<Route path="home" element={<HomePage />} />
<Route path="ride_offer" element={<RideOfferPage />} />
</Route>

<Route path="" element={<Authenticated />}>
<Route path="/me" element={<MePage />} />
<Route path="/users" element={<UserPage />} />
<Route path="dashboard" element={<>Dashboard Content</>} />
<Route
path="protected"
element={<>Example protected page</>}
/>
<Route
path="protected2"
element={<>Example protected page 2</>}
/>

<Route path="home" element={<HomePage />} />
<Route path="ride_offer" element={<RideOfferPage />} />
<Route path="vehicle/add" element={<VehicleFormPage />} />
</Route>
</Routes>
</div>
Expand Down
19 changes: 10 additions & 9 deletions src/contexts/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { login, sendEmailRecover, setAuthorization } from '@services/api/auth';

export type TAuthContext = {
user: User | null;
token: string | null;
setUser: (user: User | null) => void;
loading: boolean;
isLoadingRequest?: boolean;
Expand All @@ -35,19 +36,17 @@ export function AuthProvider({ children }: PropsWithChildren) {
const [loading, setLoading] = useState(true);
const [isLoadingRequest, setIsLoadingRequest] = useState(false);

const handleLogin = async (email: string, password: string) => {
const handleLogin = async (cpf: string, password: string) => {
setIsLoadingRequest(true);
try {
const { data } = await login(email, password);
const { user, token } = data;
const { data } = await login(cpf, password);
const { access } = data;

if (user) {
setAuthorization(token);
handleSetUser(user);
localStorage.setItem(tokenLocalStorageKey, token);
setAuthorization(access);
handleSetUser(null);
localStorage.setItem(tokenLocalStorageKey, access);

navigate(location.state?.from?.pathname || '/');
}
navigate(location.state?.from?.pathname || '/home');
setIsLoadingRequest(false);
} catch (error) {
setIsLoadingRequest(false);
Expand Down Expand Up @@ -95,8 +94,10 @@ export function AuthProvider({ children }: PropsWithChildren) {
localStorage.setItem(userLocalStorageKey, JSON.stringify(user));
};

const token = localStorage.getItem(tokenLocalStorageKey);
const contextValue = {
user,
token,
setUser: handleSetUser,
loading,
isLoadingRequest,
Expand Down
15 changes: 9 additions & 6 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '@styles/index.scss';
import '../App.css';
import logo from '@assets/logo-small.svg';

import { Icon, Item, ItemList, PageHeader, Tab } from '@components';
import { Button, Icon, Item, ItemList, PageHeader, Tab } from '@components';
import { useAuthContext } from '@contexts/AuthProvider';

function HomePage() {
Expand Down Expand Up @@ -43,17 +43,20 @@ function HomePage() {
component: <div>Conteúdo de Caronas</div>,
},
];
const { logout, user } = useAuthContext();
const { logout } = useAuthContext();

return (
<>
<div className="p-s-300 align-items-center">
<div className="pb-s-300 pt-s-300 align-items-center">
<PageHeader
logo={logo}
actions={
<a onClick={() => logout()}>
<Icon>logout</Icon>
</a>
<Button
className="sidebutton__label sidebutton__label--xs"
prefixes={<Icon>logout</Icon>}
design="transparent"
onClick={() => logout()}
></Button>
}
/>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/pages/LoginPage/LoginPage.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import LoginPage from './LoginPage';

const contextValues: AuthContext.TAuthContext = {
user: null,
token: null,
setUser: jest.fn(),
loading: false,
login: jest.fn().mockResolvedValue({} as ApiResponse),
Expand Down
4 changes: 2 additions & 2 deletions src/services/api/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export const setAuthorization = (token: string | null) => {
api.defaults.headers.common.Authorization = authorization;
};

export const login = async (email: string, password: string) => {
return api.post('/login', { email, password });
export const login = async (cpf: string, password: string) => {
return api.post('/token/', { cpf, password });
};

export const sendEmailRecover = async (email: string) => {
Expand Down
3 changes: 2 additions & 1 deletion src/services/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export type ErrorResponse = {
};

export const api = axios.create({
baseURL: process.env.REACT_APP_BASE_URL,
baseURL: `${process.env.REACT_APP_BASE_URL}/api/v1`,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
});

Expand Down

0 comments on commit 79bf4d4

Please sign in to comment.