Fix login redirect behavior

This commit is contained in:
2026-05-12 22:00:56 +07:00
parent 713bfbcbb9
commit f54ff6eb0e
6 changed files with 52 additions and 9 deletions

View File

@ -33,17 +33,53 @@ Target deploy:
- reverse proxy `nginx`
Contoh langkah di server:
1. Buat user deploy khusus:
```bash
sudo adduser deploywa
sudo usermod -aG sudo deploywa
sudo mkdir -p /var/www
sudo chown -R deploywa:deploywa /var/www
su - deploywa
```
2. Clone project:
```bash
cd /var/www
git clone https://git.iptek.co/wirabasalamah/webhook-test.git wa-test-nextjs
cd wa-test-nextjs
```
3. Siapkan environment:
```bash
cp .env.example .env
```
4. Install dependency dan build:
```bash
npm ci
npm run build
```
5. Jalankan dengan PM2:
```bash
npx pm2 start ecosystem.config.cjs
npx pm2 save
```
6. Pasang config nginx:
```bash
sudo cp deploy/nginx/wa.iptek.co.conf /etc/nginx/sites-available/wa.iptek.co.conf
sudo ln -s /etc/nginx/sites-available/wa.iptek.co.conf /etc/nginx/sites-enabled/wa.iptek.co.conf
sudo nginx -t
sudo systemctl reload nginx
```
7. Jika pakai HTTPS:
```bash
sudo certbot --nginx -d wa.iptek.co
```
Set `.env` minimal:
```bash
NEXT_PUBLIC_APP_URL=https://wa.iptek.co

View File

@ -1,5 +1,6 @@
import { NextResponse } from 'next/server'
import { createSession } from '@/lib/auth'
import { buildRedirectUrl } from '@/lib/url'
export async function POST(request: Request) {
const formData = await request.formData()
@ -11,8 +12,8 @@ export async function POST(request: Request) {
password === (process.env.WA_TEST_LOGIN_PASSWORD || 'admin123')
) {
await createSession()
return NextResponse.redirect(new URL('/settings', request.url))
return NextResponse.redirect(buildRedirectUrl(request, '/login'))
}
return NextResponse.redirect(new URL('/login?error=Login%20gagal', request.url))
return NextResponse.redirect(buildRedirectUrl(request, '/login?error=Login%20gagal'))
}

View File

@ -1,7 +1,8 @@
import { NextResponse } from 'next/server'
import { clearSession } from '@/lib/auth'
import { buildRedirectUrl } from '@/lib/url'
export async function POST(request: Request) {
await clearSession()
return NextResponse.redirect(new URL('/login', request.url))
return NextResponse.redirect(buildRedirectUrl(request, '/login'))
}

View File

@ -1,14 +1,15 @@
import { NextResponse } from 'next/server'
import { sendTextMessage } from '@/lib/whatsapp'
import { buildRedirectUrl } from '@/lib/url'
export async function POST(request: Request) {
const formData = await request.formData()
const message = formData.get('message')?.toString() || ''
if (!message.trim()) {
return NextResponse.redirect(new URL('/test?status=Pesan%20kosong', request.url))
return NextResponse.redirect(buildRedirectUrl(request, '/test?status=Pesan%20kosong'))
}
await sendTextMessage(process.env.WA_TEST_NUMBER || '', message)
return NextResponse.redirect(new URL('/test?status=Pesan%20berhasil%20dikirim', request.url))
return NextResponse.redirect(buildRedirectUrl(request, '/test?status=Pesan%20berhasil%20dikirim'))
}

View File

@ -1,10 +1,7 @@
import { redirect } from 'next/navigation'
import { isAuthenticated } from '@/lib/auth'
export default async function LoginPage({ searchParams }: { searchParams: Promise<{ error?: string }> }) {
if (await isAuthenticated()) {
redirect('/settings')
}
await isAuthenticated()
const params = await searchParams

7
src/lib/url.ts Normal file
View File

@ -0,0 +1,7 @@
export function getAppBaseUrl(request: Request) {
return process.env.NEXT_PUBLIC_APP_URL || request.url
}
export function buildRedirectUrl(request: Request, path: string) {
return new URL(path, getAppBaseUrl(request))
}