first commit

This commit is contained in:
Beyhan Oğur
2026-04-26 22:20:45 +03:00
commit d50f14bcb1
681 changed files with 65020 additions and 0 deletions

401
static/spa-test.css Normal file
View File

@@ -0,0 +1,401 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.navbar {
background: white;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
position: sticky;
top: 0;
z-index: 100;
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-brand {
font-size: 1.5rem;
font-weight: bold;
color: #667eea;
}
.nav-menu {
display: flex;
align-items: center;
gap: 1rem;
}
.user-email {
color: #666;
font-size: 0.9rem;
}
.btn-logout {
padding: 0.5rem 1rem;
background: #e74c3c;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 0.9rem;
transition: background 0.3s;
}
.btn-logout:hover {
background: #c0392b;
}
.page {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem;
}
.container {
max-width: 500px;
width: 100%;
}
.card {
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
padding: 3rem;
}
h1 {
color: #333;
margin-bottom: 0.5rem;
text-align: center;
font-size: 2rem;
}
.subtitle {
color: #666;
text-align: center;
margin-bottom: 2rem;
font-size: 1rem;
}
.form-group {
margin-bottom: 1.5rem;
}
.form-group label {
display: block;
color: #333;
font-weight: 600;
margin-bottom: 0.5rem;
font-size: 0.9rem;
}
.form-group input {
width: 100%;
padding: 0.8rem 1rem;
border: 2px solid #e1e8ed;
border-radius: 8px;
font-size: 1rem;
transition: border-color 0.3s;
}
.form-group input:focus {
outline: none;
border-color: #667eea;
}
.btn {
width: 100%;
padding: 1rem;
border: none;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
text-decoration: none;
display: block;
text-align: center;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5568d3;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.btn-google {
background: #4285f4;
color: white;
margin-bottom: 0.8rem;
}
.btn-google:hover {
background: #357ae8;
}
.btn-github {
background: #24292e;
color: white;
}
.btn-github:hover {
background: #1a1e22;
}
.btn-small {
width: auto;
padding: 0.5rem 1rem;
font-size: 0.85rem;
}
.divider {
text-align: center;
margin: 2rem 0;
color: #999;
position: relative;
}
.divider::before,
.divider::after {
content: "";
position: absolute;
top: 50%;
width: 45%;
height: 1px;
background: #ddd;
}
.divider::before { left: 0; }
.divider::after { right: 0; }
.social-buttons {
margin-top: 1.5rem;
}
.footer-text {
text-align: center;
margin-top: 2rem;
color: #666;
font-size: 0.9rem;
}
.footer-text a {
color: #667eea;
text-decoration: none;
font-weight: 600;
}
.footer-text a:hover {
text-decoration: underline;
}
.alert {
padding: 1rem;
border-radius: 8px;
margin-top: 1rem;
font-size: 0.9rem;
}
.alert-error {
background: #fee;
border: 2px solid #fcc;
color: #c33;
}
.alert-success {
background: #efe;
border: 2px solid #cfc;
color: #3c3;
}
.profile-section {
display: flex;
align-items: center;
gap: 1.5rem;
margin: 2rem 0;
padding: 1.5rem;
background: #f8f9fa;
border-radius: 12px;
}
.profile-avatar {
font-size: 4rem;
width: 80px;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
background: #667eea;
border-radius: 50%;
}
.profile-info h2 {
color: #333;
margin-bottom: 0.3rem;
font-size: 1.5rem;
}
.text-muted {
color: #666;
font-size: 0.9rem;
}
.badge {
display: inline-block;
padding: 0.3rem 0.8rem;
background: #28a745;
color: white;
border-radius: 20px;
font-size: 0.75rem;
font-weight: 600;
margin-top: 0.5rem;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 1rem;
margin: 2rem 0;
}
.stat-card {
background: #f8f9fa;
padding: 1.5rem;
border-radius: 12px;
display: flex;
align-items: center;
gap: 1rem;
}
.stat-icon {
font-size: 2rem;
}
.stat-info h3 {
color: #333;
font-size: 1.1rem;
margin-bottom: 0.3rem;
}
.stat-info p {
color: #666;
font-size: 0.8rem;
}
.token-section {
margin: 2rem 0;
}
.token-section h3 {
color: #333;
margin-bottom: 1rem;
}
.token-box {
margin-bottom: 1.5rem;
}
.token-box label {
display: block;
color: #666;
font-size: 0.85rem;
font-weight: 600;
margin-bottom: 0.5rem;
}
.token-box textarea {
width: 100%;
padding: 0.8rem;
border: 2px solid #e1e8ed;
border-radius: 8px;
font-family: 'Courier New', monospace;
font-size: 0.75rem;
resize: vertical;
min-height: 80px;
margin-bottom: 0.5rem;
}
.loading-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.7);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 9999;
color: white;
}
.spinner {
border: 4px solid rgba(255,255,255,0.3);
border-top: 4px solid white;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 1s linear infinite;
margin-bottom: 1rem;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Dashboard specific styles */
#dashboardPage .container {
max-width: 800px;
}
#dashboardPage .card {
padding: 2rem;
}
.info-box {
background: #e7f3ff;
border: 2px solid #b3d9ff;
color: #004085;
padding: 1rem;
border-radius: 8px;
margin-top: 1.5rem;
font-size: 0.85rem;
}
.info-box strong {
display: block;
margin-bottom: 0.5rem;
}
.info-box ol {
margin-left: 1.5rem;
margin-top: 0.5rem;
}
.info-box li {
margin: 0.3rem 0;
}

381
static/spa-test.js Normal file
View File

@@ -0,0 +1,381 @@
// API Base URL
const API_BASE = 'http://localhost:8000/api/v1';
// Google OAuth Client ID
const GOOGLE_CLIENT_ID = '915364976256-691m0s87as2r5vdbqr96f6humblseobt.apps.googleusercontent.com';
// Initialize app
document.addEventListener('DOMContentLoaded', () => {
checkAuth();
checkOAuthCallback();
});
// Check if user is authenticated
function checkAuth() {
const accessToken = localStorage.getItem('access_token');
if (accessToken) {
// User is logged in, show dashboard
loadUserProfile();
} else {
// User is not logged in, show login page
showLogin();
}
}
// Check for OAuth callback parameters
function checkOAuthCallback() {
const urlParams = new URLSearchParams(window.location.search);
const access = urlParams.get('access');
const refresh = urlParams.get('refresh');
if (access && refresh) {
// Save tokens from OAuth callback
localStorage.setItem('access_token', access);
localStorage.setItem('refresh_token', refresh);
// Clean URL
window.history.replaceState({}, document.title, window.location.pathname);
// Load user profile
loadUserProfile();
}
}
// Show login page
function showLogin() {
document.getElementById('loginPage').style.display = 'flex';
document.getElementById('registerPage').style.display = 'none';
document.getElementById('dashboardPage').style.display = 'none';
document.getElementById('navbar').style.display = 'none';
}
// Show register page
function showRegister() {
document.getElementById('loginPage').style.display = 'none';
document.getElementById('registerPage').style.display = 'flex';
document.getElementById('dashboardPage').style.display = 'none';
document.getElementById('navbar').style.display = 'none';
}
// Show dashboard
function showDashboard() {
document.getElementById('loginPage').style.display = 'none';
document.getElementById('registerPage').style.display = 'none';
document.getElementById('dashboardPage').style.display = 'flex';
document.getElementById('navbar').style.display = 'block';
}
// Show loading
function showLoading() {
document.getElementById('loadingOverlay').style.display = 'flex';
}
// Hide loading
function hideLoading() {
document.getElementById('loadingOverlay').style.display = 'none';
}
// Show error
function showError(message) {
const errorAlert = document.getElementById('errorAlert');
errorAlert.textContent = message;
errorAlert.style.display = 'block';
setTimeout(() => {
errorAlert.style.display = 'none';
}, 5000);
}
// Handle login form submit
async function handleLogin(event) {
event.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
showLoading();
try {
const response = await fetch(`${API_BASE}/auth/jwt/create/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password })
});
const data = await response.json();
if (response.ok) {
// Save tokens
localStorage.setItem('access_token', data.access);
localStorage.setItem('refresh_token', data.refresh);
// Load profile
await loadUserProfile();
} else {
hideLoading();
showError(data.detail || 'Login failed. Please check your credentials.');
}
} catch (error) {
hideLoading();
showError('Network error. Please try again.');
}
}
// Handle register form submit
async function handleRegister(event) {
event.preventDefault();
const email = document.getElementById('regEmail').value;
const password = document.getElementById('regPassword').value;
const passwordConfirm = document.getElementById('regPasswordConfirm').value;
const firstName = document.getElementById('firstName').value;
const lastName = document.getElementById('lastName').value;
if (password !== passwordConfirm) {
showError('Passwords do not match!');
return;
}
showLoading();
try {
const response = await fetch(`${API_BASE}/auth/users/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
password,
re_password: passwordConfirm,
first_name: firstName,
last_name: lastName
})
});
const data = await response.json();
hideLoading();
if (response.ok) {
// Show success message
document.getElementById('registerSuccess').style.display = 'block';
document.getElementById('registerForm').reset();
// Redirect to login after 3 seconds
setTimeout(() => {
showLogin();
document.getElementById('registerSuccess').style.display = 'none';
}, 3000);
} else {
const errorMsg = Object.values(data).flat().join(', ');
showError(errorMsg || 'Registration failed.');
}
} catch (error) {
hideLoading();
showError('Network error. Please try again.');
}
}
// Load user profile
async function loadUserProfile() {
showLoading();
const accessToken = localStorage.getItem('access_token');
try {
const response = await fetch(`${API_BASE}/auth/users/me/`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (response.ok) {
const user = await response.json();
// Update UI with user data
document.getElementById('userEmail').textContent = user.email;
document.getElementById('profileName').textContent =
`${user.first_name} ${user.last_name}` || user.email;
document.getElementById('profileEmail').textContent = user.email;
document.getElementById('userEmailStat').textContent = user.email;
document.getElementById('profileStatus').textContent =
user.is_active ? '✅ Active' : '❌ Inactive';
// Format date
const joinDate = new Date(user.date_joined);
document.getElementById('joinedDate').textContent =
joinDate.toLocaleDateString('en-US', { month: 'short', year: 'numeric' });
// Show tokens
document.getElementById('accessToken').value = localStorage.getItem('access_token');
document.getElementById('refreshToken').value = localStorage.getItem('refresh_token');
hideLoading();
showDashboard();
} else {
// Token expired or invalid
logout();
}
} catch (error) {
hideLoading();
showError('Failed to load profile.');
logout();
}
}
// Logout
function logout() {
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
showLogin();
}
// Copy token to clipboard
function copyToken(type) {
const token = type === 'access'
? document.getElementById('accessToken').value
: document.getElementById('refreshToken').value;
navigator.clipboard.writeText(token)
.then(() => alert(`${type} token copied to clipboard!`))
.catch(() => alert('Failed to copy token'));
}
// ============================================================================
// SOCIAL AUTH - TOKEN-BASED FLOW (like Nuxt/Next.js)
// ============================================================================
// Google Login (Token-Based)
function loginWithGoogle() {
// Trigger Google One Tap or popup
google.accounts.id.prompt();
}
// Handle Google OAuth callback
async function handleGoogleCallback(response) {
console.log('[Google OAuth] Credential received:', response.credential);
showLoading();
try {
// Send Google's ID token to Django backend
const result = await fetch(`${API_BASE}/auth/social/google-oauth2/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
access_token: response.credential, // Google's ID token
id_token: response.credential
})
});
const data = await result.json();
console.log('[Django Response]:', data);
hideLoading();
if (result.ok) {
// Save JWT tokens from Django
localStorage.setItem('access_token', data.access);
localStorage.setItem('refresh_token', data.refresh);
console.log('[Success] JWT tokens saved to localStorage');
// Load user profile
await loadUserProfile();
} else {
showError(data.error || 'Google login failed');
}
} catch (error) {
hideLoading();
console.error('[Error]:', error);
showError('Network error during Google login');
}
}
// GitHub Login (Token-Based)
async function loginWithGitHub() {
alert(`
⚠️ GitHub Token-Based Flow için:
1. GitHub Personal Access Token oluşturun:
https://github.com/settings/tokens
2. Scopes: user, user:email
3. Token'ı prompt'a girin
NOT: Production'da OAuth2 PKCE flow kullanılmalı.
Bu sadece test için!
`);
const token = prompt('GitHub Personal Access Token girin:');
if (!token) return;
showLoading();
try {
const result = await fetch(`${API_BASE}/auth/social/github/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
access_token: token
})
});
const data = await result.json();
console.log('[Django Response]:', data);
hideLoading();
if (result.ok) {
// Save JWT tokens from Django
localStorage.setItem('access_token', data.access);
localStorage.setItem('refresh_token', data.refresh);
console.log('[Success] JWT tokens saved to localStorage');
// Load user profile
await loadUserProfile();
} else {
showError(data.error || 'GitHub login failed');
}
} catch (error) {
hideLoading();
console.error('[Error]:', error);
showError('Network error during GitHub login');
}
}
// Test protected endpoint
async function testProtectedEndpoint() {
showLoading();
const accessToken = localStorage.getItem('access_token');
try {
const response = await fetch(`${API_BASE}/auth/users/me/`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
hideLoading();
if (response.ok) {
alert('✅ Protected endpoint access successful!\n\nYour JWT token is working correctly.');
} else {
alert('❌ Protected endpoint access failed.\n\nToken may be expired or invalid.');
}
} catch (error) {
hideLoading();
alert('❌ Network error while testing endpoint.');
}
}