Rework authentication
This commit is contained in:
parent
a85330e8cf
commit
c07d33bcc9
17 changed files with 317 additions and 128 deletions
|
|
@ -1,17 +1,23 @@
|
|||
import { type FastifyReply, type FastifyRequest } from "fastify";
|
||||
import type {
|
||||
ILoginRequest,
|
||||
IRegisterResponseError,
|
||||
IRegisterResponseSuccess,
|
||||
IRegisterRequest,
|
||||
ILoginResponseError,
|
||||
ILoginResponseSuccess,
|
||||
IPostLoginRequest,
|
||||
IPostRegisterResponseError,
|
||||
IPostRegisterResponseSuccess,
|
||||
IPostRegisterRequest,
|
||||
IPostLoginResponseError,
|
||||
IPostLoginResponseSuccess,
|
||||
IGetRefreshResponseError,
|
||||
IGetRefreshResponseSuccess,
|
||||
} from "./types.js";
|
||||
import { loginUser, registerUser } from "../../services/auth/auth.js";
|
||||
import {
|
||||
loginUser,
|
||||
refreshSession,
|
||||
registerUser,
|
||||
} from "../../services/auth/auth.js";
|
||||
import { API_ERROR } from "../errors.js";
|
||||
|
||||
const postRegister = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { username, password, email } = request.body as IRegisterRequest;
|
||||
const postRegister = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { username, password, email } = request.body as IPostRegisterRequest;
|
||||
|
||||
const newUser = await registerUser({
|
||||
username: username,
|
||||
|
|
@ -20,20 +26,21 @@ const postRegister = async (request: FastifyRequest, _reply: FastifyReply) => {
|
|||
});
|
||||
|
||||
if (!newUser) {
|
||||
reply.status(409);
|
||||
return {
|
||||
error: API_ERROR.USER_ALREADY_EXISTS,
|
||||
} as IRegisterResponseError;
|
||||
} as IPostRegisterResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
id: newUser.id,
|
||||
username: newUser.username,
|
||||
registerDate: newUser.registerDate?.getTime(),
|
||||
} as IRegisterResponseSuccess;
|
||||
} as IPostRegisterResponseSuccess;
|
||||
};
|
||||
|
||||
const postLogin = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { username, password } = request.body as ILoginRequest;
|
||||
const postLogin = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const { username, password } = request.body as IPostLoginRequest;
|
||||
|
||||
const session = await loginUser({
|
||||
username: username,
|
||||
|
|
@ -41,17 +48,44 @@ const postLogin = async (request: FastifyRequest, _reply: FastifyReply) => {
|
|||
});
|
||||
|
||||
if (!session) {
|
||||
reply.status(403);
|
||||
return {
|
||||
username: username,
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as ILoginResponseError;
|
||||
} as IPostLoginResponseError;
|
||||
}
|
||||
|
||||
reply.setCookie("token", session.cookie, {
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
sameSite: "none",
|
||||
secure: true,
|
||||
maxAge: 60 * 60 * 24 * 365 * 100,
|
||||
});
|
||||
|
||||
return {
|
||||
id: session.id,
|
||||
ownerId: session.userId,
|
||||
token: session.token,
|
||||
} as ILoginResponseSuccess;
|
||||
} as IPostLoginResponseSuccess;
|
||||
};
|
||||
|
||||
export { postRegister, postLogin };
|
||||
const getRefresh = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const cookie = request.cookies["token"];
|
||||
|
||||
const refresh = await refreshSession(cookie);
|
||||
|
||||
if (!refresh) {
|
||||
reply.status(403);
|
||||
return {
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as IGetRefreshResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
id: refresh[0].id,
|
||||
ownerId: refresh[0].userId,
|
||||
token: refresh[1],
|
||||
} as IGetRefreshResponseSuccess;
|
||||
};
|
||||
|
||||
export { postRegister, postLogin, getRefresh };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue