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,
|
"**/CVS": true,
|
||||||
"**/.DS_Store": true,
|
"**/.DS_Store": true,
|
||||||
"**/Thumbs.db": true,
|
"**/Thumbs.db": true,
|
||||||
".**": true,
|
"**/.next": true,
|
||||||
"node_modules": 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 { constructAPIUrl } from "@/util/url";
|
||||||
import { cookies } from "next/headers"
|
import { cookies } from "next/headers";
|
||||||
import { parseSetCookie } from "@/util/parseSetCookie";
|
import { parseSetCookie } from "@/util/cookies";
|
||||||
import makeFetchCookie from 'fetch-cookie';
|
import makeFetchCookie from "fetch-cookie";
|
||||||
import fetchCookie from "fetch-cookie";
|
import fetchCookie from "fetch-cookie";
|
||||||
import { Attribute, Attributes } from "@sequelize/core";
|
import { User, Auth } from "@/models";
|
||||||
import { User, Auth } from "@/models";
|
|
||||||
import { AuthProps } from "@/providers/providers";
|
import { AuthProps } from "@/providers/providers";
|
||||||
import { ActionResult } from "./ActionResult";
|
|
||||||
|
|
||||||
type LoginReturn = {
|
type LoginReturn = {
|
||||||
cookie?:unknown,
|
cookie?: unknown;
|
||||||
errorMessage?:string;
|
errorMessage?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Instantiate header object
|
||||||
|
let headers: Headers = new Headers();
|
||||||
|
|
||||||
|
// Prepare fetchCookie
|
||||||
|
const { CookieJar, Cookie } = fetchCookie.toughCookie;
|
||||||
|
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")}`
|
||||||
|
);
|
||||||
|
let res = await fetchWithCookie(constructAPIUrl("auth"), {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "include",
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(jar.store.idx["localhost"]["/"]);
|
||||||
|
|
||||||
|
let koek = res.headers.getSetCookie();
|
||||||
|
|
||||||
|
let cookieDict = parseSetCookie(koek);
|
||||||
|
|
||||||
|
await cookies().set("auth", cookieDict.auth);
|
||||||
|
return {
|
||||||
|
cookie: cookieDict.auth,
|
||||||
|
errorMessage: "",
|
||||||
|
};
|
||||||
|
// console.log(koek);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function attemptAPILogin(method:string,formData:FormData):Promise<LoginReturn|null>
|
export async function serverAttemptAuthenticateUser(
|
||||||
{
|
_currentState: unknown,
|
||||||
// Check if form data is present with required fields, return null if not
|
formData: FormData
|
||||||
if(!formData || !formData.get('input_username') || !formData.get('input_password')) return null;
|
): Promise<LoginReturn | null> {
|
||||||
|
try {
|
||||||
// Instantiate header object
|
const signInStatus = await attemptAPILogin("credentials", formData);
|
||||||
let headers:Headers = new Headers();
|
return signInStatus;
|
||||||
|
} catch (error: any) {
|
||||||
// Prepare fetchCookie
|
if (error) {
|
||||||
const { CookieJar, Cookie } = fetchCookie.toughCookie;
|
switch (error.type) {
|
||||||
const jar = new CookieJar()
|
case "CredentialsSignin":
|
||||||
const fetchWithCookie = makeFetchCookie(fetch, jar);
|
return { errorMessage: "invalidCredentials" };
|
||||||
|
default:
|
||||||
// Set Basic Auth
|
return { errorMessage: "Something went wrong." };
|
||||||
headers.set('Authorization', `Basic ${Buffer.from(`${formData.get('input_username')}:${formData.get('input_password')}`).toString('base64')}`);
|
}
|
||||||
let res = await fetchWithCookie(constructAPIUrl("auth"), {
|
}
|
||||||
method:'POST',
|
throw Error;
|
||||||
credentials: 'include',
|
}
|
||||||
headers:headers,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(jar.store.idx['localhost']['/']);
|
|
||||||
|
|
||||||
let koek = res.headers.getSetCookie();
|
|
||||||
|
|
||||||
let cookieDict = parseSetCookie(koek);
|
|
||||||
|
|
||||||
await cookies().set('auth', cookieDict.auth);
|
|
||||||
return {
|
|
||||||
cookie:cookieDict.auth,
|
|
||||||
errorMessage:""
|
|
||||||
};
|
|
||||||
// console.log(koek);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function serverAttemptAuthenticateUser(_currentState: unknown, formData: FormData):Promise<LoginReturn|null>
|
export async function serverValidateSessionCookie(
|
||||||
{
|
koek: string
|
||||||
try {
|
): Promise<boolean> {
|
||||||
const signInStatus = await attemptAPILogin('credentials', formData)
|
const validateSession = await fetch(constructAPIUrl("auth/validate"), {
|
||||||
return signInStatus;
|
method: "POST",
|
||||||
} catch (error:any) {
|
headers: {
|
||||||
if (error) {
|
Cookie: `auth=${koek};`,
|
||||||
switch (error.type) {
|
},
|
||||||
case 'CredentialsSignin': return { errorMessage: 'invalidCredentials' };
|
});
|
||||||
default: return { errorMessage: 'Something went wrong.' };
|
if (validateSession.status == 200) return true;
|
||||||
}
|
else return false;
|
||||||
}
|
|
||||||
throw Error
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function serverValidateSessionCookie(koek:string):Promise<boolean>
|
export async function userIsAdmin(): Promise<boolean> {
|
||||||
{
|
const cookieAuthValue = await cookies().get("auth")?.value;
|
||||||
const validateSession = await fetch(constructAPIUrl("auth/validate"),{
|
const cookieAuthSanitized = cookieAuthValue
|
||||||
method:"POST",
|
? JSON.parse(JSON.stringify(cookieAuthValue))
|
||||||
headers:{
|
: "";
|
||||||
Cookie: `auth=${koek};`
|
|
||||||
}
|
if (!cookieAuthSanitized) return false;
|
||||||
});
|
const parsedAuth = JSON.parse(cookieAuthSanitized);
|
||||||
if(validateSession.status == 200)
|
|
||||||
return true
|
if (!parsedAuth.id || !parsedAuth.token || !parsedAuth.user_id)
|
||||||
else
|
return false;
|
||||||
return false
|
|
||||||
|
const p: AuthProps = {
|
||||||
|
auth: {
|
||||||
|
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;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function userIsAdmin():Promise<boolean>
|
export async function getCookieAuth(): Promise<AuthProps> {
|
||||||
{
|
const cookieAuthValue = await cookies().get("auth")?.value;
|
||||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
const cookieAuthSanitized = cookieAuthValue
|
||||||
const cookieAuthSanitized = cookieAuthValue? JSON.parse(JSON.stringify(cookieAuthValue)) : "";
|
? JSON.parse(JSON.stringify(cookieAuthValue))
|
||||||
|
: "";
|
||||||
|
console.log("kanker koek");
|
||||||
|
|
||||||
if(!cookieAuthSanitized) return false;
|
if (!cookieAuthSanitized) return {};
|
||||||
const parsedAuth = JSON.parse(cookieAuthSanitized);
|
|
||||||
|
|
||||||
if(!parsedAuth.id || !parsedAuth.token || !parsedAuth.user_id) return false
|
|
||||||
|
|
||||||
const p:AuthProps = {
|
|
||||||
auth: {
|
|
||||||
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;
|
|
||||||
|
|
||||||
return true;
|
const kd = JSON.parse(cookieAuthSanitized);
|
||||||
}
|
if (!kd.id || !kd.token || !kd.user_id) return {};
|
||||||
|
|
||||||
export async function getCookieAuth():Promise<AuthProps>
|
const foundAuth = await Auth.findOne({
|
||||||
{
|
where: { id: kd.id },
|
||||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
include: { model: User },
|
||||||
const cookieAuthSanitized = cookieAuthValue? JSON.parse(JSON.stringify(cookieAuthValue)) : "";
|
});
|
||||||
console.log("kanker koek")
|
if (!foundAuth) return {};
|
||||||
|
const authObject: AuthProps = {
|
||||||
if(!cookieAuthSanitized) return {}
|
auth: {
|
||||||
|
id: kd.id,
|
||||||
const kd = JSON.parse(cookieAuthSanitized);
|
token: kd.token,
|
||||||
if(!kd.id || !kd.token || !kd.user_id) return {};
|
user_id: kd.user_id,
|
||||||
|
},
|
||||||
const foundAuth = await Auth.findOne({where: { id: kd.id},include:{model:User}});
|
user: await foundAuth.user,
|
||||||
if(!foundAuth) return {};
|
};
|
||||||
const authObject:AuthProps = {
|
return authObject;
|
||||||
auth: {
|
|
||||||
id:kd.id,
|
|
||||||
token:kd.token,
|
|
||||||
user_id:kd.user_id
|
|
||||||
},
|
|
||||||
user: await foundAuth.user
|
|
||||||
}
|
|
||||||
return authObject;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import Link from "next/link";
|
|||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import { Router } from "next/router";
|
import { Router } from "next/router";
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import { truncateString } from "@/util/utils";
|
import { truncateString } from "@/util/strings";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { MDXRemote } from "next-mdx-remote/rsc";
|
import { MDXRemote } from "next-mdx-remote/rsc";
|
||||||
import { ExampleComponent } from "./article";
|
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 './error';
|
||||||
|
export * from './getAPIEnv';
|
||||||
export * from './user';
|
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 './DeepPartial';
|
||||||
export * from './gens';
|
export * from './aifa';
|
||||||
export * from './getAPIEnv';
|
export * from './auth';
|
||||||
export * from './parseSetCookie';
|
export * from './cookies';
|
||||||
export * from './state';
|
export * from './state';
|
||||||
export * from './url';
|
export * from './strings';
|
||||||
export * from './utils';
|
|
||||||
|
|||||||
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'
|
'use server'
|
||||||
|
|
||||||
import { getAPIEnv } from "../getAPIEnv";
|
import { getAPIEnv } from "../api/getAPIEnv";
|
||||||
|
|
||||||
export function constructAPIUrl(endpoint:string){
|
export function constructAPIUrl(endpoint:string){
|
||||||
const { schema, host, port, basepath } = getAPIEnv();
|
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