Estado inicial: frontend React + backend Laravel
This commit is contained in:
26
frontend-plataforma-tutoriais/src/hooks/useGetCurrentUser.ts
Normal file
26
frontend-plataforma-tutoriais/src/hooks/useGetCurrentUser.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
import type { User } from "../types";
|
||||
|
||||
interface ApiUserResponse {
|
||||
message: string;
|
||||
data: User;
|
||||
errors: null | unknown;
|
||||
}
|
||||
|
||||
export function useGetCurrentUser() {
|
||||
async function getCurrentUser(): Promise<ApiUserResponse> {
|
||||
const response = await fetch("http://127.0.0.1:8000/api/me", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${localStorage.getItem("token")}`
|
||||
},
|
||||
});
|
||||
|
||||
const currentUser = await response.json() as ApiUserResponse;
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
return { getCurrentUser };
|
||||
}
|
||||
Reference in New Issue
Block a user