Mark-Lasfar
commited on
Commit
·
cd321ac
1
Parent(s):
1f3defe
Update authorize for google & github
Browse files- main.py +29 -8
- templates/login.html +16 -47
main.py
CHANGED
|
@@ -114,17 +114,38 @@ app.include_router(
|
|
| 114 |
tags=["users"],
|
| 115 |
)
|
| 116 |
app.include_router(
|
| 117 |
-
fastapi_users.get_oauth_router(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
prefix="/auth/google",
|
| 119 |
tags=["auth"],
|
| 120 |
)
|
| 121 |
app.include_router(
|
| 122 |
-
fastapi_users.get_oauth_router(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
prefix="/auth/github",
|
| 124 |
tags=["auth"],
|
| 125 |
)
|
| 126 |
app.include_router(api_router)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# Custom middleware for handling 404 and 500 errors
|
| 129 |
class NotFoundMiddleware(BaseHTTPMiddleware):
|
| 130 |
async def dispatch(self, request: Request, call_next):
|
|
@@ -144,12 +165,12 @@ app.add_middleware(NotFoundMiddleware)
|
|
| 144 |
@app.exception_handler(GetIdEmailError)
|
| 145 |
async def handle_oauth_error(request: Request, exc: GetIdEmailError):
|
| 146 |
logger.error(f"OAuth error: {exc}")
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
# Root endpoint for homepage
|
| 155 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 114 |
tags=["users"],
|
| 115 |
)
|
| 116 |
app.include_router(
|
| 117 |
+
fastapi_users.get_oauth_router(
|
| 118 |
+
google_oauth_client,
|
| 119 |
+
auth_backend,
|
| 120 |
+
JWT_SECRET,
|
| 121 |
+
redirect_url="/auth/google/callback" # Updated to callback endpoint
|
| 122 |
+
),
|
| 123 |
prefix="/auth/google",
|
| 124 |
tags=["auth"],
|
| 125 |
)
|
| 126 |
app.include_router(
|
| 127 |
+
fastapi_users.get_oauth_router(
|
| 128 |
+
github_oauth_client,
|
| 129 |
+
auth_backend,
|
| 130 |
+
JWT_SECRET,
|
| 131 |
+
redirect_url="/auth/github/callback" # Updated to callback endpoint
|
| 132 |
+
),
|
| 133 |
prefix="/auth/github",
|
| 134 |
tags=["auth"],
|
| 135 |
)
|
| 136 |
app.include_router(api_router)
|
| 137 |
|
| 138 |
+
# Custom OAuth callbacks to redirect to /chat
|
| 139 |
+
@app.get("/auth/google/callback", response_class=RedirectResponse)
|
| 140 |
+
async def google_oauth_callback(request: Request):
|
| 141 |
+
logger.info(f"Processing Google OAuth callback: {request.url}")
|
| 142 |
+
return RedirectResponse(url="/chat", status_code=302)
|
| 143 |
+
|
| 144 |
+
@app.get("/auth/github/callback", response_class=RedirectResponse)
|
| 145 |
+
async def github_oauth_callback(request: Request):
|
| 146 |
+
logger.info(f"Processing GitHub OAuth callback: {request.url}")
|
| 147 |
+
return RedirectResponse(url="/chat", status_code=302)
|
| 148 |
+
|
| 149 |
# Custom middleware for handling 404 and 500 errors
|
| 150 |
class NotFoundMiddleware(BaseHTTPMiddleware):
|
| 151 |
async def dispatch(self, request: Request, call_next):
|
|
|
|
| 165 |
@app.exception_handler(GetIdEmailError)
|
| 166 |
async def handle_oauth_error(request: Request, exc: GetIdEmailError):
|
| 167 |
logger.error(f"OAuth error: {exc}")
|
| 168 |
+
error_message = "Failed to authenticate with OAuth. Please try again or contact support."
|
| 169 |
+
return templates.TemplateResponse(
|
| 170 |
+
"login.html",
|
| 171 |
+
{"request": request, "error": error_message},
|
| 172 |
+
status_code=400
|
| 173 |
+
)
|
| 174 |
|
| 175 |
# Root endpoint for homepage
|
| 176 |
@app.get("/", response_class=HTMLResponse)
|
templates/login.html
CHANGED
|
@@ -104,12 +104,12 @@
|
|
| 104 |
</button>
|
| 105 |
</form>
|
| 106 |
<div class="flex justify-center gap-4 mt-4 flex-wrap">
|
| 107 |
-
<
|
| 108 |
Login with Google <i class="bx bxl-google ml-2"></i>
|
| 109 |
-
</
|
| 110 |
-
<
|
| 111 |
Login with GitHub <i class="bx bxl-github ml-2"></i>
|
| 112 |
-
</
|
| 113 |
</div>
|
| 114 |
<p class="mt-4">Don't have an account? <a href="/register" class="text-emerald-300 hover:underline">Register</a></p>
|
| 115 |
<p id="errorMsg" class="text-red-500 mt-4 hidden"></p>
|
|
@@ -194,6 +194,8 @@
|
|
| 194 |
const loginBtn = document.getElementById('loginBtn');
|
| 195 |
const spinner = document.getElementById('spinner');
|
| 196 |
const errorMsg = document.getElementById('errorMsg');
|
|
|
|
|
|
|
| 197 |
loginForm.addEventListener('submit', async (e) => {
|
| 198 |
e.preventDefault();
|
| 199 |
spinner.classList.remove('hidden');
|
|
@@ -216,60 +218,27 @@
|
|
| 216 |
spinner.classList.add('hidden');
|
| 217 |
errorMsg.textContent = 'An error occurred. Please try again.';
|
| 218 |
errorMsg.classList.remove('hidden');
|
|
|
|
| 219 |
}
|
| 220 |
});
|
|
|
|
|
|
|
| 221 |
function showCardDetails(cardId) {
|
| 222 |
document.getElementById(`${cardId}-details`).classList.remove('hidden');
|
| 223 |
}
|
|
|
|
| 224 |
function closeCardDetails(cardId) {
|
| 225 |
document.getElementById(`${cardId}-details`).classList.add('hidden');
|
| 226 |
}
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
try {
|
| 230 |
-
const response = await fetch('/auth/google/authorize');
|
| 231 |
-
const data = await response.json();
|
| 232 |
-
if (data.authorization_url) {
|
| 233 |
-
window.location.href = data.authorization_url;
|
| 234 |
-
} else {
|
| 235 |
-
console.error('No authorization URL received');
|
| 236 |
-
errorMsg.textContent = 'Error starting Google login. Try again.';
|
| 237 |
-
errorMsg.classList.remove('hidden');
|
| 238 |
-
}
|
| 239 |
-
} catch (error) {
|
| 240 |
-
console.error('Error initiating Google login:', error);
|
| 241 |
-
errorMsg.textContent = 'Error starting Google login. Try again.';
|
| 242 |
-
errorMsg.classList.remove('hidden');
|
| 243 |
-
}
|
| 244 |
-
});
|
| 245 |
-
// GitHub Login
|
| 246 |
-
document.getElementById('githubLoginBtn').addEventListener('click', async () => {
|
| 247 |
-
try {
|
| 248 |
-
const response = await fetch('/auth/github/authorize');
|
| 249 |
-
const data = await response.json();
|
| 250 |
-
if (data.authorization_url) {
|
| 251 |
-
window.location.href = data.authorization_url;
|
| 252 |
-
} else {
|
| 253 |
-
console.error('No authorization URL received');
|
| 254 |
-
errorMsg.textContent = 'Error starting GitHub login. Try again.';
|
| 255 |
-
errorMsg.classList.remove('hidden');
|
| 256 |
-
}
|
| 257 |
-
} catch (error) {
|
| 258 |
-
console.error('Error initiating GitHub login:', error);
|
| 259 |
-
errorMsg.textContent = 'Error starting GitHub login. Try again.';
|
| 260 |
-
errorMsg.classList.remove('hidden');
|
| 261 |
-
}
|
| 262 |
-
});
|
| 263 |
-
// تسجيل Service Worker لتفعيل الـ PWA
|
| 264 |
if ('serviceWorker' in navigator) {
|
| 265 |
navigator.serviceWorker.register('/static/js/sw.js')
|
| 266 |
-
.then(
|
| 267 |
-
|
| 268 |
-
}).catch(function(err) {
|
| 269 |
-
console.error('❌ Service Worker registration failed', err);
|
| 270 |
-
});
|
| 271 |
}
|
| 272 |
-
|
|
|
|
| 273 |
let deferredPrompt;
|
| 274 |
window.addEventListener('beforeinstallprompt', (e) => {
|
| 275 |
e.preventDefault();
|
|
|
|
| 104 |
</button>
|
| 105 |
</form>
|
| 106 |
<div class="flex justify-center gap-4 mt-4 flex-wrap">
|
| 107 |
+
<a id="googleLoginBtn" href="/auth/google" class="inline-flex items-center bg-gradient-to-r from-white to-gray-200 text-gray-800 px-6 py-3 rounded-full font-semibold hover:scale-105 transition-transform">
|
| 108 |
Login with Google <i class="bx bxl-google ml-2"></i>
|
| 109 |
+
</a>
|
| 110 |
+
<a id="githubLoginBtn" href="/auth/github" class="inline-flex items-center bg-gradient-to-r from-gray-800 to-black text-white px-6 py-3 rounded-full font-semibold hover:scale-105 transition-transform">
|
| 111 |
Login with GitHub <i class="bx bxl-github ml-2"></i>
|
| 112 |
+
</a>
|
| 113 |
</div>
|
| 114 |
<p class="mt-4">Don't have an account? <a href="/register" class="text-emerald-300 hover:underline">Register</a></p>
|
| 115 |
<p id="errorMsg" class="text-red-500 mt-4 hidden"></p>
|
|
|
|
| 194 |
const loginBtn = document.getElementById('loginBtn');
|
| 195 |
const spinner = document.getElementById('spinner');
|
| 196 |
const errorMsg = document.getElementById('errorMsg');
|
| 197 |
+
|
| 198 |
+
// Handle email/password login
|
| 199 |
loginForm.addEventListener('submit', async (e) => {
|
| 200 |
e.preventDefault();
|
| 201 |
spinner.classList.remove('hidden');
|
|
|
|
| 218 |
spinner.classList.add('hidden');
|
| 219 |
errorMsg.textContent = 'An error occurred. Please try again.';
|
| 220 |
errorMsg.classList.remove('hidden');
|
| 221 |
+
console.error('Error during login:', error);
|
| 222 |
}
|
| 223 |
});
|
| 224 |
+
|
| 225 |
+
// Handle card details toggle
|
| 226 |
function showCardDetails(cardId) {
|
| 227 |
document.getElementById(`${cardId}-details`).classList.remove('hidden');
|
| 228 |
}
|
| 229 |
+
|
| 230 |
function closeCardDetails(cardId) {
|
| 231 |
document.getElementById(`${cardId}-details`).classList.add('hidden');
|
| 232 |
}
|
| 233 |
+
|
| 234 |
+
// Service Worker for PWA
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
if ('serviceWorker' in navigator) {
|
| 236 |
navigator.serviceWorker.register('/static/js/sw.js')
|
| 237 |
+
.then(reg => console.log('✅ Service Worker Registered', reg))
|
| 238 |
+
.catch(err => console.error('❌ Service Worker registration failed', err));
|
|
|
|
|
|
|
|
|
|
| 239 |
}
|
| 240 |
+
|
| 241 |
+
// Handle PWA install prompt
|
| 242 |
let deferredPrompt;
|
| 243 |
window.addEventListener('beforeinstallprompt', (e) => {
|
| 244 |
e.preventDefault();
|