Skip to content

Commit

Permalink
Added development stuff and print details test
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew committed Nov 4, 2023
1 parent 987742d commit 39dc00b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
NEXT_PUBLIC_SUPABASE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJyb2xlIjogImFub24iLAogICJpc3MiOiAic3VwYWJhc2UiLAogICJpYXQiOiAxNjk1Mzk4NDAwLAogICJleHAiOiAxODUzMjUxMjAwCn0.IldZY_FwoLs6cXpVyas9fR_F3uLDWSXu70s3Uyvn-dI
NEXT_PUBLIC_SUPABASE_URL=https://sb.chibimello.com/
NEXTAUTH_URL=https://nthumods.com
NTHU_OAUTH_CLIENT_ID=nthumods
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXTAUTH_URL=http://localhost:3000
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXTAUTH_URL=https://nthumods.com
5 changes: 5 additions & 0 deletions src/app/[lang]/cds/CdsCourseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { normalizeRoomName } from '@/const/venues';
import { useMediaQuery } from 'usehooks-ts';
import { useSettings } from '@/hooks/contexts/settings';
import supabase, { CdsCourseDefinition } from '@/config/supabase';
import { useSession } from 'next-auth/react';

const createTimetableFromCdsCourses = (data: CdsCourseDefinition[], theme = 'tsinghuarian') => {
const newTimetableData: CourseTimeslotData[] = [];
Expand Down Expand Up @@ -92,6 +93,10 @@ const CdsCoursesForm: FC<{
const { timetableTheme } = useSettings();
const [displayToggles, setDisplayToggles] = useState<{ [key: string]: boolean }>({});
const scrollRef = useRef<HTMLDivElement>(null);

const session = useSession();
console.log(session)

const emptyFilters = {
textSearch: "",
level: [1, 2, 3, 4],
Expand Down
104 changes: 69 additions & 35 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,78 @@
import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"

const handler = NextAuth({
providers: [
{
id: "nthu",
name: "NTHU",
type: "oauth",
clientId: process.env.NTHU_OAUTH_CLIENT_ID,
clientSecret: process.env.NTHU_OAUTH_SECRET_KEY,
authorization: {
url: "https://oauth.ccxp.nthu.edu.tw/v1.1/authorize.php",
params: { scope: "inschool email userid name" }
},
token: "https://oauth.nthumods.com/v1.1/token.php",
userinfo: "https://oauth.nthumods.com/v1.1/resource.php",
profile(profile) {
if(profile.success == false) throw new Error("Failed to fetch user profile");
return {
id: profile.userid,
inschool: profile.inschool,
name_zh: profile.name,
name_en: profile.name_en,
email: profile.email,
}
},
session: { strategy: "jwt" },
providers: [
process.env.NODE_ENV === "development"
? CredentialsProvider({
id: "dev",
name: "Credentials",
credentials: {
username: {
label: "Username",
type: "text",
placeholder: "jsmith",
},
password: { label: "Password", type: "password" },
},
async authorize() {
return {
id: "b07901001",
inschool: true,
name: "王小明",
name_en: "Wang, Xiao-Ming",
email: "chewtzihwee@gmail.com"
}
],
callbacks: {
async jwt({ token, account, profile }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (account && profile) {
token.id = profile.id,
token.name = profile.name_zh,
token.name_en = profile.name_en,
token.inschool = profile.inschool,
token.email = profile.email
},
})
: {
id: "nthu",
name: "NTHU",
type: "oauth",
clientId: process.env.NTHU_OAUTH_CLIENT_ID,
clientSecret: process.env.NTHU_OAUTH_SECRET_KEY,
authorization: {
url: "https://oauth.ccxp.nthu.edu.tw/v1.1/authorize.php",
params: { scope: "inschool email userid name" }
},
token: "https://oauth.nthumods.com/v1.1/token.php",
userinfo: "https://oauth.nthumods.com/v1.1/resource.php",
profile(profile, tokens) {
if (profile.success == false) throw new Error("Failed to fetch user profile");
return {
id: profile.userid,
inschool: profile.inschool,
name_zh: profile.name,
name_en: profile.name_en,
email: profile.email,
}
},
}
],
callbacks: {
async jwt({ token, account, profile }) {
if (process.env.NODE_ENV == 'development') {
return {
...token,
id: "b07901001",
inschool: true,
name_zh: "王小明",
name_en: "Wang, Xiao-Ming",
email: "chewtzihwee@gmail.com"
}
return token
}
}

if (account && profile) {
token.id = profile.id,
token.name = profile.name_zh,
token.name_en = profile.name_en,
token.inschool = profile.inschool,
token.email = profile.email
}
return token
}
}
})

export { handler as GET, handler as POST }
Expand Down

0 comments on commit 39dc00b

Please sign in to comment.