A modern authentication system featuring JWT tokens, secure cookie sessions, and real-time validation. Built with Go Fiber backend and Vanilla JavaScript frontend for maximum performance and simplicity.
|
|
- Go 1.19+ installed
- Node.js 16+ installed
# Navigate to backend directory
cd backend
# Install dependencies
go mod tidy
# Run the server
go run main.go
🎯 Backend runs on:
http://localhost:8000
# Navigate to frontend directory
cd frontend
# Start the file server
node server.js
🎯 Frontend available at:
http://localhost:3000
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST |
/api/register |
📝 Register new user | ❌ |
POST |
/api/login |
🔐 User login | ❌ |
GET |
/api/user |
👤 Get user profile | ✅ |
POST |
/api/logout |
🚪 User logout | ✅ |
Feature | Description |
---|---|
🔐 Secure Authentication | JWT tokens with HttpOnly cookies |
🛡️ Password Security | bcrypt hashing with salt rounds |
🔒 Protected Routes | Authentication middleware |
📱 Responsive Design | Mobile-first Bootstrap UI |
⚡ Real-time Validation | Instant form feedback |
🔄 Smart Redirects | Context-aware navigation |
🍪 Secure Sessions | XSS-protected cookie storage |
⏰ Token Expiry | 24-hour automatic timeout |
🔐 | Password Hashing bcrypt with configurable salt rounds |
🍪 | HttpOnly Cookies XSS protection for JWT storage |
🌐 | CORS Configuration Restricted to frontend origin only |
⏰ | Token Expiration Automatic 24-hour timeout |
// Core Framework
github.com/gofiber/fiber/v2
// Database ORM
gorm.io/gorm
gorm.io/driver/sqlite
// JWT Handling
github.com/golang-jwt/jwt/v4
// Password Hashing
golang.org/x/crypto/bcrypt
- 🎨 Bootstrap 5 - Modern UI framework
- ⚡ Vanilla JavaScript - No framework overhead
- 🌐 Fetch API - Native HTTP requests
JWTAUTHAPI/
├── 📂 backend/
│ ├── 📄 main.go # Server entry point
│ ├── 📄 models.go # Database models
│ ├── 📄 handlers.go # Route handlers
│ ├── 📄 middleware.go # Auth middleware
│ └── 📄 go.mod # Go dependencies
├── 📂 frontend/
│ ├── 📄 index.html # Main HTML file
│ ├── 📄 script.js # JavaScript logic
│ ├── 📄 style.css # Custom styles
│ └── 📄 server.js # Static file server
└── 📄 README.md
const response = await fetch('/api/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'John Doe',
email: '[email protected]',
password: 'securePassword123'
})
});
const userInfo = await fetch('/api/user', {
credentials: 'include' // Include cookies
});
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.