Skip to content

Commit

Permalink
update auth routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 6a1568c commit 5a1b050
Show file tree
Hide file tree
Showing 23 changed files with 410 additions and 438 deletions.
119 changes: 119 additions & 0 deletions src/v2/routes/auth/account-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { AppHandler } from "../handler"
import { UserAuthenticationManager } from "@/v2/lib/managers/auth/user-auth-manager"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"

const createAccountSchema = z.object({
username: z.string().min(3).max(32).openapi({
description: "The username of the user.",
example: "user",
}),
email: z.string().min(3).max(32).openapi({
description: "The email of the user.",
example: "[email protected]",
}),
password: z.string().min(8).max(64).openapi({
description: "The password of the user.",
example: "password1234",
}),
passwordConfirmation: z.string().min(8).max(64).openapi({
description: "The password confirmation of the user.",
example: "password1234",
}),
})

const createAccountResponseSchema = z.object({
success: z.literal(true),
})

const userCreateAccountRoute = createRoute({
path: "/create",
method: "post",
description: "Create a new user account with an email and password.",
tags: ["Auth"],
request: {
body: {
content: {
"application/json": {
schema: createAccountSchema,
},
},
},
},
responses: {
200: {
description: "Returns true.",
content: {
"application/json": {
schema: createAccountResponseSchema,
},
},
},
...GenericResponses,
},
})

export const UserCreateAccountRoute = (handler: AppHandler) => {
handler.openapi(userCreateAccountRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)

const { user } = await authSessionManager.validateSession()

if (user) {
return ctx.json(
{
success: false,
message: "Already logged in",
},
401
)
}

const { email, password, username } = ctx.req.valid("json")

const userAuthManager = new UserAuthenticationManager(ctx)

const existingUser = false

if (existingUser) {
return ctx.json(
{
success: false,
message: "User already exists with that email",
},
400
)
}

const newLoginCookie = await userAuthManager.createAccount(
{
email,
username,
},
password
)

if (!newLoginCookie) {
return ctx.json(
{
success: false,
message: "Failed to create account",
},
500
)
}

ctx.header("Set-Cookie", newLoginCookie.serialize(), {
append: true,
})

return ctx.json(
{
success: true,
},
200
)
})
}
100 changes: 100 additions & 0 deletions src/v2/routes/auth/account-login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { UserAuthenticationManager } from "@/v2/lib/managers/auth/user-auth-manager"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"

const loginSchema = z.object({
email: z.string().min(3).max(32).openapi({
description: "The email of the user.",
example: "[email protected]",
}),
password: z.string().min(8).max(64).openapi({
description: "The password of the user.",
example: "password1234",
}),
passwordConfirmation: z.string().min(8).max(64).openapi({
description: "The password confirmation of the user.",
example: "password1234",
}),
})

const loginResponseSchema = z.object({
success: z.literal(true),
})

export const userLoginRoute = createRoute({
path: "/login",
method: "post",
description: "Login to a user with an email and password.",
tags: ["Auth"],
request: {
body: {
content: {
"application/json": {
schema: loginSchema,
},
},
},
},
responses: {
200: {
description: "Returns true.",
content: {
"application/json": {
schema: loginResponseSchema,
},
},
},
...GenericResponses,
},
})

export const UserLoginRoute = (handler: AppHandler) => {
handler.openapi(userLoginRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)

const { user } = await authSessionManager.validateSession()

if (user) {
return ctx.json(
{
success: false,
message: "Already logged in",
},
401
)
}

const { email, password } = ctx.req.valid("json")

const userAuthManager = new UserAuthenticationManager(ctx)

const newLoginCookie = await userAuthManager.loginViaPassword(
email,
password
)

if (!newLoginCookie) {
return ctx.json(
{
success: false,
message: "Invalid credentials",
},
401
)
}

ctx.header("Set-Cookie", newLoginCookie.serialize(), {
append: true,
})

return ctx.json(
{
success: true,
},
200
)
})
}
30 changes: 0 additions & 30 deletions src/v2/routes/auth/create/openapi.ts

This file was deleted.

69 changes: 0 additions & 69 deletions src/v2/routes/auth/create/route.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/v2/routes/auth/create/schema.ts

This file was deleted.

Loading

0 comments on commit 5a1b050

Please sign in to comment.