Skip to content

Commit 82e5617

Browse files
committed
feat: added @kgpian.iitkgp.ac.in verification on frontend and backend
1 parent 4dd5039 commit 82e5617

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

backend/src/api/handlers/auth.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ pub async fn google_auth_callback(
107107

108108
let claims = token_data.claims;
109109

110+
// Validate email domain - only allow @kgpian.iitkgp.ac.in
111+
if !claims.email.ends_with("@kgpian.iitkgp.ac.in") {
112+
return Err(crate::api::errors::AuthError::BadResponse(
113+
"Only @kgpian.iitkgp.ac.in email addresses are allowed to sign in".to_string()
114+
).into());
115+
}
116+
110117
let user_info = GoogleUserInfo {
111118
google_id: claims.sub,
112119
email: claims.email,

dev-startup.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ create_directories() {
194194

195195
# Default paths if not set in .env
196196
STATIC_STORAGE="${STATIC_FILE_STORAGE_LOCATION:-${HOME}/static}"
197-
LOG_DIR="${LOG_LOCATION:-${HOME}/log}"
197+
# Extract directory from LOG_LOCATION file path (e.g., /path/to/backend.log -> /path/to)
198+
LOG_FILE="${LOG_LOCATION:-${HOME}/log/backend.log}"
199+
LOG_DIR="$(dirname "${LOG_FILE}")"
198200

199201
# Create directories
200202
mkdir -p "${STATIC_STORAGE}/cfmn/notes/uploaded"

frontend/src/contexts/AuthContext.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({
150150
use_fedcm_for_prompt: false,
151151
ux_mode: 'popup',
152152
context: 'signin',
153+
// Restrict to @kgpian.iitkgp.ac.in domain only
154+
hd: 'kgpian.iitkgp.ac.in',
153155
// One Tap specific configurations
154156
itp_support: true,
155157
state_cookie_domain: window.location.hostname,
@@ -294,10 +296,27 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({
294296
} else {
295297
const errorText = await result.text();
296298
console.error('Authentication failed:', result.status, errorText);
297-
throw new Error(`Authentication failed: ${result.status}`);
299+
300+
// Check if it's an email domain restriction error
301+
let errorMessage = `Authentication failed: ${result.status}`;
302+
try {
303+
const errorData = JSON.parse(errorText);
304+
if (errorData.error && errorData.error.includes('@kgpian.iitkgp.ac.in')) {
305+
errorMessage = 'Only @kgpian.iitkgp.ac.in email addresses are allowed. Please sign in with your institutional email.';
306+
} else if (errorData.error) {
307+
errorMessage = errorData.error;
308+
}
309+
} catch {
310+
// If JSON parsing fails, use the status-based message
311+
}
312+
313+
throw new Error(errorMessage);
298314
}
299315
} catch (error) {
300-
console.error('Authentication error:', error instanceof Error ? error.message : String(error));
316+
const errorMsg = error instanceof Error ? error.message : String(error);
317+
console.error('Authentication error:', errorMsg);
318+
// Display user-friendly error message
319+
alert(errorMsg);
301320
} finally {
302321
setIsLoading(false);
303322
}

0 commit comments

Comments
 (0)