login
This commit is contained in:
parent
38313f6340
commit
897dbfe20d
6
app/auth/login/page.tsx
Normal file
6
app/auth/login/page.tsx
Normal file
@ -0,0 +1,6 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export default function AuthLoginRedirect() {
|
||||
// Redirect legacy /auth/login URL to the actual login page at /login
|
||||
redirect('/login');
|
||||
}
|
||||
@ -1,7 +1,8 @@
|
||||
'use client';
|
||||
'use client';
|
||||
import React, { useState, ChangeEvent, FormEvent } from 'react';
|
||||
import IconTrashLines from '../icon/icon-trash-lines';
|
||||
import axios from 'axios';
|
||||
import Cookies from 'universal-cookie';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { showMessage } from '@/utils/CommonFunction.utils';
|
||||
import { buildApiUrl } from '@/utils/BaseUrl.utils';
|
||||
@ -86,9 +87,17 @@ const CreateEventForm: React.FC = () => {
|
||||
}
|
||||
|
||||
try {
|
||||
const cookies = new Cookies();
|
||||
const token = cookies.get('token');
|
||||
if (!token) {
|
||||
showMessage('Access denied. Please sign in first.');
|
||||
router.push('/login');
|
||||
return;
|
||||
}
|
||||
const ImageUpload = await axios.post(buildApiUrl('upload/single'), data, {
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data", // important for file upload
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
console.log("ImageUpload", ImageUpload)
|
||||
@ -100,7 +109,11 @@ const CreateEventForm: React.FC = () => {
|
||||
eventimageurl: ImageUpload?.data?.data?.fullUrl
|
||||
}
|
||||
|
||||
const res = await axios.post(buildApiUrl('events'), createData)
|
||||
const res = await axios.post(buildApiUrl('events'), createData, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${cookies.get('token')}`,
|
||||
},
|
||||
})
|
||||
console.log("res", res)
|
||||
showMessage("Event Created Successfully", "success")
|
||||
router?.push(`/`)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
'use client';
|
||||
'use client';
|
||||
import axios from 'axios';
|
||||
import Cookies from 'universal-cookie';
|
||||
import { Metadata } from 'next';
|
||||
import Link from 'next/link';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
@ -24,7 +25,16 @@ const ListOfEvents = () => {
|
||||
|
||||
const getEvents = async () => {
|
||||
try {
|
||||
const eventRes: any = await axios?.get(buildApiUrl('events'))
|
||||
const cookies = new Cookies();
|
||||
const token = cookies.get('token');
|
||||
if (!token) {
|
||||
// No token: redirect to login
|
||||
router.push('/login');
|
||||
return;
|
||||
}
|
||||
const eventRes: any = await axios.get(buildApiUrl('events'), {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
console.log("eventRes", eventRes)
|
||||
setEvents(eventRes?.data?.data)
|
||||
} catch (error) {
|
||||
@ -62,7 +72,11 @@ const ListOfEvents = () => {
|
||||
}).then(async (result) => {
|
||||
if (result.isConfirmed) {
|
||||
try {
|
||||
await axios.delete(buildApiUrl(`events/${event.id}`));
|
||||
const cookies = new Cookies();
|
||||
const token = cookies.get('token');
|
||||
await axios.delete(buildApiUrl(`events/${event.id}`), {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
Swal.fire({
|
||||
title: 'Deleted!',
|
||||
text: 'Your file has been deleted.',
|
||||
|
||||
38
middleware.ts
Normal file
38
middleware.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export function middleware(req: NextRequest) {
|
||||
const { pathname } = req.nextUrl;
|
||||
|
||||
// Allow public and framework paths without auth
|
||||
const allowlist = [
|
||||
'/login', // login page
|
||||
|
||||
];
|
||||
|
||||
if (
|
||||
pathname.startsWith('/_next') ||
|
||||
pathname.startsWith('/static') ||
|
||||
pathname.startsWith('/assets') ||
|
||||
|
||||
allowlist.some((p) => pathname === p || pathname.startsWith(p + '/')) ||
|
||||
// allow public files (images, css, etc.)
|
||||
/\.(jpg|jpeg|png|svg|ico|css|js|map)$/.test(pathname)
|
||||
) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// For all other routes, require a token cookie
|
||||
const token = req.cookies.get('token')?.value;
|
||||
if (!token) {
|
||||
const url = req.nextUrl.clone();
|
||||
url.pathname = '/login';
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/:path*'],
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user