31 lines
665 B
TypeScript
31 lines
665 B
TypeScript
import { type Component, type JSX, Show } from "solid-js";
|
|
import { jwtDecode } from "jwt-decode";
|
|
import { Navigate } from "@solidjs/router";
|
|
|
|
export const isTokenValid = (): boolean => {
|
|
const token = localStorage.getItem("access");
|
|
|
|
if (token == null) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
jwtDecode(token);
|
|
return true;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export const ProtectedRoute: Component<{ children?: JSX.Element }> = (
|
|
props,
|
|
) => {
|
|
const isValid = isTokenValid();
|
|
|
|
return (
|
|
<Show when={isValid} fallback={<Navigate href="/login" />}>
|
|
{props.children}
|
|
</Show>
|
|
);
|
|
};
|