generated from theodorusclarence/ts-nextjs-tailwind-starter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a939335
commit 852651f
Showing
9 changed files
with
288 additions
and
25 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
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 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,29 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function LoginHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (req.method === 'POST') { | ||
const { password } = req.body as { password: string }; | ||
|
||
if (!password) { | ||
return res.status(400).json({ | ||
message: 'Password are required', | ||
}); | ||
} | ||
|
||
if (password !== process.env.NEXT_PUBLIC_APP_PASSWORD) { | ||
return res.status(401).json({ | ||
message: 'Incorrect password', | ||
}); | ||
} | ||
|
||
return res | ||
.status(200) | ||
.json({ token: jwt.sign({}, process.env.NEXT_PUBLIC_APP_SECRET!) }); | ||
} else { | ||
res.status(405).json({ message: 'Method Not Allowed' }); | ||
} | ||
} |
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,92 @@ | ||
import axios from 'axios'; | ||
import router from 'next/router'; | ||
import * as React from 'react'; | ||
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'; | ||
import toast from 'react-hot-toast'; | ||
|
||
import useLoadingToast from '@/hooks/toast/useLoadingToast'; | ||
|
||
import Accent from '@/components/Accent'; | ||
import Button from '@/components/buttons/Button'; | ||
import Input from '@/components/forms/Input'; | ||
import Layout from '@/components/layout/Layout'; | ||
import Seo from '@/components/Seo'; | ||
|
||
import { DEFAULT_TOAST_MESSAGE } from '@/constant/toast'; | ||
|
||
type NewLinkFormData = { | ||
slug: string; | ||
link: string; | ||
}; | ||
|
||
export default function NewLinkPage() { | ||
const isLoading = useLoadingToast(); | ||
|
||
//#region //*=========== Form =========== | ||
const methods = useForm<NewLinkFormData>({ | ||
mode: 'onTouched', | ||
}); | ||
const { handleSubmit } = methods; | ||
//#endregion //*======== Form =========== | ||
|
||
//#region //*=========== Form Submit =========== | ||
const onSubmit: SubmitHandler<NewLinkFormData> = (data) => { | ||
toast.promise( | ||
axios.post<{ token: string }>('/api/login', data).then((res) => { | ||
localStorage.setItem('@notiolink/app_token', res.data.token); | ||
router.replace(`/new`); | ||
}), | ||
{ | ||
...DEFAULT_TOAST_MESSAGE, | ||
success: 'Logged in, you can now add new link', | ||
} | ||
); | ||
}; | ||
//#endregion //*======== Form Submit =========== | ||
|
||
return ( | ||
<Layout> | ||
<Seo templateTitle='Login' /> | ||
|
||
<main> | ||
<section> | ||
<div className='layout flex flex-col justify-center items-center py-20 min-h-screen'> | ||
<h1 className='h0'> | ||
<Accent>Login to the account</Accent> | ||
</h1> | ||
|
||
<FormProvider {...methods}> | ||
<form | ||
onSubmit={handleSubmit(onSubmit)} | ||
className='mt-8 w-full max-w-sm' | ||
> | ||
<div className='space-y-4'> | ||
<Input | ||
id='password' | ||
label='Password' | ||
type='password' | ||
validation={{ | ||
required: 'Password must be filled', | ||
}} | ||
/> | ||
</div> | ||
|
||
<div className='flex flex-col mt-5'> | ||
<Button | ||
isLoading={isLoading} | ||
className='justify-center w-full md:ml-auto md:w-auto' | ||
variant='outline' | ||
isDarkBg | ||
type='submit' | ||
> | ||
Login! | ||
</Button> | ||
</div> | ||
</form> | ||
</FormProvider> | ||
</div> | ||
</section> | ||
</main> | ||
</Layout> | ||
); | ||
} |
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
Oops, something went wrong.
852651f
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: