-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
22 changed files
with
661 additions
and
51 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,32 @@ | ||
import type { FC } from 'react' | ||
import { signIn, signOut, useSession } from 'next-auth/react' | ||
|
||
const LoginButton: FC = () => { | ||
const { data: session } = useSession() | ||
if (session) { | ||
return ( | ||
<> | ||
Signed in as {session.user?.email} <br /> | ||
<button | ||
className="btn" | ||
onClick={() => signOut()} | ||
> | ||
Sign out | ||
</button> | ||
</> | ||
) | ||
} | ||
return ( | ||
<> | ||
Not signed in <br /> | ||
<button | ||
className="btn" | ||
onClick={() => signIn()} | ||
> | ||
Sign in | ||
</button> | ||
</> | ||
) | ||
} | ||
|
||
export default LoginButton |
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,24 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
import NextAuth from 'next-auth' | ||
import GithubProvider from 'next-auth/providers/github' | ||
import { PrismaAdapter } from '@next-auth/prisma-adapter' | ||
import { serviceGetSetting } from '~/services/bootstrap' | ||
import { prisma } from '~/db' | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
const setting = await serviceGetSetting() | ||
return NextAuth(req, res, { | ||
adapter: PrismaAdapter(prisma), | ||
providers: [ | ||
GithubProvider({ | ||
clientId: setting?.githubClientId, | ||
clientSecret: setting?.githubClientSecret, | ||
httpOptions: { timeout: 40000 }, | ||
}), | ||
], | ||
pages: { signIn: '/auth/signin' }, | ||
}) | ||
} |
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,15 @@ | ||
import type { Setting } from '@prisma/client' | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
import { serviceCreateSetting } from '~/services/bootstrap' | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<Setting>, | ||
) { | ||
const { method } = req | ||
|
||
if (method === 'POST') { | ||
const result = await serviceCreateSetting(req.body) | ||
res.status(200).json(result) | ||
} | ||
} |
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,52 @@ | ||
import type { GetServerSideProps } from 'next' | ||
import type { ClientSafeProvider } from 'next-auth/react' | ||
import { getProviders, signIn } from 'next-auth/react' | ||
import type { FC } from 'react' | ||
|
||
interface Props { | ||
providers: ClientSafeProvider[] | ||
} | ||
|
||
const Signin: FC<Props> = ({ providers }) => { | ||
return ( | ||
<div className="hero"> | ||
<div className="hero-content flex-col"> | ||
<div className="text-center"> | ||
<h1 className="text-5xl font-bold">Ohbug</h1> | ||
<p className="py-6">An open source application information monitoring platform.</p> | ||
</div> | ||
<div className="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100"> | ||
<div className="card-body"> | ||
{/* <div className="form-control"> | ||
<label className="label"> | ||
<span className="label-text">Email</span> | ||
</label> | ||
<input | ||
className="input input-bordered" | ||
placeholder="email" | ||
type="text" | ||
/> | ||
</div> */} | ||
{providers?.map(provider => ( | ||
<button | ||
className='btn' | ||
key={provider.name} | ||
onClick={() => signIn(provider.id)} | ||
> | ||
Sign in with{' '}{provider.name} | ||
</button> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Signin | ||
|
||
export const getServerSideProps: GetServerSideProps<Props> = async() => { | ||
const providers = await getProviders() | ||
|
||
return { props: { providers: JSON.parse(JSON.stringify(Object.values(providers ?? {}))) } } | ||
} |
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,87 @@ | ||
import type { Setting } from '@prisma/client' | ||
import { useRouter } from 'next/router' | ||
import type { ReactElement } from 'react' | ||
import { useCallback } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
|
||
const Bootstrap = () => { | ||
const router = useRouter() | ||
const { register, handleSubmit } = useForm<Setting>() | ||
const onSubmit = useCallback((data: Setting) => { | ||
fetch( | ||
'/api/setting', | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
headers: { 'Content-Type': 'application/json; charset=utf-8' }, | ||
}, | ||
) | ||
.then(res => res.json()) | ||
.then((setting) => { | ||
if (setting) | ||
router.replace('/auth/signin') | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<div className="hero min-h-screen"> | ||
<div className="hero-content"> | ||
<div className="card flex-shrink-0 w-96 shadow-2xl bg-base-100"> | ||
<div className="card-body"> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<div className="form-control"> | ||
<label className="label"> | ||
<span className="label-text">Github OAuth Client ID</span> | ||
</label> | ||
<input | ||
className="input input-bordered" | ||
placeholder="Client ID" | ||
required | ||
type="text" | ||
{...register('githubClientId')} | ||
/> | ||
</div> | ||
<div className="form-control"> | ||
<label className="label"> | ||
<span className="label-text">Github OAuth Client Secret</span> | ||
</label> | ||
<input | ||
className="input input-bordered" | ||
placeholder="Client Secret" | ||
required | ||
type="text" | ||
{...register('githubClientSecret')} | ||
/> | ||
<label className="label"> | ||
<a | ||
className="label-text-alt link link-hover" | ||
href="https://docs.github.com/developers/apps/building-oauth-apps/creating-an-oauth-app" | ||
rel="noreferrer" | ||
target="_blank" | ||
> | ||
Get Client ID/Secret? | ||
</a> | ||
</label> | ||
</div> | ||
|
||
<div className="form-control mt-6"> | ||
<button | ||
className="btn btn-primary" | ||
type="submit" | ||
> | ||
Bootstrap | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
Bootstrap.getLayout = function getLayout(page: ReactElement) { | ||
return page | ||
} | ||
|
||
export default Bootstrap |
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,17 @@ | ||
import type { Setting } from '@prisma/client' | ||
import { prisma } from '~/db' | ||
|
||
export function serviceGetSetting() { | ||
return prisma.setting.findFirst() | ||
} | ||
|
||
export async function serviceCreateSetting(data: Setting) { | ||
const setting = await prisma.setting.findFirst() | ||
if (setting) { | ||
return prisma.setting.update({ | ||
data, | ||
where: { id: setting.id }, | ||
}) | ||
} | ||
return prisma.setting.create({ data }) | ||
} |
Oops, something went wrong.