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 React, { useState, ChangeEvent, FormEvent } from 'react';
|
||||||
import IconTrashLines from '../icon/icon-trash-lines';
|
import IconTrashLines from '../icon/icon-trash-lines';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import Cookies from 'universal-cookie';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { showMessage } from '@/utils/CommonFunction.utils';
|
import { showMessage } from '@/utils/CommonFunction.utils';
|
||||||
import { buildApiUrl } from '@/utils/BaseUrl.utils';
|
import { buildApiUrl } from '@/utils/BaseUrl.utils';
|
||||||
@ -86,9 +87,17 @@ const CreateEventForm: React.FC = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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, {
|
const ImageUpload = await axios.post(buildApiUrl('upload/single'), data, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "multipart/form-data", // important for file upload
|
"Content-Type": "multipart/form-data", // important for file upload
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
console.log("ImageUpload", ImageUpload)
|
console.log("ImageUpload", ImageUpload)
|
||||||
@ -100,7 +109,11 @@ const CreateEventForm: React.FC = () => {
|
|||||||
eventimageurl: ImageUpload?.data?.data?.fullUrl
|
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)
|
console.log("res", res)
|
||||||
showMessage("Event Created Successfully", "success")
|
showMessage("Event Created Successfully", "success")
|
||||||
router?.push(`/`)
|
router?.push(`/`)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import Cookies from 'universal-cookie';
|
||||||
import { Metadata } from 'next';
|
import { Metadata } from 'next';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
@ -24,7 +25,16 @@ const ListOfEvents = () => {
|
|||||||
|
|
||||||
const getEvents = async () => {
|
const getEvents = async () => {
|
||||||
try {
|
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)
|
console.log("eventRes", eventRes)
|
||||||
setEvents(eventRes?.data?.data)
|
setEvents(eventRes?.data?.data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -62,7 +72,11 @@ const ListOfEvents = () => {
|
|||||||
}).then(async (result) => {
|
}).then(async (result) => {
|
||||||
if (result.isConfirmed) {
|
if (result.isConfirmed) {
|
||||||
try {
|
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({
|
Swal.fire({
|
||||||
title: 'Deleted!',
|
title: 'Deleted!',
|
||||||
text: 'Your file has been 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