more refactoring

This commit is contained in:
2024-07-02 09:08:49 +02:00
parent de148eaf19
commit 1971966099
27 changed files with 400 additions and 324 deletions
-16
View File
@@ -1,16 +0,0 @@
'server only'
import { hash, compare } from "bcrypt";
export async function validatePassword(password:string, hashString:string){
const result = await compare(password, hashString);
return result;
}
export async function hashPassword(password:string){
const hashString = await hash(password, 10);
return hashString;
}
export default { validatePassword, hashPassword };
+1 -20
View File
@@ -1,25 +1,6 @@
'server only'
import { ReactNode } from "react";
import Gens from "./gens";
// import Auth from "./Auth";
function getAPIEnv(){
return {
'schema': process.env.API_SCHEMA,
'host': process.env.API_HOST,
'port': process.env.API_PORT,
'basepath': process.env.API_BASEPATH
};
}
function constructAPIUrl(endpoint:string){
const { schema, host, port, basepath } = getAPIEnv();
return `${schema}://${host}:${port}/${basepath}/${endpoint}`
}
function constructUrl(endpoint:string){
const { schema, host, port, basepath } = getAPIEnv();
return `${schema}://${host}:${port}/${endpoint}`
}
function truncateString(str:string = '', num:number = 255) {
if (str.length > num) {
return str.slice(0, num) + "...";
@@ -29,5 +10,5 @@ function truncateString(str:string = '', num:number = 255) {
}
export { Gens, constructAPIUrl, constructUrl, truncateString }
export { Gens, truncateString }
export const aifa = (a: ReactNode, b: ReactNode) => (a ? a : b);
+2
View File
@@ -0,0 +1,2 @@
export * from './error';
export * from './user';
+34 -24
View File
@@ -1,34 +1,44 @@
// import { MUser } from "@/model/sequelize/User";
// import { MAuth } from "@/model/sequelize/Auth";
import { validatePassword } from "@/util/Auth";
import { validatepassword } from "@/util/auth";
import { APIError } from "@/util/api/error";
import { User } from "@/models";
export function parseBasicAuth(authb64:string):UserAuth
{
const authString:string = Buffer.from(authb64.split(" ")[1] as any, "base64").toString("utf8");
var userAuth:UserAuth = {
username:authString.split(":")[0] as any,
password:authString.split(":")[1] as any
};
return userAuth
export function parseBasicAuth(authb64: string): UserAuth {
const authString: string = Buffer.from(
authb64.split(" ")[1] as any,
"base64"
).toString("utf8");
var userAuth: UserAuth = {
username: authString.split(":")[0] as any,
password: authString.split(":")[1] as any,
};
return userAuth;
}
export type UserAuth = {
username: string,
password: string
username: string;
password: string;
};
export async function getAssociatedUser(auth: UserAuth) {
let foundUser = await User.findOne({
attributes: { include: ["password"] },
where: { username: auth.username },
});
if (!foundUser)
throw new APIError({
status: 401,
responseText: "Unauthorized: Invalid Username",
});
if (!(await validatepassword(auth.password, foundUser.password)))
throw new APIError({
status: 401,
responseText: "Unauthorized: Invalid Password",
});
return foundUser;
}
export async function getAssociatedUser(auth:UserAuth)
{
let foundUser = await User.findOne({ attributes: {include: ['password']}, where: {username: auth.username} });
if (!foundUser)
throw new APIError({status: 401, responseText:"Unauthorized: Invalid Username"});
if (!(await validatePassword(auth.password, foundUser.password)))
throw new APIError({status: 401, responseText:"Unauthorized: Invalid Password"});
return foundUser;
}
+8
View File
@@ -0,0 +1,8 @@
'server only'
import { hash } from "bcrypt";
export async function hashpassword(password:string){
const hashString = await hash(password, 10);
return hashString;
}
+2
View File
@@ -0,0 +1,2 @@
export * from './hashpassword';
export * from './validatepassword';
+9
View File
@@ -0,0 +1,9 @@
'server only';
import { compare } from "bcrypt";
export async function validatepassword(password: string, hashString: string) {
const result = await compare(password, hashString);
return result;
}
+10
View File
@@ -0,0 +1,10 @@
'server only';
// import Auth from "./Auth";
export function getAPIEnv() {
return {
'schema': process.env.API_SCHEMA,
'host': process.env.API_HOST,
'port': process.env.API_PORT,
'basepath': process.env.API_BASEPATH
};
}
+10
View File
@@ -0,0 +1,10 @@
export * from './api';
export * from './auth';
export * from './Cookies';
export * from './DeepPartial';
export * from './gens';
export * from './getAPIEnv';
export * from './parseSetCookie';
export * from './state';
export * from './url';
export * from './utils';
+2
View File
@@ -0,0 +1,2 @@
export * from './statetypes';
export * from './stateutils';
+1 -9
View File
@@ -1,12 +1,4 @@
import { Dispatch, SetStateAction } from "react";
export type StateHook<T> = {
state: T;
setState: Dispatch<SetStateAction<T>>;
};
export type StateHookArray<T> = [
T,Dispatch<SetStateAction<T>>
]
import { StateHookArray, StateHook } from "./statetypes";
export function parseStateHook<T>(hook:StateHookArray<T>):StateHook<T>{
const stateHook:Partial<StateHook<T>> = {};
+9
View File
@@ -0,0 +1,9 @@
import { Dispatch, SetStateAction } from "react";
export type StateHook<T> = {
state: T;
setState: Dispatch<SetStateAction<T>>;
};
export type StateHookArray<T> = [
T,Dispatch<SetStateAction<T>>
]
+1
View File
@@ -0,0 +1 @@
export * from "./urlConstructor"
+12
View File
@@ -0,0 +1,12 @@
'use server'
import { getAPIEnv } from "../getAPIEnv";
export function constructAPIUrl(endpoint:string){
const { schema, host, port, basepath } = getAPIEnv();
return `${schema}://${host}:${port}/${basepath}/${endpoint}`
}
export function constructUrl(endpoint:string){
const { schema, host, port, basepath } = getAPIEnv();
return `${schema}://${host}:${port}/${endpoint}`
}1