45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import type { AuthRouteRequirement } from '../types/auth'
|
|
|
|
const PROTECTED_ROUTES: AuthRouteRequirement[] = [
|
|
{
|
|
method: 'GET',
|
|
path: '/profile/me'
|
|
},
|
|
{
|
|
method: 'GET',
|
|
path: '/dashboard'
|
|
}
|
|
]
|
|
|
|
/**
|
|
* Normaliza o caminho da requisição para facilitar a comparação de rotas.
|
|
* Remove query string e barra final extra.
|
|
*
|
|
* @param path Caminho original recebido na requisição.
|
|
* @returns Caminho em formato padronizado.
|
|
*/
|
|
function normalizePath(path: string): string {
|
|
const withoutQuery = path.split('?')[0] ?? '/'
|
|
const normalized = withoutQuery.replace(/\/+$/, '')
|
|
|
|
return normalized || '/'
|
|
}
|
|
|
|
/**
|
|
* Retorna a regra de autenticação da rota atual, quando existir.
|
|
*
|
|
* @param method Método HTTP da requisição.
|
|
* @param path Caminho da requisição.
|
|
* @returns Regra da rota protegida ou `null` quando a rota é pública.
|
|
*/
|
|
export function getRouteRequirement(method: string, path: string): AuthRouteRequirement | null {
|
|
const normalizedMethod = method.toUpperCase()
|
|
const normalizedPath = normalizePath(path)
|
|
|
|
return (
|
|
PROTECTED_ROUTES.find(
|
|
(route) => route.method === normalizedMethod && route.path === normalizedPath
|
|
) ?? null
|
|
)
|
|
}
|