Boilerplate generator and deno tasks for monorepo

This commit is contained in:
Emma
2023-05-01 20:28:18 -06:00
parent a367562ba0
commit a910783882
36 changed files with 1164 additions and 47 deletions

28
user-service/data.ts Normal file
View File

@@ -0,0 +1,28 @@
import mongoose, { Schema, SchemaTypes } from 'mongoose';
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
import { genSalt } from 'https://deno.land/x/bcrypt@v0.4.1/src/main.ts';
import { configDatabase } from 'lib/data.ts';
configDatabase(mongoose, 'CGGUsers');
const userSchema = new Schema({
username: {
type: SchemaTypes.String,
required: true,
unique: true
},
password: {
type: SchemaTypes.String,
required: true
}
})
userSchema.methods.generateHash = async (pw: string) => {
return await bcrypt.hash(pw, await genSalt(8));
}
userSchema.methods.validatePassword = async function(pw:string) {
return await bcrypt.compare(pw, this.password)
}
export const User = mongoose.model('User', userSchema);

49
user-service/index.ts Normal file
View File

@@ -0,0 +1,49 @@
import { Router } from 'oak';
import { CGGService } from 'cgg/Application.ts';
import { createUserSessionToken } from 'lib/jwt.ts';
import { USER_TOKEN } from '../constsAndEnums.ts';
import { User } from './data.ts';
import { bodyExists } from '../middleware/bodyExists.ts';
const app = new CGGService({ prefix: '/users' });
app.route(new Router()
.post('/signup', bodyExists, async ctx => {
const userData = await ctx.request.body({ type: 'json' }).value;
console.log(userData);
const user = new User({
username: userData.username
});
user.password = await (user as any).generateHash(userData.password);
await user.save();
ctx.response.status = 200;
})
.post('/signin', bodyExists, async ctx => {
const userData = await ctx.request.body({ type: 'json' }).value;
const user = await User.findOne({username: userData.username});
const pwValid = user && await (user as any).validatePassword(userData.password);
if (user && pwValid) {
ctx.cookies.set(USER_TOKEN, await createUserSessionToken({username: user.username}))
ctx.response.body = `User ${user.username} signed in`;
} else {
ctx.response.status = 403;
ctx.response.body = "How dare you???"
}
})
.get('/me', ctx => {
ctx.response.body = ctx.state.user?.username || 'You are not signed in';
})
.get('/:id', ctx => {
ctx.response.body = ctx.params.id;
})
)
// .post('/signup', ctx => {
// // ctx.
// }))
// app.route(routes)
// app.use(routes.routes());
// app.use(routes.allowedMethods());
app.start();
console.log('User service running on ' + Deno.args.at(0));

5
user-service/perms Normal file
View File

@@ -0,0 +1,5 @@
--allow-read
--allow-write
--allow-net
--allow-sys
--allow-env

1
user-service/prefix Normal file
View File

@@ -0,0 +1 @@
users