-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: fixed auth + commented out code
- Loading branch information
1 parent
c44add7
commit 34115c8
Showing
12 changed files
with
198 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`, | ||
}); | ||
} | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.