-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
410 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.