Compare commits
3 Commits
5f97af3f96
...
0410cc8655
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0410cc8655 | ||
|
|
d829857378 | ||
|
|
8c6ef435b6 |
26
.vscode/settings.json
vendored
26
.vscode/settings.json
vendored
@ -8,8 +8,28 @@
|
||||
"**/CVS": true,
|
||||
"**/.DS_Store": true,
|
||||
"**/Thumbs.db": true,
|
||||
".**": true,
|
||||
"node_modules": true,
|
||||
"**/.next": true,
|
||||
"**/.sqlite_queries": true,
|
||||
"**/node_modules": true,
|
||||
"**/.env.example": true,
|
||||
"**/.vscode": true,
|
||||
"**/.env**": true,
|
||||
"**/.gitignore": true,
|
||||
"**/.eslintrc.json": true,
|
||||
"**/next-env.d.ts": true,
|
||||
"**/package-lock.json": true,
|
||||
"**/package.json": true,
|
||||
"**/bucket": true,
|
||||
|
||||
},
|
||||
"hide-files.files": [],
|
||||
"exportall.config.folderListener": [
|
||||
"/src/util/api",
|
||||
"/src/util/cookies",
|
||||
"/src/util",
|
||||
"/src/util/textgen"
|
||||
],
|
||||
"exportall.config.relExclusion": [
|
||||
"/src/util/url",
|
||||
"/src/util/api"
|
||||
],
|
||||
}
|
||||
@ -1,129 +1,150 @@
|
||||
'use server'
|
||||
"use server";
|
||||
|
||||
import { constructAPIUrl } from "@/util/url";
|
||||
import { cookies } from "next/headers"
|
||||
import { parseSetCookie } from "@/util/parseSetCookie";
|
||||
import makeFetchCookie from 'fetch-cookie';
|
||||
import { cookies } from "next/headers";
|
||||
import { parseSetCookie } from "@/util/cookies";
|
||||
import makeFetchCookie from "fetch-cookie";
|
||||
import fetchCookie from "fetch-cookie";
|
||||
import { Attribute, Attributes } from "@sequelize/core";
|
||||
import { User, Auth } from "@/models";
|
||||
import { AuthProps } from "@/providers/providers";
|
||||
import { ActionResult } from "./ActionResult";
|
||||
|
||||
type LoginReturn = {
|
||||
cookie?:unknown,
|
||||
errorMessage?:string;
|
||||
}
|
||||
cookie?: unknown;
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
async function attemptAPILogin(method:string,formData:FormData):Promise<LoginReturn|null>
|
||||
{
|
||||
async function attemptAPILogin(
|
||||
method: string,
|
||||
formData: FormData
|
||||
): Promise<LoginReturn | null> {
|
||||
// Check if form data is present with required fields, return null if not
|
||||
if(!formData || !formData.get('input_username') || !formData.get('input_password')) return null;
|
||||
if (
|
||||
!formData ||
|
||||
!formData.get("input_username") ||
|
||||
!formData.get("input_password")
|
||||
)
|
||||
return null;
|
||||
|
||||
// Instantiate header object
|
||||
let headers:Headers = new Headers();
|
||||
let headers: Headers = new Headers();
|
||||
|
||||
// Prepare fetchCookie
|
||||
const { CookieJar, Cookie } = fetchCookie.toughCookie;
|
||||
const jar = new CookieJar()
|
||||
const jar = new CookieJar();
|
||||
const fetchWithCookie = makeFetchCookie(fetch, jar);
|
||||
|
||||
// Set Basic Auth
|
||||
headers.set('Authorization', `Basic ${Buffer.from(`${formData.get('input_username')}:${formData.get('input_password')}`).toString('base64')}`);
|
||||
headers.set(
|
||||
"Authorization",
|
||||
`Basic ${Buffer.from(
|
||||
`${formData.get("input_username")}:${formData.get(
|
||||
"input_password"
|
||||
)}`
|
||||
).toString("base64")}`
|
||||
);
|
||||
let res = await fetchWithCookie(constructAPIUrl("auth"), {
|
||||
method:'POST',
|
||||
credentials: 'include',
|
||||
headers:headers,
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: headers,
|
||||
});
|
||||
|
||||
console.log(jar.store.idx['localhost']['/']);
|
||||
console.log(jar.store.idx["localhost"]["/"]);
|
||||
|
||||
let koek = res.headers.getSetCookie();
|
||||
|
||||
let cookieDict = parseSetCookie(koek);
|
||||
|
||||
await cookies().set('auth', cookieDict.auth);
|
||||
await cookies().set("auth", cookieDict.auth);
|
||||
return {
|
||||
cookie:cookieDict.auth,
|
||||
errorMessage:""
|
||||
cookie: cookieDict.auth,
|
||||
errorMessage: "",
|
||||
};
|
||||
// console.log(koek);
|
||||
}
|
||||
|
||||
export async function serverAttemptAuthenticateUser(_currentState: unknown, formData: FormData):Promise<LoginReturn|null>
|
||||
{
|
||||
export async function serverAttemptAuthenticateUser(
|
||||
_currentState: unknown,
|
||||
formData: FormData
|
||||
): Promise<LoginReturn | null> {
|
||||
try {
|
||||
const signInStatus = await attemptAPILogin('credentials', formData)
|
||||
const signInStatus = await attemptAPILogin("credentials", formData);
|
||||
return signInStatus;
|
||||
} catch (error:any) {
|
||||
} catch (error: any) {
|
||||
if (error) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin': return { errorMessage: 'invalidCredentials' };
|
||||
default: return { errorMessage: 'Something went wrong.' };
|
||||
case "CredentialsSignin":
|
||||
return { errorMessage: "invalidCredentials" };
|
||||
default:
|
||||
return { errorMessage: "Something went wrong." };
|
||||
}
|
||||
}
|
||||
throw Error
|
||||
throw Error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function serverValidateSessionCookie(koek:string):Promise<boolean>
|
||||
{
|
||||
const validateSession = await fetch(constructAPIUrl("auth/validate"),{
|
||||
method:"POST",
|
||||
headers:{
|
||||
Cookie: `auth=${koek};`
|
||||
}
|
||||
export async function serverValidateSessionCookie(
|
||||
koek: string
|
||||
): Promise<boolean> {
|
||||
const validateSession = await fetch(constructAPIUrl("auth/validate"), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: `auth=${koek};`,
|
||||
},
|
||||
});
|
||||
if(validateSession.status == 200)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
if (validateSession.status == 200) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
export async function userIsAdmin():Promise<boolean>
|
||||
{
|
||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
||||
const cookieAuthSanitized = cookieAuthValue? JSON.parse(JSON.stringify(cookieAuthValue)) : "";
|
||||
export async function userIsAdmin(): Promise<boolean> {
|
||||
const cookieAuthValue = await cookies().get("auth")?.value;
|
||||
const cookieAuthSanitized = cookieAuthValue
|
||||
? JSON.parse(JSON.stringify(cookieAuthValue))
|
||||
: "";
|
||||
|
||||
if(!cookieAuthSanitized) return false;
|
||||
if (!cookieAuthSanitized) return false;
|
||||
const parsedAuth = JSON.parse(cookieAuthSanitized);
|
||||
|
||||
if(!parsedAuth.id || !parsedAuth.token || !parsedAuth.user_id) return false
|
||||
if (!parsedAuth.id || !parsedAuth.token || !parsedAuth.user_id)
|
||||
return false;
|
||||
|
||||
const p:AuthProps = {
|
||||
const p: AuthProps = {
|
||||
auth: {
|
||||
id:parsedAuth.id,
|
||||
token:parsedAuth.token,
|
||||
user_id:parsedAuth.user_id
|
||||
}
|
||||
id: parsedAuth.id,
|
||||
token: parsedAuth.token,
|
||||
user_id: parsedAuth.user_id,
|
||||
},
|
||||
};
|
||||
|
||||
const foundAuth = await Auth.findOne({where: { id: p.auth?.id}});
|
||||
if(!foundAuth || foundAuth.token != p.auth?.token ) return false;
|
||||
const foundAuth = await Auth.findOne({ where: { id: p.auth?.id } });
|
||||
if (!foundAuth || foundAuth.token != p.auth?.token) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function getCookieAuth():Promise<AuthProps>
|
||||
{
|
||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
||||
const cookieAuthSanitized = cookieAuthValue? JSON.parse(JSON.stringify(cookieAuthValue)) : "";
|
||||
console.log("kanker koek")
|
||||
export async function getCookieAuth(): Promise<AuthProps> {
|
||||
const cookieAuthValue = await cookies().get("auth")?.value;
|
||||
const cookieAuthSanitized = cookieAuthValue
|
||||
? JSON.parse(JSON.stringify(cookieAuthValue))
|
||||
: "";
|
||||
console.log("kanker koek");
|
||||
|
||||
if(!cookieAuthSanitized) return {}
|
||||
if (!cookieAuthSanitized) return {};
|
||||
|
||||
const kd = JSON.parse(cookieAuthSanitized);
|
||||
if(!kd.id || !kd.token || !kd.user_id) return {};
|
||||
if (!kd.id || !kd.token || !kd.user_id) return {};
|
||||
|
||||
const foundAuth = await Auth.findOne({where: { id: kd.id},include:{model:User}});
|
||||
if(!foundAuth) return {};
|
||||
const authObject:AuthProps = {
|
||||
const foundAuth = await Auth.findOne({
|
||||
where: { id: kd.id },
|
||||
include: { model: User },
|
||||
});
|
||||
if (!foundAuth) return {};
|
||||
const authObject: AuthProps = {
|
||||
auth: {
|
||||
id:kd.id,
|
||||
token:kd.token,
|
||||
user_id:kd.user_id
|
||||
id: kd.id,
|
||||
token: kd.token,
|
||||
user_id: kd.user_id,
|
||||
},
|
||||
user: await foundAuth.user
|
||||
}
|
||||
user: await foundAuth.user,
|
||||
};
|
||||
return authObject;
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@ import Link from "next/link";
|
||||
import { redirect } from 'next/navigation';
|
||||
import { Router } from "next/router";
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { truncateString } from "@/util/utils";
|
||||
import { truncateString } from "@/util/strings";
|
||||
// @ts-ignore
|
||||
import { MDXRemote } from "next-mdx-remote/rsc";
|
||||
import { ExampleComponent } from "./article";
|
||||
|
||||
3
src/util/aifa.ts
Normal file
3
src/util/aifa.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { ReactNode } from "react";
|
||||
|
||||
export const aifa = (a: ReactNode, b: ReactNode) => (a ? a : b);
|
||||
@ -1,2 +1,3 @@
|
||||
export * from './error';
|
||||
export * from './getAPIEnv';
|
||||
export * from './user';
|
||||
|
||||
2
src/util/cookies/index.ts
Normal file
2
src/util/cookies/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './Cookies';
|
||||
export * from './parseSetCookie';
|
||||
@ -1,10 +0,0 @@
|
||||
|
||||
class Gens{
|
||||
|
||||
public static loremipsum():String
|
||||
{
|
||||
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer egestas eros a imperdiet ultrices. Maecenas tincidunt tristique dolor, vitae dignissim ligula faucibus sit amet. Ut hendrerit elit eu elit molestie, vel consectetur leo accumsan. Phasellus ex mi, dignissim at aliquam at, rutrum eget mi. Curabitur pellentesque auctor nulla sed pulvinar. Maecenas scelerisque orci at sem finibus tincidunt. Mauris viverra pulvinar nibh. Etiam ornare purus leo, at cursus elit ornare nec. Suspendisse potenti. Sed nisl libero, sollicitudin vitae dignissim sit amet, laoreet sit amet odio. Duis rhoncus felis ut erat facilisis, vitae rutrum odio sollicitudin. Praesent et scelerisque eros. Praesent laoreet eu orci ut blandit. Morbi dapibus nibh urna, eget blandit quam aliquet vitae. Nulla quam metus, volutpat et vulputate vel, viverra sed diam.'
|
||||
}
|
||||
|
||||
}
|
||||
export default Gens;
|
||||
@ -1,10 +1,6 @@
|
||||
export * from './api';
|
||||
export * from './auth';
|
||||
export * from './Cookies';
|
||||
export * from './DeepPartial';
|
||||
export * from './gens';
|
||||
export * from './getAPIEnv';
|
||||
export * from './parseSetCookie';
|
||||
export * from './aifa';
|
||||
export * from './auth';
|
||||
export * from './cookies';
|
||||
export * from './state';
|
||||
export * from './url';
|
||||
export * from './utils';
|
||||
export * from './strings';
|
||||
|
||||
1
src/util/strings/index.ts
Normal file
1
src/util/strings/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './loremipsum';
|
||||
6
src/util/strings/loremipsum.ts
Normal file
6
src/util/strings/loremipsum.ts
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
export function loremipsum():String
|
||||
{
|
||||
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer egestas eros a imperdiet ultrices. Maecenas tincidunt tristique dolor, vitae dignissim ligula faucibus sit amet. Ut hendrerit elit eu elit molestie, vel consectetur leo accumsan. Phasellus ex mi, dignissim at aliquam at, rutrum eget mi. Curabitur pellentesque auctor nulla sed pulvinar. Maecenas scelerisque orci at sem finibus tincidunt. Mauris viverra pulvinar nibh. Etiam ornare purus leo, at cursus elit ornare nec. Suspendisse potenti. Sed nisl libero, sollicitudin vitae dignissim sit amet, laoreet sit amet odio. Duis rhoncus felis ut erat facilisis, vitae rutrum odio sollicitudin. Praesent et scelerisque eros. Praesent laoreet eu orci ut blandit. Morbi dapibus nibh urna, eget blandit quam aliquet vitae. Nulla quam metus, volutpat et vulputate vel, viverra sed diam.'
|
||||
}
|
||||
|
||||
7
src/util/strings/truncateString.ts
Normal file
7
src/util/strings/truncateString.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export function truncateString(str:string = '', num:number = 255) {
|
||||
if (str.length > num) {
|
||||
return str.slice(0, num) + "...";
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
'use server'
|
||||
|
||||
import { getAPIEnv } from "../getAPIEnv";
|
||||
import { getAPIEnv } from "../api/getAPIEnv";
|
||||
|
||||
export function constructAPIUrl(endpoint:string){
|
||||
const { schema, host, port, basepath } = getAPIEnv();
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
'server only'
|
||||
import { ReactNode } from "react";
|
||||
import Gens from "./gens";
|
||||
function truncateString(str:string = '', num:number = 255) {
|
||||
if (str.length > num) {
|
||||
return str.slice(0, num) + "...";
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export { Gens, truncateString }
|
||||
export const aifa = (a: ReactNode, b: ReactNode) => (a ? a : b);
|
||||
Loading…
x
Reference in New Issue
Block a user