Integrate the new UI pages with the working backend for the rate_summary task. This also migrates some pages to properly link up authorization flows. Some cruft is still in

This commit is contained in:
Keith Stevens
2022-12-19 15:12:45 +09:00
parent 00ad927a2e
commit 2aacc542d3
13 changed files with 412 additions and 103 deletions
@@ -0,0 +1,38 @@
import NextAuth from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
import EmailProvider from "next-auth/providers/email";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "src/lib/prismadb";
export const authOptions = {
// Ensure we can store user data in a database.
adapter: PrismaAdapter(prisma),
providers: [
// Register a Discord auth method.
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
}),
// Register an email magic link auth method.
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
pages: {
signIn: "/auth/signin",
},
session: {
strategy: "jwt",
},
};
export default NextAuth(authOptions);
@@ -0,0 +1,71 @@
import { getToken } from "next-auth/jwt";
import { authOptions } from "src/pages/api/auth/[...nextauth]";
/**
* Returns a new task created from the Task Backend. We do a few things here:
*
* 1) Get the task from the backend and register the requesting user.
* 2) Store the task in our local database.
* 3) Send and Ack to the Task Backend with our local id for the task.
* 4) Return everything to the client.
*/
export default async (req, res) => {
const { task_type } = req.query;
const token = await getToken({ req });
// Return nothing if the user isn't registered.
if (!token) {
res.status(401).end();
return;
}
// Fetch the new task.
//
// This needs to be refactored into an easier to use library.
const taskRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/`, {
method: "POST",
headers: {
"X-API-Key": process.env.FASTAPI_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
type: task_type,
user: {
id: token.sub,
display_name: token.name || token.email,
auth_method: "local",
},
}),
});
const task = await taskRes.json();
// Store the task and link it to the user..
const registeredTask = await prisma.registeredTask.create({
data: {
task,
user: {
connect: {
id: token.sub,
},
},
},
});
// Update the backend with our Task ID
const ackRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/${task.id}/ack`, {
method: "POST",
headers: {
"X-API-Key": process.env.FASTAPI_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
post_id: registeredTask.id,
}),
});
const ack = await ackRes.json();
// Send the results to the client.
res.status(200).json(registeredTask);
};
+78
View File
@@ -0,0 +1,78 @@
import { getToken } from "next-auth/jwt";
import { authOptions } from "src/pages/api/auth/[...nextauth]";
/**
* Stores the task interaction with the Task Backend and then returns the next task generated.
*
* This implicity does a few things:
* 1) Stores the answer with the Task Backend.
* 2) Records the new task in our local database.
* 3) (TODO) Acks the new task with our local task ID to the Task Backend.
* 4) Returns the newly created task to the client.
*/
export default async (req, res) => {
const token = await getToken({ req });
// Return nothing if the user isn't registered.
if (!token) {
res.status(401).end();
return;
}
// Parse out the local task ID and the interaction contents.
const { id, content } = await JSON.parse(req.body);
// Log the interaction locally to create our user_post_id needed by the Task
// Backend.
const interaction = await prisma.taskInteraction.create({
data: {
content,
task: {
connect: {
id,
},
},
},
});
// Send the interaction to the Task Backend. This automatically fetches the
// next task in the sequence (or the done task).
const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/interaction`, {
method: "POST",
headers: {
"X-API-Key": process.env.FASTAPI_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
type: "post_rating",
user: {
id: token.sub,
display_name: token.name || token.email,
auth_method: "local",
},
post_id: id,
user_post_id: interaction.id,
...content,
}),
});
const newTask = await interactionRes.json();
// Stores the new task with our database.
const newRegisteredTask = await prisma.registeredTask.create({
data: {
task: newTask,
user: {
connect: {
id: token.sub,
},
},
},
});
// TODO: Ack the task with the Task Backend using the newly created local
// task ID.
// Send the next task in the sequence to the client.
res.status(200).json(newRegisteredTask);
};