Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ GEMINI_API_KEY=your_gen_ai_key_here

# Server Port (optional, defaults to 3000)
PORT=3000

# Google OAuth 2.0 Client ID for Sign-In
GOOGLE_CLIENT_ID=your_google_client_id_here

# JWT Secret for Session Management
JWT_SECRET=your_jwt_secret_here
10 changes: 10 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const db = new sqlite3.Database(path.join(__dirname, 'studyplan.db'));

function initDb() {
db.serialize(() => {
// Users Table
db.run(`CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
name TEXT,
picture TEXT,
auth_provider TEXT DEFAULT 'local',
password TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`);

// Subjects Table
db.run(`CREATE TABLE IF NOT EXISTS subjects (
Expand Down
49 changes: 49 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<link rel="icon" type="image/x-icon" href="/public/favicon.ico" />
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script src="https://accounts.google.com/gsi/client" async defer></script>
</head>
<body>
<!-- Auth Modal -->
Expand All @@ -33,6 +34,9 @@ <h2 id="auth-title" style="margin:0 0 8px; font-size:22px; font-weight:700;">Wel
Sign In
</button>

<div style="margin: 16px 0; text-align: center; color: #666; font-size: 14px;">OR</div>
<div id="google-signin-button" style="display:flex; justify-content:center; margin-bottom: 12px;"></div>

<div style="font-size:12px; color:#666; margin-top:10px; line-height:1.6;">
Password must contain:
<ul style="padding-left:18px; margin-top:6px;">
Expand Down Expand Up @@ -501,6 +505,9 @@ <h3 style="font-size:12px; font-weight:700; text-transform:uppercase; color:var(
}

localStorage.setItem('studyplan_user', JSON.stringify({ email: data.email }));
if (data.token) {
localStorage.setItem('studyplan_token', data.token);
}
document.getElementById('auth-modal').style.display = 'none';

} catch (err) {
Expand All @@ -509,6 +516,47 @@ <h3 style="font-size:12px; font-weight:700; text-transform:uppercase; color:var(
}
});

// Google OAuth Callback
async function handleCredentialResponse(response) {
const token = response.credential;
const errorEl = document.getElementById('auth-error');
errorEl.style.display = 'none';
try {
const res = await fetch('/api/auth/google', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token })
});
const data = await res.json();
if (!res.ok) {
errorEl.textContent = data.error || 'Google login failed';
errorEl.style.display = 'block';
return;
}
localStorage.setItem('studyplan_user', JSON.stringify({ email: data.email }));
if (data.token) {
localStorage.setItem('studyplan_token', data.token);
}
document.getElementById('auth-modal').style.display = 'none';
} catch(err) {
errorEl.textContent = 'Network error during Google sign-in';
errorEl.style.display = 'block';
}
}

window.onload = function () {
if (window.google) {
google.accounts.id.initialize({
client_id: 'YOUR_GOOGLE_CLIENT_ID_HERE',
callback: handleCredentialResponse
});
google.accounts.id.renderButton(
document.getElementById('google-signin-button'),
{ theme: 'outline', size: 'large', width: 300 }
);
}
};

// Check if already logged in
if (localStorage.getItem('studyplan_user')) {
document.getElementById('auth-modal').style.display = 'none';
Expand All @@ -518,6 +566,7 @@ <h3 style="font-size:12px; font-weight:700; text-transform:uppercase; color:var(
const logoutBtn = document.getElementById('logout-btn');
logoutBtn.addEventListener('click', () => {
localStorage.removeItem('studyplan_user');
localStorage.removeItem('studyplan_token');

// Show login modal again
document.getElementById('auth-modal').style.display = 'flex';
Expand Down
72 changes: 69 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"cors": "^2.8.6",
"dotenv": "^17.4.2",
"express": "^5.2.1",
"google-auth-library": "^10.7.0",
"jsonwebtoken": "^9.0.3",
"sqlite3": "^6.0.1"
},
"engines": {
Expand Down
Loading