-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: admin login wip * chore: admin login complete * chore: sample env updated * v1.4.0 * fix: types updated
- Loading branch information
1 parent
88b70d6
commit d6f7842
Showing
12 changed files
with
516 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ADMIN_PASSWORD="" | ||
IRON_SESSION_COOKIE_PASSWORD="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type Required<T, K extends keyof T> = T & { [P in K]-?: T[P] }; | ||
|
||
type WithRequired<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & | ||
Required<T, K>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
type Required<T, K extends keyof T> = T & { [P in K]-?: T[P] }; | ||
import * as IronSession from "iron-session"; | ||
|
||
type WithRequired<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> & | ||
Required<T, K>; | ||
declare module "iron-session" { | ||
interface IronSessionData { | ||
user?: { | ||
admin?: boolean; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Admin Home | ||
*/ | ||
|
||
// Dependencies | ||
import React from "react"; | ||
import Head from "next/head"; | ||
import PublicLayout from "../../../layouts/PublicLayout"; | ||
import WorkInProgress from "../../../components/reusable/WorkInProgress"; | ||
|
||
const AdminHomePage = () => { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Admin | Kunal Keshan</title> | ||
</Head> | ||
<PublicLayout> | ||
<WorkInProgress /> | ||
</PublicLayout> | ||
</> | ||
); | ||
}; | ||
|
||
export default AdminHomePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { withSessionRoute } from "../../../utils/withSession"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { z } from "zod"; | ||
import ApiError from "../../../utils/apiError"; | ||
|
||
export default withSessionRoute( | ||
async (req: NextApiRequest, res: NextApiResponse) => { | ||
switch (req.method) { | ||
case "POST": { | ||
return await loginRoute(req, res); | ||
} | ||
} | ||
} | ||
); | ||
|
||
const loginBodySchema = z.string(); | ||
|
||
interface LoginAdminApiRequest extends NextApiRequest { | ||
body: { | ||
password: z.infer<typeof loginBodySchema>; | ||
}; | ||
} | ||
|
||
async function loginRoute(req: LoginAdminApiRequest, res: NextApiResponse) { | ||
try { | ||
const { password } = req.body; | ||
const valid = loginBodySchema.safeParse(password); | ||
if (!valid.success) | ||
throw new ApiError({ | ||
statusCode: 400, | ||
message: "admin/invalid-password-type", | ||
}); | ||
const validPassword = password! === process.env.ADMIN_PASSWORD!; | ||
if (!validPassword) | ||
throw new ApiError({ | ||
statusCode: 401, | ||
message: "admin/wrong-password", | ||
}); | ||
req.session = { ...req.session, user: { admin: true } }; | ||
await req.session.save(); | ||
return res.status(200).json({ ok: true }); | ||
} catch (error) { | ||
if (error instanceof ApiError) { | ||
return res | ||
.status(error.statusCode) | ||
.json({ message: error.message, data: error.data }); | ||
} else { | ||
return res.status(500).json({ message: "app/internal-server-error" }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface ApiErrorOptions { | ||
message: string; | ||
statusCode: number; | ||
data?: object; | ||
} | ||
|
||
class ApiError extends Error { | ||
public statusCode: number; | ||
public data: object | undefined; | ||
constructor({ message, statusCode, data }: ApiErrorOptions) { | ||
super(); | ||
this.message = message; | ||
this.statusCode = statusCode; | ||
this.data = data; | ||
|
||
// Helps identify `instanceOf` applications | ||
Object.setPrototypeOf(this, ApiError.prototype); | ||
} | ||
} | ||
export default ApiError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
GetServerSidePropsContext, | ||
GetServerSidePropsResult, | ||
NextApiHandler, | ||
} from "next"; | ||
import { withIronSessionApiRoute, withIronSessionSsr } from "iron-session/next"; | ||
import { ironOptions } from "../config"; | ||
|
||
function withSessionRoute(handler: NextApiHandler) { | ||
return withIronSessionApiRoute(handler, ironOptions); | ||
} | ||
|
||
// Theses types are compatible with InferGetStaticPropsType https://nextjs.org/docs/basic-features/data-fetching#typescript-use-getstaticprops | ||
function withSessionSsr< | ||
P extends { [key: string]: unknown } = { [key: string]: unknown } | ||
>( | ||
handler: ( | ||
context: GetServerSidePropsContext | ||
) => GetServerSidePropsResult<P> | Promise<GetServerSidePropsResult<P>> | ||
) { | ||
return withIronSessionSsr(handler, ironOptions); | ||
} | ||
|
||
export { withSessionRoute, withSessionSsr }; |
Oops, something went wrong.
d6f7842
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
kunalkeshan-dev – ./
kunalkeshan-dev.vercel.app
kunalkeshan-dev-git-main-kunalkeshan.vercel.app
www.kunalkeshan.dev
kunalkeshan.dev
kunalkeshan-dev-kunalkeshan.vercel.app