updated codegen, added client query generation, etc.

This commit is contained in:
lnn0q 2025-03-02 21:11:11 +02:00
parent 864f37a652
commit 9518787206
17 changed files with 5244 additions and 445 deletions

View file

@ -0,0 +1,25 @@
import bcrypt from 'bcrypt';
const usersDB = {
users: require('../model/users.json'),
setUsers: function (data) {
this.users = data;
}
};
const handleLogin = async (req, res) => {
const { user, pwd } = req.body;
if (!user || !pwd) res.status(400).json({ message: 'Username and password are required.' });
const foundUser = usersDB.users.find(person => person.username === user);
if (!foundUser) return res.sendStatus(401); // unauthorized
// evaluate password
const match = await bcrypt.compare(pwd, foundUser.password);
if (match) {
// create JWTs
res.json({ success: `User ${user} is logged in.` });
} else {
res.sendStatus(401);
}
};
module.exports = { handleLogin };

View file

@ -0,0 +1,49 @@
import bcrypt from 'bcrypt';
export async function SignUpResolver(_: any, input: any): Promise<any> {
// validate
// db
const users = [
{
id: '1',
email: 'joe@test.com',
username: 'Joe',
password: 'test'
}
];
const usersDB = { users };
// check for duplicate email+username
// encrypt the password
const hashedPwd = await bcrypt.hash(input.pwd, 10);
// store user in the db, return new user
return user;
}
const handleNewUser = async (req, res) => {
const { user, pwd } = req.body;
if (!user || !pwd)
return res.status(400).json({ message: 'Username and password are required.' });
// check for duplicate usernames in the db.
const duplicate = usersDB.users.find(person => person.username === user);
if (duplicate) return res.status(409);
try {
// encrypt the password
const hashedPwd = await brcypt.hash(pwd, 10);
// store the new user
const newUser = { username: user, password: hashedPwd };
usersDB.setUsers([...usersDB.users, newUser]);
await fs_promises.writeFile(
path.join(__dirname, '..', 'model', 'users.json'),
JSON.stringify(usersDB.users)
);
console.log(usersDB.users);
res.status(201).json({ success: `New user ${user} created!` });
} catch (err) {
res.status(500).json({ message: err.message });
}
};

View file

@ -1,10 +1,7 @@
import { BookInput, Book } from "@generated/graphql";
import { books } from "./booksManyResolver";
export async function BookOneResolver(
_: any,
{ ...input }: BookInput,
): Promise<Book> {
export async function BookOneResolver(_: any, input: BookInput): Promise<Book> {
console.log("Called 'BookOneResolver' resolver...");
const book = books.filter((item) => item.id === input.id)[0];

View file

@ -14,7 +14,7 @@ import { Query, Mutation, Subscription } from "./graphql/graphql";
const __dirname = import.meta.dirname;
const HOST = process.env.VITE_HOST || "localhost";
const PORT = parseInt(process.env.VITE_PORT || "3000");
const MOTD = `\n\n\n\x1b[35m-server is listening-\x1b[0m\n\nhttp://${HOST}:${PORT}\nhttp://localhost:${PORT}\n\n\n`;
const MOTD = `\n\n\n\x1b[35m-server is listening at...-\x1b[0m\n\nhttp://${HOST}:${PORT}\nhttp://localhost:${PORT}\n\n\n`;
const app = express();
const server = http.createServer(app);
@ -30,7 +30,11 @@ const resolvers = {
// Subscription,
};
const apollo = new ApolloServer({
interface IdentityContext {
authScope?: String;
}
const apollo = new ApolloServer<IdentityContext>({
typeDefs,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer: server })],
@ -42,7 +46,16 @@ app.use(
"/graphql",
cors<cors.CorsRequest>(),
express.json(),
expressMiddleware(apollo),
expressMiddleware(apollo, {
context: async ({ req, res }) => {
const token = req.headers.authorization || "";
// const user = await getUser(token);
const user = "user";
return { user };
},
}),
);
await new Promise<void>((resolve) =>