Skip to content

Commit

Permalink
implement login and settings load
Browse files Browse the repository at this point in the history
  • Loading branch information
makinbacon21 committed Mar 28, 2024
1 parent fcd1c06 commit 4fde701
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 111 deletions.
10 changes: 10 additions & 0 deletions app/NextAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use client" // SessionProvider needs to wrap around a client component to be used in layout.tsx, cannot be used directly in layout.tsx
import { SessionProvider } from "next-auth/react";

type Props = {
children?: React.ReactNode;
};

export const NextAuthProvider = ({ children }: Props) => {
return <SessionProvider>{children}</SessionProvider>;
};
7 changes: 7 additions & 0 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const dynamic = 'force-dynamic' // defaults to auto
import NextAuth from "next-auth";
import { config } from "../../../../lib/auth";

const handler = NextAuth(config)

export { handler as GET, handler as POST }
23 changes: 13 additions & 10 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Nav from '@/components/nav'
import './globals.css'
import Script from 'next/script'
import { NextAuthProvider } from './NextAuthProvider';

export const metadata = {
title: 'Cygnet',
Expand All @@ -13,15 +14,17 @@ export default function RootLayout({
children: React.ReactNode
}) {
return (
<html lang="en">
<body className="bg">
<Nav />
<div className="container mont">
{children}
</div>
</body>
{/*@ts-ignore*/}
<Script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"/>
</html>
<NextAuthProvider>
<html lang="en">
<body className="bg">
<Nav />
<div className="container mont">
{children}
</div>
</body>
{/*@ts-ignore*/}
<Script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous" />
</html>
</NextAuthProvider>
)
}
44 changes: 33 additions & 11 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import 'bootstrap/dist/css/bootstrap.css';
import './globals.css';
import { Suspense } from 'react';
import { headers } from 'next/headers';
import SearchBar from '@/components/searchbar';
import CardBody from '@/components/cardbody';
import PageBody from '@/components/pagebody';
import { auth } from '@/lib/auth';
import { signIn } from 'next-auth/react';
import { notFound } from 'next/navigation';

function IP() {
const FALLBACK_IP_ADDRESS = '0.0.0.0'
const forwardedFor = headers().get('x-forwarded-for')

if (forwardedFor) {
return forwardedFor.split(',')[0] ?? FALLBACK_IP_ADDRESS
}

return headers().get('x-real-ip') ?? FALLBACK_IP_ADDRESS
}

export default async function Home({ searchParams }: {
searchParams?: {
Expand All @@ -12,14 +26,22 @@ export default async function Home({ searchParams }: {
}
}) {

return (
<>
<Suspense>
<SearchBar />
</Suspense>
<Suspense>
<PageBody searchParams={searchParams} />
</Suspense>
</>
);
// check ip or authentication
const clientIPArr = IP().split('.');
if((clientIPArr[0].includes('130') && clientIPArr[1] === '58') ||
(clientIPArr[0].includes('172')) || await auth()) {
return (
<>
<Suspense>
<SearchBar />
</Suspense>
<Suspense>
<PageBody searchParams={searchParams} />
</Suspense>
</>
);
}

// otherwise sign in
notFound();
}
159 changes: 109 additions & 50 deletions app/settings/page.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,122 @@
'use client'
import Card from '@/components/card';
import { StudentInfo } from '@/components/pagebody';
import { DbInfo, StudentInfo } from '@/components/pagebody';
import { auth } from '@/lib/auth';
import fs from 'fs';
import 'bootstrap/dist/css/bootstrap.css';
import ForceSignin from '@/components/forceSignIn';
import { notFound, redirect } from 'next/navigation';
import { queryDb } from '../queryDb';

// will need to be loaded from our own database eventually,
// just UI functional for now

const user_data:StudentInfo = {
last: "Fettig",
first: "Nicholas",
year: "2026",
dorm: "MERTZ",
room: "125",
id: '',
photo_path: '',
pronouns: ''
async function getUser(id: string | undefined) {
if (!id) {
notFound();
}

console.log("UID: " + id);
const file = fs.readFileSync(`${__dirname}/../../../../student_settings/student_settings.json`, 'utf8');
const user_settings = JSON.parse(file);

// @ts-ignore
const raw: any[] = await queryDb(`SELECT FIRST_NAME, LAST_NAME, GRAD_YEAR, DORM, DORM_ROOM \
FROM student_data WHERE USER_ID='${id}' `);

const student: DbInfo = raw[0];

let path = '/placeholder.jpg';

if (!(user_settings[0]['PHOTO_HIDDEN'].includes(id))) {
const modPath = `/photos/mod/${id}_m.jpg`;
const genModPath = `${__dirname}/../../../..${modPath}`;
console.log(genModPath)

// production needs domain due to external static server
// dev uses next public dir b/c doesn't need build-time copy
const prePath = (process.env.NODE_ENV === "production") ? process.env.DOMAIN : "";
const staticPath = `/photos/${id}.jpg`;
const genPath = `${__dirname}/../../../../${staticPath}`;

if (fs.existsSync(genModPath)) {
path = prePath + modPath
}
else if (fs.existsSync(genPath)) {
//path = fullPath
path = prePath + staticPath
} else {
const imgBuffer = await queryDb(`SELECT PHOTO FROM student_data WHERE USER_ID='${id}' `)

// @ts-ignore
fs.writeFileSync(genPath, imgBuffer[0]['PHOTO']);
// path = fullPath
path = prePath + staticPath
}
}

const user_data: StudentInfo = {
first: student['FIRST_NAME'],
last: student['LAST_NAME'],
year: student['GRAD_YEAR'],
dorm: student['DORM'],
room: student['DORM_ROOM'],
id: id,
photo_path: path,
pronouns: ""
}

return user_data;
}

export default function Settings(){
return (
<div className="d-inline-flex w-full margin-spacing">
<div className="w-25">
<Card {...user_data} />
</div>
<div className="flex-grow-1 bg-white p-4 shadow-sm ml-2 rounded-lg mont">
<h1 className="h4">Edit Profile</h1>
<div className="d-inline-flex w-75 mt-3">
<div className="w-full mr-2">
<label className="h6 d-block">First Name</label>
<input
className = "py-1 px-2 w-full d-block"
placeholder={user_data.first} />
</div>
<div className="w-full ml-2">
<label className="h6 d-block">Last Name</label>
<input
className = "py-1 px-2 w-full d-block"
placeholder={user_data.last} />
export default async function Settings() {
// await auth check
const session = (await auth());
if (session?.user) {
const user_data = await getUser(session.user.email?.split('@')[0]);
return (
<div className="d-inline-flex w-full margin-spacing container">
<div className="row">
<div className="col-12 col-md-4 h-100 w-full">
<Card {...user_data} />
</div>
<div className="w-full ml-2">
<label className="h6 d-block">Pronouns</label>
<input
className = "py-1 px-2 w-75 d-block"
placeholder={""/*prounouns will go in here eventually*/} />
<div className="col-12 col-md-8 h-100 bg-white pt-3 pb-2 px-4 shadow-sm rounded-lg mont">
<h1 className="h4">Edit Profile</h1>
<div className="d-inline-flex w-75 mt-3">
<div className="w-full mr-2">
<label className="h6 d-block">First Name</label>
<input
className="py-1 px-2 w-full d-block"
placeholder={user_data.first} />
</div>
<div className="w-full ml-2">
<label className="h6 d-block">Last Name</label>
<input
className="py-1 px-2 w-full d-block"
placeholder={user_data.last} />
</div>
<div className="w-full ml-2">
<label className="h6 d-block">Pronouns</label>
<input
className="py-1 px-2 w-75 d-block"
placeholder={""/*prounouns will go in here eventually*/} />
</div>
</div>
<div className="form-check form-switch mt-4">
<input className="form-check-input" type="checkbox" />
<label className="mont">Show Dorm</label>
</div>
<div className="form-check form-switch mt-3">
<input className="form-check-input" type="checkbox" />
<label className="mont">Show Picture</label>
</div>
<div className="form-check form-switch mt-3">
<input className="form-check-input" type="checkbox" />
<label className="mont">Show Profile</label>
</div>
</div>
</div>
<div className="form-check form-switch mt-4">
<input className="form-check-input" type="checkbox"/>
<label className="mont">Show Dorm</label>
</div>
<div className="form-check form-switch mt-3">
<input className="form-check-input" type="checkbox"/>
<label className="mont">Show Picture</label>
</div>
<div className="form-check form-switch mt-3">
<input className="form-check-input" type="checkbox"/>
<label className="mont">Show Profile</label>
</div>
</div>
</div>
)
)
}
return (<ForceSignin />)
}
40 changes: 18 additions & 22 deletions components/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,28 @@ export default function Card(props: StudentInfo | any) {
//probably can just use props -abhi
if (props.id) {
return (
<div className="col-6 col-md-4 col-lg-3 gy-4">
<div className="align-items-center bg-white rounded-lg pt-3 pb-2 cont shadow-sm">
<UserImage
photo_path={props.photo_path}
//NEWPHOTO = {NEWPHOTO}
/> {/*sourced from where?*/}
<UserInfo
first={props.first}
last={props.last}
year={props.year}
dorm={props.dorm}
room={props.room}
id={props.id}
photo_path={props.photo_path}
//NEWPHOTO = {NEWPHOTO}
/>
</div>
<div className="align-items-center bg-white rounded-lg pt-3 pb-2 cont shadow-sm">
<UserImage
photo_path={props.photo_path}
//NEWPHOTO = {NEWPHOTO}
/> {/*sourced from where?*/}
<UserInfo
first={props.first}
last={props.last}
year={props.year}
dorm={props.dorm}
room={props.room}
id={props.id}
photo_path={props.photo_path}
//NEWPHOTO = {NEWPHOTO}
/>
</div>
)
} else {
return (
<div className="col-6 col-md-4 col-lg-3 gy-4">
<div className="align-items-center bg-white rounded-lg pt-3 pb-2 cont shadow-sm">
<UserImage />
<UserInfo />
</div>
<div className="align-items-center bg-white rounded-lg pt-3 pb-2 cont shadow-sm">
<UserImage />
<UserInfo />
</div>
)
}
Expand Down
Loading

0 comments on commit 4fde701

Please sign in to comment.