-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from fga-eps-mds/128-realizar-login
128 - criação da tela de realizar login
- Loading branch information
Showing
8 changed files
with
312 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
createContext, | ||
ReactNode, | ||
useCallback, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
import { toast } from '@/utils/toast'; | ||
import { api } from '@/config/lib/axios'; | ||
import { SignedUser, SignInCredentials, AuthResponse } from '@/types/auth'; | ||
|
||
interface AuthContextData { | ||
signOut(): void; | ||
signIn(credentials: SignInCredentials): Promise<void>; | ||
isAuthenticated: boolean; | ||
user: SignedUser | null; | ||
} | ||
|
||
const AuthContext = createContext({} as AuthContextData); | ||
|
||
interface AuthProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export function AuthProvider({ children }: AuthProviderProps) { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const [user, setUser] = useState<SignedUser | null>(() => { | ||
const loadedUser = localStorage.getItem('@alectrion:user'); | ||
|
||
if (!loadedUser) return {} as SignedUser; | ||
|
||
return JSON.parse(loadedUser); | ||
}); | ||
const isAuthenticated = !!user?.token; | ||
|
||
const signIn = useCallback( | ||
async ({ username, password }: SignInCredentials) => { | ||
try { | ||
const response = await api.post('user/login', { | ||
username, | ||
password, | ||
}); | ||
|
||
const { email, expireIn, job, name, role, token } = response.data; | ||
|
||
localStorage.setItem('@alectrion:token', token); | ||
localStorage.setItem( | ||
'@alectrion:user', | ||
JSON.stringify({ | ||
name, | ||
email, | ||
expireIn, | ||
token, | ||
job, | ||
role, | ||
}) | ||
); | ||
|
||
setUser({ email, expireIn, job, name, role, token }); | ||
|
||
api.defaults.headers.common.Authorization = `Bearer ${token}`; | ||
|
||
const from = location.state?.from?.pathname || '/'; | ||
|
||
navigate(from, { replace: true }); | ||
} catch (err) { | ||
toast.error( | ||
'Não foi possível realizar o login! Verifique o nome de usuário e a senha e tente novamente.' | ||
); | ||
} | ||
}, | ||
[navigate, location.state?.from?.pathname] | ||
); | ||
|
||
const signOut = useCallback(() => { | ||
localStorage.removeItem('@alectrion:token'); | ||
localStorage.removeItem('@alectrion:user'); | ||
|
||
setUser(null); | ||
|
||
navigate('/login'); | ||
}, [navigate]); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
signIn, | ||
signOut, | ||
isAuthenticated, | ||
user, | ||
}), | ||
[signIn, signOut, isAuthenticated, user] | ||
); | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
} | ||
|
||
export function useAuth(): AuthContextData { | ||
const context = useContext(AuthContext); | ||
|
||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Icon, IconProps } from '@chakra-ui/react'; | ||
|
||
export function AlectrionIcon(props: IconProps) { | ||
return ( | ||
<Icon | ||
width="672" | ||
height="654" | ||
viewBox="0 0 672 654" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
{...props} | ||
> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M328.57 4.34284C331.546 -1.44761 339.824 -1.44761 342.8 4.34283L670.474 641.843C673.21 647.167 669.345 653.5 663.359 653.5H8.01059C2.02454 653.5 -1.84099 647.167 0.895506 641.843L328.57 4.34284ZM331.45 180.727C333.403 176.764 338.199 175.135 342.162 177.088L342.653 177.33C344.721 177.591 346.657 178.654 347.981 180.364C349.184 181.512 349.983 182.997 350.301 184.59L491.304 459.723C493.258 463.535 491.857 468.183 488.186 470.299C486.733 472.301 484.374 473.605 481.707 473.605L307.685 473.605L341.857 454.367C343.055 453.693 344.406 453.339 345.781 453.339H465.257L340.074 209.075L159.901 574.631C157.948 578.594 153.152 580.223 149.189 578.27L145.36 576.383C141.397 574.43 139.768 569.633 141.721 565.67L331.45 180.727ZM341.185 375.5C351.678 375.5 360.185 366.994 360.185 356.5C360.185 346.007 351.678 337.5 341.185 337.5C330.691 337.5 322.185 346.007 322.185 356.5C322.185 366.994 330.691 375.5 341.185 375.5Z" | ||
fill="#F49320" | ||
/> | ||
</Icon> | ||
); | ||
} |
Oops, something went wrong.