Skip to content

Commit

Permalink
refactor: fixed auth + commented out code
Browse files Browse the repository at this point in the history
  • Loading branch information
jesse-chou committed Feb 15, 2024
1 parent c44add7 commit 34115c8
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 178 deletions.
7 changes: 4 additions & 3 deletions app/_components/Sidebar/NavLinks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DashboardSideBar from "./SideBarList/DashboardSidebar";
import IncidentsSideBar from "./SideBarList/IncidentsSidebar";
import ConnectClusterSideBar from "./SideBarList/ConnectClusterSidebar";
import ConfigurationsSidebar from "./SideBarList/ConfigurationsSidebar";
// import ConfigurationsSidebar from "./SideBarList/ConfigurationsSidebar";

export default function NavLinks() {
return (
Expand All @@ -19,9 +19,10 @@ export default function NavLinks() {
<ConnectClusterSideBar />
</li>

<li>
{/* COMMENT BACK IN WHEN DEREK FINISHES CONFIGURATION PAGE */}
{/* <li>
<ConfigurationsSidebar />
</li>
</li> */}
</ul>
</>
);
Expand Down
91 changes: 91 additions & 0 deletions app/api/auth/[...nextauth]/authOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { NextAuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import GithubProvider from 'next-auth/providers/github'
import sql from '../../../utils/db'
import bcrypt from 'bcrypt'

export const authOptions: NextAuthOptions = {

providers: [
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string
}),

CredentialsProvider({
name: 'credentials',
credentials: {},

async authorize(credentials): Promise<any> {
const {email, password} = credentials as {email: string, password: string}

try {
//Checking to see if the user exists
let res = await sql`SELECT * FROM users WHERE email=${email}`
if (!res.length) {
return null
}
//Checking to see if the password is correct
const passwordsMatch = await bcrypt.compare(password, res[0].password)
if (!passwordsMatch) {
return null
}
return res[0]
}
catch(e) {
console.log(e)
return null
}
},
})
],
session: {
strategy: 'jwt',
maxAge: 2 * 60 * 60
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn:'/auth/login'
},
callbacks: {
async session({session, user}): Promise<any> {
if (!session) return
try {
//Identifying the userid in the db to put in the session.user object
let res = await sql`SELECT * FROM users WHERE email=${session.user.email!}`

session.user.userid = res[0].user_id
return session
}
catch(e) {
console.log(e)
return null
}
},

async signIn({profile, account}): Promise<any> {
//Checking to see if the user is using the Github oauth or local auth
if (account?.provider == 'credentials') {
return true
}
console.log(profile)

try {
//Checking to see if the user exists
//Typescript note: 'profile?.email!' has an explanation mark to ensure that this value will not be null
let res = await sql`SELECT * FROM users WHERE email=${profile?.email!}`
if (!res.length) {
//Inputing user into the db
await sql`INSERT INTO users (name, email) VALUES (${profile?.name as string}, ${profile?.email as string});`
}

return true
}
catch(e) {
console.log(e)
return
}

}
}
}
93 changes: 2 additions & 91 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,5 @@
import NextAuth, { NextAuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import GithubProvider from 'next-auth/providers/github'
import sql from '../../../utils/db'
import bcrypt from 'bcrypt'

export const authOptions: NextAuthOptions = {

providers: [
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string
}),

CredentialsProvider({
name: 'credentials',
credentials: {},

async authorize(credentials): Promise<any> {
const {email, password} = credentials as {email: string, password: string}

try {
//Checking to see if the user exists
let res = await sql`SELECT * FROM users WHERE email=${email}`
if (!res.length) {
return null
}
//Checking to see if the password is correct
const passwordsMatch = await bcrypt.compare(password, res[0].password)
if (!passwordsMatch) {
return null
}
return res[0]
}
catch(e) {
console.log(e)
return null
}
},
})
],
session: {
strategy: 'jwt',
maxAge: 2 * 60 * 60
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn:'/auth/login'
},
callbacks: {
async session({session, user}): Promise<any> {
if (!session) return
try {
//Identifying the userid in the db to put in the session.user object
let res = await sql`SELECT * FROM users WHERE email=${session.user.email!}`

session.user.userid = res[0].user_id
return session
}
catch(e) {
console.log(e)
return null
}
},

async signIn({profile, account}): Promise<any> {
//Checking to see if the user is using the Github oauth or local auth
if (account?.provider == 'credentials') {
return true
}
console.log(profile)

try {
//Checking to see if the user exists
//Typescript note: 'profile?.email!' has an explanation mark to ensure that this value will not be null
let res = await sql`SELECT * FROM users WHERE email=${profile?.email!}`
if (!res.length) {
//Inputing user into the db
await sql`INSERT INTO users (name, email) VALUES (${profile?.name as string}, ${profile?.email as string});`
}

return true
}
catch(e) {
console.log(e)
return
}

}
}
}
import { authOptions } from "./authOptions"
import NextAuth from "next-auth"

const handler = NextAuth(authOptions)

Expand Down
2 changes: 1 addition & 1 deletion app/api/connect-cluster/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextResponse} from 'next/server';
import sql from '../../utils/db';
import { getServerSession } from 'next-auth';
import { authOptions } from '../auth/[...nextauth]/route'
import { authOptions } from '../auth/[...nextauth]/authOptions'
import { activeCluster } from '../../lib/queries';

export async function POST(req: any) {
Expand Down
2 changes: 1 addition & 1 deletion app/api/get-cluster/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextResponse } from "next/server";
import sql from "../../utils/db";
import { getServerSession } from "next-auth";
import { authOptions } from "../auth/[...nextauth]/route";
import { authOptions } from "../auth/[...nextauth]/authOptions";

export async function GET(req: any) {
// This grabs the user id from the current logged in user
Expand Down
45 changes: 24 additions & 21 deletions app/api/updateCluster/delete/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import { NextRequest, NextResponse} from 'next/server';
import sql from '../../../../utils/db';
import { NextRequest, NextResponse } from "next/server";
import sql from "../../../../utils/db";

export async function GET(request: NextRequest, {params}: {params: {id: string}}) {

const {id} = params;
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const { id } = params;

try {

await sql`
update users set cluster_id=NULL where cluster_id=${id}
`
await sql`
delete from incidents where cluster_id=${id}
`
await sql`
await sql`
update users set cluster_id=NULL where cluster_id=${id}
`;
await sql`
delete from incidents where cluster_id=${id}
`;
await sql`
delete from clusters where cluster_id=${id}
`
`;
// TO-DO: BUILD LOGIC - IF INCIDENT IS DELETE, ALSO DELETE METRIC DATA
// await sql`
// delete from metric_data_id where cluster_id=${id}
// `;

return NextResponse.json({message: 'success'});

} catch(err) {
console.log('error', err)
return NextResponse.json({ message: "success" });
} catch (err) {
console.log("error", err);
return NextResponse.json({
message: `Error deleting cluster.`
message: `Error deleting cluster.`,
});
}
}

};
2 changes: 1 addition & 1 deletion app/auth/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import LoginPage from "../../_components/LoginPage";
import { getServerSession } from "next-auth";
import {redirect} from "next/navigation";
import {authOptions} from "../../api/auth/[...nextauth]/route";
import {authOptions} from "../../api/auth/[...nextauth]/authOptions";
import React from 'react';

export default async function Login() {
Expand Down
2 changes: 1 addition & 1 deletion app/auth/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SignUpPage from "../../_components/SignUpPage";
import { getServerSession } from "next-auth";
import {redirect} from "next/navigation"
import {authOptions} from "../../api/auth/[...nextauth]/route"
import {authOptions} from "../../api/auth/[...nextauth]/authOptions"

export default async function SignUp() {
const session = await getServerSession(authOptions)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion app/dashboard/incidents/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Table from '../../_components/Table'
import {redirect} from 'next/navigation';
import sql from '../../utils/db';
import {getServerSession} from 'next-auth';
import { authOptions } from "../../api/auth/[...nextauth]/route";
import { authOptions } from "../../api/auth/[...nextauth]/authOptions";



Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import LoadingSpinner from '../_components/homePage/loadingSpinner';
import { Suspense } from 'react'
import type { Metadata } from "next";
import { redirect } from "next/navigation"
import { authOptions } from '../api/auth/[...nextauth]/route';
import { authOptions } from '../api/auth/[...nextauth]/authOptions';
import { getServerSession } from 'next-auth';
import { Tab, TabList, TabGroup, TabPanel, TabPanels, Divider } from "@tremor/react";
import React from 'react';
Expand Down
Loading

0 comments on commit 34115c8

Please sign in to comment.