Skip to content

Commit

Permalink
fix: move headless ais behind api routes (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew authored Jun 10, 2024
2 parents 563a432 + 228f9a8 commit e0d0b70
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/app/api/ais_auth/refresh/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { refreshUserSession, signInToCCXP } from "@/lib/headless_ais";
import { NextRequest, NextResponse } from "next/server";

export const POST = async (req: NextRequest) => {
const form = await req.formData();
const studentid = form.get("studentid");
const encryptedPassword = form.get("encryptedPassword");

if(!studentid || !encryptedPassword) return NextResponse.json({ error: { message: "Missing Student ID and Password" }}, { status: 400 })

return NextResponse.json(await refreshUserSession(studentid as string, encryptedPassword as string));
}
12 changes: 12 additions & 0 deletions src/app/api/ais_auth/signin/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { signInToCCXP } from "@/lib/headless_ais";
import { NextRequest, NextResponse } from "next/server";

export const POST = async (req: NextRequest) => {
const form = await req.formData();
const studentid = form.get("studentid");
const password = form.get("password");

if(!studentid || !password) return NextResponse.json({ error: { message: "Missing Student ID and Password" }}, { status: 400 })

return NextResponse.json(await signInToCCXP(studentid as string, password as string));
}
27 changes: 27 additions & 0 deletions src/helpers/headless_ais.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This is a temporary workaround to get headless ais to work
// as current server actions are failing

import type { signInToCCXP } from "@/lib/headless_ais";
import type { refreshUserSession } from "@/lib/headless_ais";

export const fetchSignInToCCXP = async (studentid: string, password: string) => {
const form = new FormData();
form.append("studentid", studentid);
form.append("password", password);
const res = await fetch('/api/ais_auth/signin', {
method: 'POST',
body: form
});
return await res.json() as ReturnType<Awaited<typeof signInToCCXP>>;
}

export const fetchRefreshUserSession = async (studentid: string, encryptedPassword: string) => {
const form = new FormData();
form.append("studentid", studentid);
form.append("encryptedPassword", encryptedPassword);
const res = await fetch('/api/ais_auth/refresh', {
method: 'POST',
body: form
});
return await res.json() as ReturnType<Awaited<typeof refreshUserSession>>;
}
10 changes: 5 additions & 5 deletions src/hooks/contexts/useHeadlessAIS.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use client";
"use client";;
import {HeadlessAISStorage, LoginError, UserJWT} from '@/types/headless_ais';
import { toast } from "@/components/ui/use-toast";
import { FC, PropsWithChildren, createContext, useContext, useEffect, useState } from "react";
import { useLocalStorage } from 'usehooks-ts';
import {refreshUserSession, signInToCCXP} from '@/lib/headless_ais';
import useDictionary from "@/dictionaries/useDictionary";
import { useCookies } from "react-cookie";
import { decode } from 'jsonwebtoken';
import {fetchRefreshUserSession, fetchSignInToCCXP} from '@/helpers/headless_ais';
const headlessAISContext = createContext<ReturnType<typeof useHeadlessAISProvider>>({
user: undefined,
ais: {
Expand Down Expand Up @@ -56,7 +56,7 @@ const useHeadlessAISProvider = () => {
return ;
}
setLoading(true);
return await signInToCCXP(username, password)
return await fetchSignInToCCXP(username, password)
.then((res) => {
if(!res) throw new Error("太多人在使用代理登入,請稍後再試");
if('error' in res) throw new Error(res.error.message);
Expand Down Expand Up @@ -108,7 +108,7 @@ const useHeadlessAISProvider = () => {
// legacy support, if encrypted password is not set, set it
if(!headlessAIS.encrypted) {
// use signInToCCXP to get encrypted password
return await signInToCCXP(headlessAIS.studentid, headlessAIS.password)
return await fetchSignInToCCXP(headlessAIS.studentid, headlessAIS.password)
.then((res) => {
if('error' in res) throw new Error(res.error.message);
setHeadlessAIS({
Expand All @@ -125,7 +125,7 @@ const useHeadlessAISProvider = () => {
})
}

return await refreshUserSession(headlessAIS.studentid, headlessAIS.password)
return await fetchRefreshUserSession(headlessAIS.studentid, headlessAIS.password)
.then((res) => {
if('error' in res) throw new Error(res.error.message);
setHeadlessAIS({
Expand Down

0 comments on commit e0d0b70

Please sign in to comment.