Skip to content

Commit

Permalink
Merge pull request #76 from HungrySlugs-CSE115A/db_rework
Browse files Browse the repository at this point in the history
made database functions follow CRUD
  • Loading branch information
IanHollow authored May 13, 2024
2 parents dc3f363 + ca731cc commit 0457aea
Showing 20 changed files with 415 additions and 528 deletions.
11 changes: 1 addition & 10 deletions app/[dh_choice]/page.tsx
Original file line number Diff line number Diff line change
@@ -166,17 +166,11 @@ export default function Page({ searchParams }) {

return (
<main>

<div className="container mx-auto">
<h1 className="font-semibold py-5 text-4xl text-[#003C6C]">
{searchParams.name}
</h1>






{/* Search button */}
<div>
<button onClick={handleSearch}>Search</button>
@@ -185,10 +179,7 @@ export default function Page({ searchParams }) {
{/* Categories */}
{categories.map((category, i) => (
<div key={i}>
<Accordion
category={category}
isOpen={expandedCategory === i}
/>
<Accordion category={category} isOpen={expandedCategory === i} />
</div>
))}
</div>
1 change: 0 additions & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
@@ -26,7 +26,6 @@ export default function RootLayout({
<body className={monst.className}>
<Navbar height={navbarHeight} />
<div className="" style={{ paddingTop: navbarHeight }}>

{children}
</div>
</body>
73 changes: 39 additions & 34 deletions app/loginPage/page.tsx
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
"use client";
import React, { useContext, useEffect, useState } from "react";
import { GoogleOAuthProvider, useGoogleLogin, googleLogout, TokenResponse} from "@react-oauth/google";
import {
GoogleOAuthProvider,
useGoogleLogin,
googleLogout,
TokenResponse,
} from "@react-oauth/google";
import { jwtDecode } from "jwt-decode";
import axios from "axios";




interface User {
name: string;
email: string;
picture: string;
}
const LoginPage = () => {
return(
<GoogleOAuthProvider clientId={'1040494859138-vji3ddfil5jancg23ifaginvmn71hktf.apps.googleusercontent.com'}>
<LoginComponent/>
return (
<GoogleOAuthProvider
clientId={
"1040494859138-vji3ddfil5jancg23ifaginvmn71hktf.apps.googleusercontent.com"
}
>
<LoginComponent />
</GoogleOAuthProvider>
)
}

);
};

const LoginComponent = () => {
const [user, setUser] = useState<User | null>(null);




useEffect(() => {
console.log("LoginPage component mounted");
}, []);


const handleLoginSuccess = (tokenResponse: any) => {
if ('code' in tokenResponse) {
if ("code" in tokenResponse) {
// Handle authorization code flow
console.log('Authorization Code:', tokenResponse.code);
console.log("Authorization Code:", tokenResponse.code);
// Exchange code for tokens here

// Store authentication token in the browser's local storage for navigation bar use
localStorage.setItem('token', tokenResponse.code)
localStorage.setItem("token", tokenResponse.code);
// Redirect the user to main page
window.location.href = '/';
window.location.href = "/";
} else {
// Handle implicit flow
console.log('Token Received:', tokenResponse.access_token);
console.log("Token Received:", tokenResponse.access_token);
const decoded: User = jwtDecode(tokenResponse.id_token);
setUser({
name: decoded.name,
email: decoded.email,
picture: decoded.picture
picture: decoded.picture,
});
// Send token to backend if necessary
axios.post('http://localhost:8000/myapi/users/google-oauth2/', { token: tokenResponse.access_token })
.then(res => console.log('Backend login successful', res))
.catch(err => console.error('Backend login failed', err));
axios
.post("http://localhost:8000/myapi/users/google-oauth2/", {
token: tokenResponse.access_token,
})
.then((res) => console.log("Backend login successful", res))
.catch((err) => console.error("Backend login failed", err));
}


};
const handleLogin = useGoogleLogin({
flow: "auth-code",

onSuccess: (tokenResponse) => {
handleLoginSuccess(tokenResponse);
console.log('Logged in successfully');
console.log("Logged in successfully");
},
onError: (errorResponse) => console.error('Login Failed', errorResponse),
onError: (errorResponse) => console.error("Login Failed", errorResponse),
});

// const handleLogout = () => {
@@ -75,13 +77,16 @@ const LoginComponent = () => {
// // Remove the token from local storage
// localStorage.removeItem('token');
// // Redirect the user to the login page
// window.location.href = '/loginPage';
// window.location.href = '/loginPage';
// console.log('Logged out successfully');
// };

return (
<div>
<button onClick={() => handleLogin()} className="hover:underline decoration-yellow-400 underline-offset-8 m-5 p-2 text-[#003C6C] font-medium text-xl">
<button
onClick={() => handleLogin()}
className="hover:underline decoration-yellow-400 underline-offset-8 m-5 p-2 text-[#003C6C] font-medium text-xl"
>
Login with Google
</button>
{/* <button onClick={handleLogout} className="hover:underline decoration-yellow-400 underline-offset-8 top-0 right-0 m-5 p-2 text-[#003C6C] font-medium text-xl">
@@ -90,13 +95,13 @@ const LoginComponent = () => {
{user && (
<div>
<img src={user.picture} alt="User profile" />
<h2>Welcome, {user.name} - {user.email}</h2>
<h2>
Welcome, {user.name} - {user.email}
</h2>
</div>
)}
</div>

);
};


export default LoginPage;
export default LoginPage;
Loading
Oops, something went wrong.

0 comments on commit 0457aea

Please sign in to comment.