initial commit
This commit is contained in:
25
server/api/getChannelById.get.ts
Normal file
25
server/api/getChannelById.get.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
if (!event.context.authenticated) return {
|
||||
message: 'You must be logged in to view a channel.'
|
||||
}
|
||||
|
||||
if (!event.context.params.channelId) {
|
||||
event.node.res.statusCode = 400;
|
||||
return {
|
||||
message: 'A channelId is required'
|
||||
}
|
||||
}
|
||||
|
||||
const server = await prisma.server.findFirst({
|
||||
where: {
|
||||
id: event.context.params.channelId
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
server
|
||||
}
|
||||
})
|
||||
44
server/api/login.post.ts
Normal file
44
server/api/login.post.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import bcryptjs from "bcryptjs";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
|
||||
if (!body.username || !body.password) {
|
||||
event.node.res.statusCode = 400;
|
||||
return {
|
||||
message: 'A username, and a password are required to login'
|
||||
}
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
username: body.username
|
||||
}
|
||||
})
|
||||
|
||||
const isCorrect = await bcryptjs.compare(body.password, user.passwordhash)
|
||||
|
||||
if (!isCorrect) {
|
||||
event.node.res.statusCode = 401;
|
||||
return {
|
||||
message: 'Incorrect username or password'
|
||||
}
|
||||
}
|
||||
|
||||
const token = uuidv4()
|
||||
|
||||
await prisma.session.create({
|
||||
data: {
|
||||
token,
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
token,
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
39
server/api/signup.post.ts
Normal file
39
server/api/signup.post.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import bcryptjs from "bcryptjs";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
|
||||
if (!body.username || !body.password || !body.email) {
|
||||
event.node.res.statusCode = 400;
|
||||
return {
|
||||
message: 'A username, a password, and an email address are required to singup'
|
||||
}
|
||||
}
|
||||
|
||||
const passwordhash = await bcryptjs.hash(body.password, 10)
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: body.username,
|
||||
passwordhash,
|
||||
email: body.email
|
||||
}
|
||||
})
|
||||
|
||||
const token = uuidv4()
|
||||
|
||||
await prisma.session.create({
|
||||
data: {
|
||||
token,
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
token,
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user