simple jwt token validation from frontend

This commit is contained in:
IluaAir
2025-10-15 23:03:24 +03:00
parent 7768f94122
commit 2722488a4e
2 changed files with 16 additions and 2 deletions

View File

@@ -1,6 +1,18 @@
import { clsx } from "clsx"; import { clsx } from "clsx";
import { twMerge } from "tailwind-merge" import { twMerge } from "tailwind-merge"
import { jwtDecode as decode } from "jwt-decode";
export function cn(...inputs) { export function cn(...inputs) {
return twMerge(clsx(inputs)); return twMerge(clsx(inputs));
} }
export function jwtexp(token) {
const decoded = decode(token);
if (decoded.exp < Date.now() / 1000) {
console.log("Token expired");
return false;
} else {
console.log("Token is valid");
return true;
}
}

View File

@@ -4,7 +4,7 @@ import './dashboard.css';
import { MenuDock } from '@/components/ui/shadcn-io/menu-dock'; import { MenuDock } from '@/components/ui/shadcn-io/menu-dock';
import { Home, Settings, Bell } from 'lucide-react'; import { Home, Settings, Bell } from 'lucide-react';
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'; import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar';
import { jwtexp } from '@/lib/utils';
const menuItems = [ const menuItems = [
{ label: 'home', icon: Home, onClick: () => console.log('Home clicked') }, { label: 'home', icon: Home, onClick: () => console.log('Home clicked') },
{ label: 'notify', icon: Bell, onClick: () => console.log('Notifications clicked') }, { label: 'notify', icon: Bell, onClick: () => console.log('Notifications clicked') },
@@ -21,7 +21,9 @@ export default function Dashboard() {
{ title: "title16", due_date: "2025-10-15", priority: "medium", id: 6, status: "completed" }, { title: "title16", due_date: "2025-10-15", priority: "medium", id: 6, status: "completed" },
{ title: "super late title", due_date: "2025-10-15", priority: "low", id: 2, status: "open" } { title: "super late title", due_date: "2025-10-15", priority: "low", id: 2, status: "open" }
]; ];
const token = localStorage.getItem('access_token');
const tokenExp = jwtexp(token);
console.log(tokenExp);
// Get current date and week // Get current date and week
const today = new Date(); const today = new Date();
const currentDay = today.getDay(); const currentDay = today.getDay();