Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
SaBhr committed Jun 18, 2023
1 parent 13dc79f commit 50b835d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 63 deletions.
5 changes: 4 additions & 1 deletion webApp/Frontend/riot_frontend/components/LoginButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import { useRouter } from 'next/router';
const LoginButton = () => {
const { data: session } = useSession();
const router = useRouter();
const storedUser = JSON.parse(localStorage.getItem('user'));
const userName = storedUser.firstName;

return (
<div className="ml-auto flex gap-2">
{session?.user ? (
<>
<p className="text-sky-600"> {session.user.name}</p>
{/* <p className="text-sky-600"> {session.user.firstName}</p> */}
<p className="text-sky-600"> {userName}</p>
<button
className="text-blue-500 hover:underline ml-1"
onClick={async () => {
Expand Down
2 changes: 1 addition & 1 deletion webApp/Frontend/riot_frontend/components/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Sidebar = ({ children }) => {
<BiMap size={20}/>
</div>
</Link>
<Link href='/about'>
<Link href='/signin'>
<div className='bg-gray-100 hover:bg-gray-200 cursor-pointer my-4 p-3 rounded-lg inline-block'>
<AiOutlineInfoCircle size={20}/>
</div>
Expand Down
32 changes: 23 additions & 9 deletions webApp/Frontend/riot_frontend/pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default NextAuth({
},
async authorize(credentials, req) {
// Handle the case when 'credentials' is undefined
if (!credentials) {
/* if (!credentials) {
throw new Error("Invalid credentials");
}
} */
// Mock user lookup logic for testing
const mockUser = {
/* const mockUser = {
id: 1,
name: "John Smith",
email: "[email protected]",
Expand All @@ -29,18 +29,32 @@ export default NextAuth({
return mockUser;
} else {
throw new Error("Invalid credentials");
}
} */

extract_user = response => {

console.log("message!");
let response_body_obj = JSON.parse(response.payload); // Turn the response body JSON into an object

let access_jwt_str = response_body_obj?.access; // Get the access token (JWT) from body object

let jwt_payload_base64 = access_jwt_str?.split('.')[1]; // Split the jwt at every '.' and use first index to get Base64 encoded payload (JWT => header.payload.signature => 0.1.2)

let jwt_payload = atob(jwt_payload_base64); // Decode the Base64 encoded payload

let user = JSON.parse(jwt_payload); // Turn the decoded payload JSON string into the an object

return user;

}

const res = await fetch("http://127.0.0.1:8000/api/signIn", {
const res = await fetch("http://178.128.192.215:8000/api/signIn", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: credentials?.username,
password: credentials?.password,
body: JSON.stringify({ //constructs the request body
email: credentials?.username, //data to be sent in the request body
password: credentials?.password, //data to be sent in the request body
}),
});
const user = await res.json();
Expand Down
15 changes: 14 additions & 1 deletion webApp/Frontend/riot_frontend/pages/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import Providers from "@/components/Providers";
//import ContainerProblem from "./containerProblem";

export default function Home() {
/* const storedUser = JSON.parse(localStorage.getItem('user'));
if (storedUser){const userName = storedUser.firstName;} */

return (
<>
<Head>
Expand All @@ -21,7 +24,17 @@ export default function Home() {

<main className=' bg-malibu-500 min-h-screen'>
<div className='flex justify-between px-4 pt-4'>
<Providers><LoginButton /></Providers>
{/* <Providers><LoginButton /></Providers> */}
<div className="ml-auto flex gap-2">
<p className="text-sky-600">User</p>
<button
className="text-blue-500 hover:underline ml-1"
onClick={async () => {
await signOut();
router.push('/about');
}}> Sign Out
</button>
</div>
</div>

<TopCards/>
Expand Down
81 changes: 36 additions & 45 deletions webApp/Frontend/riot_frontend/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,47 @@ export default function Home() {

return (
<div class="bg-scroll bg-sky-gif min-h-screen">
<div className='rounded-lg'>
<div className='pt-4'>
<h2 className="font-bold text-center text-3xl">WELCOME TO</h2>
{/* <h2 className="font-bold text-center text-3xl">TO</h2> */}

<div className="text-center w-full">
<span className='font-bold text-blue-700 hover:text-blue-800 lg:pr-10 md:pr-8 sm:pr-6 pr-4 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl'>R</span>
<span className='font-bold text-blue-600 hover:text-blue-800 lg:pr-10 md:pr-8 sm:pr-6 pr-4 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl'>I</span>
<span className='font-bold text-blue-500 hover:text-blue-800 lg:pr-10 md:pr-8 sm:pr-6 pr-4 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl'>O</span>
<span className='font-bold text-blue-400 hover:text-blue-800 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl' >T</span>
</div>
</div>

<div className= "absolute ml-20 inset-x-0 bottom-0">
<div className='rounded-lg'>
<div className='pt-4'>
<h2 className="font-bold text-center text-3xl">WELCOME TO</h2>
{/* <h2 className="font-bold text-center text-3xl">TO</h2> */}

{/*Welcome and RIOT -------------------------------------------- */}

{/*-------------------------------------------- */}
{/*Boat Gif-------------------------------------------- */}
<div>
<Image src={boatGif} class="mx-auto"/>
</div>
{/*-------------------------------------------- */}
{/*Sign in and Sign up Buttons-------------------------------------------- */}
<div className="flex items-center justify-center">
<div className='bg-malibu-500 w-1/3 p-4 rounded-lg'>
<button className="bg-blue-200 w-full hover:bg-blue-300 flex justify-center font-bold items-center lg:p-4 p-2 rounded-lg" onClick={() => signIn()}>
Sign In
</button>
</div>
</div>
<div className="text-center w-full">
<span className='font-bold text-blue-700 hover:text-blue-800 lg:pr-10 md:pr-8 sm:pr-6 pr-4 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl'>R</span>
<span className='font-bold text-blue-600 hover:text-blue-800 lg:pr-10 md:pr-8 sm:pr-6 pr-4 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl'>I</span>
<span className='font-bold text-blue-500 hover:text-blue-800 lg:pr-10 md:pr-8 sm:pr-6 pr-4 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl'>O</span>
<span className='font-bold text-blue-400 hover:text-blue-800 shrink sm:text-4xl md:text-6xl lg:text-9xl text-xl' >T</span>
</div>
</div>
<div className= "absolute ml-20 inset-x-0 bottom-0">

<div className="flex items-center justify-center">
<div className='bg-malibu-500 w-1/3 p-4 rounded-lg'>

<Link href = '/signup' className="bg-blue-200 hover:bg-blue-300 flex justify-center items-center lg:p-4 p-2 rounded-lg cursor-pointer ">
<p className="font-bold">Sign Up</p>
</Link>

</div>
{/*Welcome and RIOT -------------------------------------------- */}

{/*-------------------------------------------- */}
{/*Boat Gif-------------------------------------------- */}
<div>
<Image src={boatGif} class="mx-auto"/>
</div>
{/*-------------------------------------------- */}
{/*Sign in and Sign up Buttons-------------------------------------------- */}
<div className="flex items-center justify-center">
<div className='bg-malibu-500 w-1/3 p-4 rounded-lg'>
<button className="bg-blue-200 w-full hover:bg-blue-300 flex justify-center font-bold items-center lg:p-4 p-2 rounded-lg" onClick={() => signIn()}>
Sign In
</button>
</div>
{/*-------------------------------------- */}



</div>

</div>


<div className="flex items-center justify-center">
<div className='bg-malibu-500 w-1/3 p-4 rounded-lg'>
<Link href = '/signin_page' className="bg-blue-200 hover:bg-blue-300 flex justify-center items-center lg:p-4 p-2 rounded-lg cursor-pointer ">
<p className="font-bold">Sign Up</p>
</Link>
</div>
</div>
{/*-------------------------------------- */}
</div>
</div>
</div>
);
}
47 changes: 41 additions & 6 deletions webApp/Frontend/riot_frontend/pages/signin_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,63 @@ import Button from "../components/Button";
import TextBox from "../components/TextBox";
import { signIn } from "next-auth/react";
import React, { useRef } from "react";
import styles from '../styles/Signin.module.css'

const LoginPage = () => {
const userName = useRef("");
const pass = useRef("");

const onSubmit = async () => {
/* const onSubmit = async () => {
const result = await signIn("credentials", {
username: userName.current,
email: userName.current,
password: pass.current,
redirect: true,
callbackUrl: "/dashboard",
});
}; */

const onSubmit = async () => {
console.log("Login Pressed!");
const res = await fetch("http://178.128.192.215:80/api/signIn/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: userName.current, //username entered by the user in textbox
password: pass.current, //password entered by the user in textbox
}), //"email": "[email protected]", password: "fr"
});
console.log("Email: " + userName.current);
const response_body = await res.json(); // Parse the response body as JSON
if (res.ok) {
const user = extract_user(response_body); // Call the extract_user function with the response body
window.location.href = "/dashboard";
localStorage.setItem('user', JSON.stringify(user));
return user;
} else {
console.log("Response not OK!");
return null;
}
};

const extract_user = (response) => {
let access_jwt_str = response?.access;
let jwt_payload_base64 = access_jwt_str?.split('.')[1];
let jwt_payload = Buffer.from(jwt_payload_base64, 'base64'); //equivalent to atob
let user = JSON.parse(jwt_payload);
return user;
};

return (
<div
className={
"flex flex-col justify-center items-center h-screen bg-gradient-to-br gap-1 from-cyan-300 to-sky-600"
}
>
<div className="px-7 py-4 shadow bg-white rounded-md flex flex-col gap-2">
<TextBox lableText="User Name" onChange={(e) => (userName.current = e.target.value)} />
<TextBox
> <h1 className={styles.signin_header}>Sign in</h1>
<div className="{styles.signinnew} px-7 py-4 shadow bg-white rounded-md flex flex-col gap-2">
<TextBox className={styles['form-group']} lableText="Email" onChange={(e) => (userName.current = e.target.value)} />
<TextBox className={styles['form-group']}
lableText="Password"
type={"password"}
onChange={(e) => (pass.current = e.target.value)}
Expand Down

0 comments on commit 50b835d

Please sign in to comment.