mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
fix: corrige tipagem do sistema de autenticação
This commit is contained in:
parent
eb77476d51
commit
1e181785b4
@ -2,10 +2,11 @@ import React from 'react';
|
|||||||
import { Navigate, useLocation } from 'react-router-dom';
|
import { Navigate, useLocation } from 'react-router-dom';
|
||||||
import { useAuth } from '../../hooks/useAuth';
|
import { useAuth } from '../../hooks/useAuth';
|
||||||
import type { AuthContextType } from '../../hooks/useAuth';
|
import type { AuthContextType } from '../../hooks/useAuth';
|
||||||
|
import type { UserRole } from '../../types/supabase';
|
||||||
|
|
||||||
interface ProtectedRouteProps {
|
interface ProtectedRouteProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
allowedRoles?: string[];
|
allowedRoles?: UserRole[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ProtectedRoute({ children, allowedRoles = [] }: ProtectedRouteProps) {
|
export function ProtectedRoute({ children, allowedRoles = [] }: ProtectedRouteProps) {
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
import { LogOut, Settings, LayoutDashboard } from 'lucide-react';
|
import { LogOut, Settings, LayoutDashboard } from 'lucide-react';
|
||||||
import { useAuth } from '../../hooks/useAuth';
|
import { useAuth } from '../../hooks/useAuth';
|
||||||
import type { AuthContextType } from '../../hooks/useAuth';
|
import type { AuthContextType } from '../../hooks/useAuth';
|
||||||
|
import type { UserRole } from '../../types/supabase';
|
||||||
|
|
||||||
export function ProfileMenu() {
|
export function ProfileMenu() {
|
||||||
const { user, userRole, signOut } = useAuth();
|
const { user, userRole, signOut } = useAuth();
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { supabase } from '../lib/supabase';
|
import { supabase } from '../lib/supabase';
|
||||||
import { User, UserMetadata } from '@supabase/supabase-js';
|
import { User } from '@supabase/supabase-js';
|
||||||
import { UserMetadata as SupabaseUserMetadata } from '../types/supabase';
|
import { UserMetadata, UserRole } from '../types/supabase';
|
||||||
|
|
||||||
export interface AuthContextType {
|
export interface AuthContextType {
|
||||||
user: User | null;
|
user: User | null;
|
||||||
@ -11,7 +11,7 @@ export interface AuthContextType {
|
|||||||
signIn: (email: string, password: string) => Promise<any>;
|
signIn: (email: string, password: string) => Promise<any>;
|
||||||
signUp: (email: string, password: string) => Promise<any>;
|
signUp: (email: string, password: string) => Promise<any>;
|
||||||
signOut: () => Promise<void>;
|
signOut: () => Promise<void>;
|
||||||
userRole: UserMetadata['role'] | null;
|
userRole: UserRole | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AuthContext = React.createContext<AuthContextType | undefined>(undefined);
|
const AuthContext = React.createContext<AuthContextType | undefined>(undefined);
|
||||||
@ -19,7 +19,7 @@ const AuthContext = React.createContext<AuthContextType | undefined>(undefined);
|
|||||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [user, setUser] = React.useState<User | null>(null);
|
const [user, setUser] = React.useState<User | null>(null);
|
||||||
const [userRole, setUserRole] = React.useState<UserMetadata['role'] | null>(null);
|
const [userRole, setUserRole] = React.useState<UserRole | null>(null);
|
||||||
const [loading, setLoading] = React.useState(true);
|
const [loading, setLoading] = React.useState(true);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
@ -29,7 +29,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
setUser(session.user);
|
setUser(session.user);
|
||||||
const role = session.user.user_metadata.role;
|
const role = session.user.user_metadata.role as UserMetadata['role'];
|
||||||
console.log('Role na sessão:', role);
|
console.log('Role na sessão:', role);
|
||||||
setUserRole(role);
|
setUserRole(role);
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
setUser(session.user);
|
setUser(session.user);
|
||||||
const role = session.user.user_metadata.role;
|
const role = session.user.user_metadata.role as UserMetadata['role'];
|
||||||
console.log('Role no evento:', role);
|
console.log('Role no evento:', role);
|
||||||
setUserRole(role);
|
setUserRole(role);
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useAuth() {
|
export function useAuth(): AuthContextType {
|
||||||
const context = React.useContext(AuthContext);
|
const context = React.useContext(AuthContext);
|
||||||
if (context === undefined) {
|
if (context === undefined) {
|
||||||
throw new Error('useAuth must be used within an AuthProvider');
|
throw new Error('useAuth must be used within an AuthProvider');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user