Updates
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import "@/src/styles/globals.css";
|
||||
|
||||
import type { AppProps } from "next/app";
|
||||
|
||||
export default function App({ Component, pageProps }: AppProps) {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import Main from "@/src/components/pages/home";
|
||||
import defaultAdminProps from "@/src/functions/pages/admin/default-admin-props";
|
||||
import Layout from "@/src/layouts/admin";
|
||||
import { GetServerSideProps } from "next";
|
||||
|
||||
export default function AdminDashboard() {
|
||||
return (
|
||||
<Layout>
|
||||
<Main />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||
return await defaultAdminProps({
|
||||
ctx,
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
import { APIReqObject } from "@/src/types";
|
||||
import loginUser from "@/src/utils/login-user";
|
||||
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<APIResponseObject>,
|
||||
) {
|
||||
try {
|
||||
if (req.method !== "POST") {
|
||||
return res.json({
|
||||
success: false,
|
||||
msg: `Wrong Method`,
|
||||
});
|
||||
}
|
||||
|
||||
const { email, username, password } = req.body as APIReqObject;
|
||||
|
||||
if (typeof password !== "string" || !password?.match(/./)) {
|
||||
throw new Error(`Password is required!`);
|
||||
}
|
||||
|
||||
const logged_in_user = await loginUser({
|
||||
res,
|
||||
email_or_username: email || username,
|
||||
password,
|
||||
});
|
||||
|
||||
return res.json(logged_in_user);
|
||||
} catch (error: any) {
|
||||
return res.json({
|
||||
success: false,
|
||||
msg: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import { NSQLITE_TEST_DB_USERS, NSQLiteTables } from "@/src/db/types";
|
||||
import { APIReqObject } from "@/src/types";
|
||||
import loginUser from "@/src/utils/login-user";
|
||||
import hashPassword from "@moduletrace/datasquirel/dist/package-shared/functions/dsql/hashPassword";
|
||||
import { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import NSQLite from "@moduletrace/nsqlite";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<APIResponseObject>,
|
||||
) {
|
||||
try {
|
||||
if (req.method !== "POST") {
|
||||
return res.json({
|
||||
success: false,
|
||||
msg: `Wrong Method`,
|
||||
});
|
||||
}
|
||||
|
||||
const { new_user } = req.body as APIReqObject;
|
||||
|
||||
if (!new_user) {
|
||||
throw new Error(`No new User Object Passed!`);
|
||||
}
|
||||
|
||||
if (!new_user.password) {
|
||||
throw new Error(`New User Password is required.`);
|
||||
}
|
||||
|
||||
const existing_users_res = await NSQLite.select<
|
||||
NSQLITE_TEST_DB_USERS,
|
||||
(typeof NSQLiteTables)[number]
|
||||
>({
|
||||
table: "users",
|
||||
});
|
||||
|
||||
if (existing_users_res.payload?.[0]?.id) {
|
||||
return res.json({
|
||||
success: false,
|
||||
msg: `Super Admin User already exists. Other Users can be created by this user.`,
|
||||
});
|
||||
}
|
||||
|
||||
const { first_name, email, last_name, password } = new_user;
|
||||
|
||||
const new_user_password = hashPassword({ password });
|
||||
|
||||
const new_user_insert_res = await NSQLite.insert<
|
||||
NSQLITE_TEST_DB_USERS,
|
||||
(typeof NSQLiteTables)[number]
|
||||
>({
|
||||
data: [
|
||||
{
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
password: new_user_password,
|
||||
is_super_admin: 1,
|
||||
},
|
||||
],
|
||||
table: "users",
|
||||
});
|
||||
|
||||
console.log("new_user_insert_res", new_user_insert_res);
|
||||
|
||||
const new_user_id = new_user_insert_res.postInsertReturn?.insertId;
|
||||
|
||||
if (!new_user_id) {
|
||||
throw new Error(`Couldn't create New User.`);
|
||||
}
|
||||
|
||||
const newly_inserted_user_res = await NSQLite.select<
|
||||
NSQLITE_TEST_DB_USERS,
|
||||
(typeof NSQLiteTables)[number]
|
||||
>({
|
||||
table: "users",
|
||||
query: {
|
||||
query: {
|
||||
is_super_admin: { value: "1" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const newly_inserted_user = newly_inserted_user_res.singleRes;
|
||||
|
||||
if (!newly_inserted_user?.id) {
|
||||
throw new Error(`Couldn't Find Newly inserted user.`);
|
||||
}
|
||||
|
||||
const logged_in_user = await loginUser({
|
||||
res,
|
||||
user_id: newly_inserted_user.id,
|
||||
});
|
||||
|
||||
return res.json(logged_in_user);
|
||||
} catch (error: any) {
|
||||
return res.json({
|
||||
success: false,
|
||||
msg: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
import Main from "@/src/components/pages/auth/login";
|
||||
import Layout from "@/src/layouts/login";
|
||||
import userAuth from "@/src/utils/user-auth";
|
||||
import NSQLite from "@moduletrace/nsqlite";
|
||||
import { GetServerSideProps } from "next";
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
@@ -8,3 +11,31 @@ export default function LoginPage() {
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||
const users = await NSQLite.select({ table: "users" });
|
||||
|
||||
if (!users.payload?.[0]) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: `/auth/signup`,
|
||||
statusCode: 307,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const { singleRes: user } = await userAuth({ req: ctx.req });
|
||||
|
||||
if (user?.logged_in_status) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: `/admin`,
|
||||
statusCode: 307,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
props: {},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import Main from "@/src/components/pages/auth/signup";
|
||||
import Layout from "@/src/layouts/login";
|
||||
import NSQLite from "@moduletrace/nsqlite";
|
||||
import { GetServerSideProps } from "next";
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
<Layout>
|
||||
<Main />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||
const users = await NSQLite.select({ table: "users" });
|
||||
|
||||
if (users.payload?.[0]) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: `/auth/login`,
|
||||
statusCode: 307,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
props: {},
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import Main from "@/src/components/pages/home";
|
||||
import Layout from "@/src/layouts/login";
|
||||
import { GetServerSideProps } from "next";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
@@ -8,3 +9,12 @@ export default function Home() {
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async () => {
|
||||
return {
|
||||
redirect: {
|
||||
destination: "/admin",
|
||||
statusCode: 307,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user