Next js

Building a Modern Blog Platform with Redux Toolkit and JWT Admin Authentication

Full-Stack Blog Application with Spring Boot Backend and Secure Redux Frontend

With Next.js and TypeScript, you can build a secure, high-performance, SEO-friendly blog platform with minimal effort. This guide covers setting up Redux Toolkit, JWT authentication (admin only), and best practices for a clean, modular Next.js structure.

πŸš€ Why Next.js + TypeScript?

  • Type safety and reduced runtime bugs
  • Server-side rendering and SEO out of the box
  • Built-in routing and performance optimizations
  • Excellent DX (developer experience)

πŸš€ Step-by-Step Installation Guide

🧰 Prerequisites

  • Node.js v18+
  • Java JDK 17+
  • npm or yarn

πŸ“¦ Create a New Next.js 15 App with TypeScript


npx create-next-app@latest nextjs-blog --typescript
cd nextjs-blog
  

πŸ”§ Install Required Packages


npm install @reduxjs/toolkit react-redux axios
npm install --save-dev @types/react-redux

πŸ” Setup .env.local


NEXT_PUBLIC_API_URL=http://localhost:8080/api
  

πŸ“ Project Folder Structure (Next.js + TypeScript + Redux + Spring Boot Backend)


nextjs-blog/
β”‚
β”œβ”€β”€ public/
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   └── store.ts                        β†’ Redux store setup
β”‚
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── axiosInstance.ts               β†’ Axios with JWT interceptors
β”‚
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”‚   β”œβ”€β”€ authSlice.ts               β†’ Login/logout JWT state
β”‚   β”‚   β”‚   └── authAPI.ts                 β†’ Auth endpoints
β”‚   β”‚   β”œβ”€β”€ posts/
β”‚   β”‚   β”‚   β”œβ”€β”€ postSlice.ts               β†’ Post list state
β”‚   β”‚   β”‚   └── postAPI.ts                 β†’ Post endpoints (CRUD)
β”‚   β”‚   β”œβ”€β”€ categories/
β”‚   β”‚   β”‚   β”œβ”€β”€ categorySlice.ts
β”‚   β”‚   β”‚   └── categoryAPI.ts
β”‚   β”‚   └── comments/
β”‚   β”‚       β”œβ”€β”€ commentSlice.ts
β”‚   β”‚       └── commentAPI.ts
β”‚
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ admin/
β”‚   β”‚   β”‚   └── AdminSidebar.tsx           β†’ Sidebar for admin panel
β”‚   β”‚   └── frontend/
β”‚   β”‚       β”œβ”€β”€ Navbar.tsx                 β†’ Blog navigation
β”‚   β”‚       β”œβ”€β”€ PostCard.tsx               β†’ Individual post display
β”‚   β”‚       └── CommentBox.tsx             β†’ Comment input + list
β”‚
β”‚   β”œβ”€β”€ layouts/
β”‚   β”‚   β”œβ”€β”€ AdminLayout.tsx                β†’ Admin UI container
β”‚   β”‚   └── FrontLayout.tsx                β†’ Public blog layout
β”‚
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   └── PrivateRoute.tsx               β†’ Admin route protection (JWT)
β”‚
β”‚   β”œβ”€β”€ middleware/                        β†’ (Optional for edge auth)
β”‚
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ _app.tsx                       β†’ Wrap app with Redux Provider
β”‚   β”‚   β”œβ”€β”€ index.tsx                      β†’ Blog homepage
β”‚   β”‚   β”œβ”€β”€ about.tsx                      β†’ Static about page
β”‚   β”‚   β”œβ”€β”€ post/[id].tsx                  β†’ Dynamic post view
β”‚   β”‚   β”œβ”€β”€ category/[id].tsx              β†’ Filtered post view
β”‚   β”‚   └── admin/
β”‚   β”‚       β”œβ”€β”€ login.tsx                  β†’ Admin login page
β”‚   β”‚       β”œβ”€β”€ dashboard.tsx              β†’ Admin dashboard summary
β”‚   β”‚       β”œβ”€β”€ posts.tsx                  β†’ Manage blog posts
β”‚   β”‚       β”œβ”€β”€ categories.tsx             β†’ Manage categories
β”‚   β”‚       └── comments.tsx               β†’ Moderate comments
β”‚
β”œβ”€β”€ tsconfig.json                          β†’ TypeScript config
β”œβ”€β”€ .env.local                             β†’ API base, secrets
β”œβ”€β”€ next.config.js                         β†’ Next.js config
└── package.json                           β†’ Dependencies
  

🧠 Setting Up Redux Toolkit in Next.js

store.ts


// src/app/store.ts
import { configureStore } from '@reduxjs/toolkit';
import authReducer from '../features/auth/authSlice';

export const store = configureStore({
  reducer: {
    auth: authReducer,
  },
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
  

_app.tsx


// pages/_app.tsx
import { Provider } from 'react-redux';
import { store } from '../app/store';
import type { AppProps } from 'next/app';

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <Provider store={store}>
      <Component {...pageProps} />
    </Provider>
  );
}
export default MyApp;
  

πŸ” Admin Authentication with JWT

authSlice.ts


import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from '../../api/axiosInstance';

interface AuthState {
  token: string | null;
  status: 'idle' | 'loading' | 'succeeded' | 'failed';
  error: string | null;
}

const initialState: AuthState = {
  token: typeof window !== 'undefined' ? localStorage.getItem('token') : null,
  status: 'idle',
  error: null,
};

export const login = createAsyncThunk(
  'auth/login',
  async (credentials: { email: string; password: string }) => {
    const res = await axios.post('/auth/login', credentials);
    return res.data.token;
  }
);
  

🧾 Admin Pages (Protected)

  • /admin/login.tsx β†’ Login form
  • /admin/dashboard.tsx β†’ Summary and metrics
  • /admin/posts.tsx β†’ Manage posts
  • /admin/comments.tsx β†’ Approve comments

🌐 Frontend Pages (Public)

  • /index.tsx β†’ Home
  • /post/[id].tsx β†’ Post Details
  • /category/[id].tsx β†’ Category Filter
  • /about.tsx β†’ Static Info

βœ… Deployment Tips

  • Use Vercel for frontend hosting
  • Deploy Spring Boot API on Render or Railway
  • Store API base in .env.local as NEXT_PUBLIC_API_URL

🎯 Final Thoughts

With this setup, you now have a robust Next.js blog system with admin-only JWT protection, full Redux state management, and a flexible codebase ready for future expansionβ€”image uploads, rich-text editor, comment replies, and beyond.

Leave a Reply

Your email address will not be published. Required fields are marked *