LingAdmin/Backend/LingAdmin.API/fix-auth.ps1

55 lines
2.3 KiB
PowerShell
Raw Permalink Normal View History

2026-04-16 18:13:06 +08:00
# Fix user passwords by using the registration API to create a test admin user
Write-Host "Fixing user authentication..." -ForegroundColor Cyan
# First, register a new admin user with proper password hashing
Write-Host "1. Registering new admin user..." -ForegroundColor Yellow
try {
$registerData = @{
name = "Admin User"
email = "admin@example.com"
password = "Admin123!"
role = "Admin"
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "http://localhost:5000/api/auth/register" -Method Post -Body $registerData -ContentType "application/json"
Write-Host "✓ Admin user registered successfully!" -ForegroundColor Green
Write-Host " Email: admin@example.com" -ForegroundColor Green
Write-Host " Password: Admin123!" -ForegroundColor Green
} catch {
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
if ($errorDetails.message -like "*already exists*") {
Write-Host "⚠ Admin user already exists" -ForegroundColor Yellow
} else {
Write-Host "✗ Registration failed: $($errorDetails.message)" -ForegroundColor Red
}
}
Write-Host ""
# Now try to login
Write-Host "2. Testing login with new admin user..." -ForegroundColor Yellow
try {
$loginData = @{
email = "admin@example.com"
password = "Admin123!"
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "http://localhost:5000/api/auth/login" -Method Post -Body $loginData -ContentType "application/json"
Write-Host "✓ Login successful!" -ForegroundColor Green
Write-Host "Token (first 50 chars): $($response.data.token.Substring(0, [Math]::Min(50, $response.data.token.Length)))..." -ForegroundColor Green
Write-Host "User: $($response.data.user.name) ($($response.data.user.role))" -ForegroundColor Green
$global:authToken = $response.data.token
$global:userId = $response.data.user.id
} catch {
Write-Host "✗ Login failed: $_" -ForegroundColor Red
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Setup Complete!" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "You can now use:" -ForegroundColor Yellow
Write-Host " Email: admin@example.com" -ForegroundColor White
Write-Host " Password: Admin123!" -ForegroundColor White