implemented post management dashboard and started work on projects
This commit is contained in:
+47
-3
@@ -5,8 +5,10 @@ import { cookies } from "next/headers"
|
||||
import { parseSetCookie } from "@/util/parseSetCookie";
|
||||
import makeFetchCookie from 'fetch-cookie';
|
||||
import fetchCookie from "fetch-cookie";
|
||||
import { Attributes } from "@sequelize/core";
|
||||
import { Attribute, Attributes } from "@sequelize/core";
|
||||
import { User } from "@/model/User";
|
||||
import { AuthProps } from "@/providers/providers";
|
||||
import { Auth } from "@/model/Auth";
|
||||
|
||||
type LoginReturn = {
|
||||
cookie?:unknown,
|
||||
@@ -80,6 +82,48 @@ export async function serverValidateSessionCookie(koek:string):Promise<boolean>
|
||||
|
||||
export async function userIsAdmin(user:Partial<Attributes<User>>):Promise<boolean>
|
||||
{
|
||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
||||
const cookieAuthSanitized = decodeURIComponent(cookieAuthValue ? cookieAuthValue: "");
|
||||
|
||||
if(!cookieAuthSanitized) return false;
|
||||
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;
|
||||
}
|
||||
|
||||
export async function getCookieAuth():Promise<AuthProps>
|
||||
{
|
||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
||||
const cookieAuthSanitized = decodeURIComponent(cookieAuthValue ? cookieAuthValue: "");
|
||||
|
||||
if(!cookieAuthSanitized) return {}
|
||||
|
||||
const kd = JSON.parse(cookieAuthSanitized);
|
||||
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 = {
|
||||
auth: {
|
||||
id:kd.id,
|
||||
token:kd.token,
|
||||
user_id:kd.user_id
|
||||
},
|
||||
user: await foundAuth.user
|
||||
}
|
||||
return authObject;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
'use server';
|
||||
import { Post } from "@/model/Post";
|
||||
import { Attributes } from "@sequelize/core";
|
||||
|
||||
|
||||
export async function deletePost(postID: number): Promise<boolean> {
|
||||
const destroy = await Post.destroy({ where: { id: postID } });
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
export async function getPosts(): Promise<string> {
|
||||
const posts = await Post.findAll();
|
||||
return JSON.stringify(posts);
|
||||
}
|
||||
Reference in New Issue
Block a user