-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rewrite next auth to use app router (#202)
* chore: rewrite to use app router and add prisma * chore: add credential auth
- Loading branch information
1 parent
0a72761
commit 9566bac
Showing
20 changed files
with
3,743 additions
and
153 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,35 @@ | ||
'use client' | ||
|
||
import { Stack, Card, CardBody } from '@chakra-ui/react' | ||
import { Auth, useAuth } from '@saas-ui/auth' | ||
import { useRouter } from 'next/navigation' | ||
import React from 'react' | ||
|
||
import { SaasUILogo } from '@saas-ui/assets' | ||
|
||
export default function LoginPage() { | ||
const { isAuthenticated } = useAuth() | ||
const router = useRouter() | ||
|
||
React.useEffect(() => { | ||
if (isAuthenticated) { | ||
router.replace('/') | ||
} | ||
}, [isAuthenticated]) | ||
|
||
return ( | ||
<Stack | ||
height="100vh" | ||
alignItems="center" | ||
justifyContent="center" | ||
spacing="10" | ||
> | ||
<SaasUILogo width="120px" /> | ||
<Card width="380px" maxW="container.md"> | ||
<CardBody> | ||
<Auth type="password" /> | ||
</CardBody> | ||
</Card> | ||
</Stack> | ||
) | ||
} |
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,44 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { useRouter } from 'next/navigation' | ||
|
||
import { Stack, Card, CardBody } from '@chakra-ui/react' | ||
import { SaasUILogo } from '@saas-ui/assets' | ||
import { Auth, AvailableProviders, useAuth } from '@saas-ui/auth' | ||
|
||
import { FaGithub } from 'react-icons/fa' | ||
|
||
const providers: AvailableProviders = { | ||
github: { | ||
icon: FaGithub, | ||
name: 'Github', | ||
}, | ||
} | ||
|
||
export default function SignupPage() { | ||
const { isAuthenticated } = useAuth() | ||
const router = useRouter() | ||
|
||
React.useEffect(() => { | ||
if (isAuthenticated) { | ||
router.replace('/') | ||
} | ||
}, [isAuthenticated]) | ||
|
||
return ( | ||
<Stack | ||
height="100vh" | ||
alignItems="center" | ||
justifyContent="center" | ||
spacing="10" | ||
> | ||
<SaasUILogo width="120px" /> | ||
<Card width="380px" maxW="container.md"> | ||
<CardBody> | ||
<Auth providers={providers} type="magiclink" view="signup" /> | ||
</CardBody> | ||
</Card> | ||
</Stack> | ||
) | ||
} |
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,6 @@ | ||
import { authConfig } from '@/lib/auth' | ||
import NextAuth from 'next-auth' | ||
|
||
const handler = NextAuth(authConfig) | ||
|
||
export { handler as GET, handler as POST } |
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,30 @@ | ||
import { NextAuthProvider } from '@/context/AuthProvider' | ||
import { SaasProvider } from '@saas-ui/react' | ||
import { authConfig } from '@/lib/auth' | ||
import { getServerSession } from 'next-auth' | ||
import { ColorModeScript } from '@chakra-ui/react' | ||
import { cookies } from 'next/headers' | ||
|
||
export default async function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
const cookieStore = cookies() | ||
|
||
const colorMode = (cookieStore.get('chakra-ui-color-mode')?.value ?? | ||
'dark') as 'light' | 'dark' | ||
|
||
const session = await getServerSession(authConfig) | ||
|
||
return ( | ||
<html lang="en" data-theme={colorMode} style={{ colorScheme: colorMode }}> | ||
<body className={`chakra-ui-${colorMode}`}> | ||
<SaasProvider> | ||
<ColorModeScript initialColorMode={colorMode} type="cookie" /> | ||
<NextAuthProvider session={session}>{children}</NextAuthProvider> | ||
</SaasProvider> | ||
</body> | ||
</html> | ||
) | ||
} |
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,18 @@ | ||
'use client' | ||
|
||
import { Authenticated } from '@/components/Authenticated' | ||
import { Box, Button } from '@chakra-ui/react' | ||
import { useAuth } from '@saas-ui/auth' | ||
|
||
export default function HomePage() { | ||
const { user, logOut } = useAuth() | ||
console.log('user', user) | ||
return ( | ||
<Authenticated> | ||
<Box> | ||
Logged in as: {user?.email}.{' '} | ||
<Button onClick={() => logOut()}>Log out</Button> | ||
</Box> | ||
</Authenticated> | ||
) | ||
} |
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,19 @@ | ||
import { useSession } from 'next-auth/react' | ||
import { useRouter } from 'next/navigation' | ||
import React from 'react' | ||
|
||
export const Authenticated: React.FC<React.PropsWithChildren> = (props) => { | ||
const router = useRouter() | ||
const { data } = useSession({ | ||
required: true, | ||
onUnauthenticated() { | ||
router.replace('/login') | ||
}, | ||
}) | ||
|
||
if (!data) { | ||
return null | ||
} | ||
|
||
return props.children | ||
} |
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,13 @@ | ||
services: | ||
postgres: | ||
image: postgres:16 | ||
restart: unless-stopped | ||
# Uncomment the following line to enable query logging | ||
# Then restart the container. | ||
# command: ['postgres', '-c', 'log_statement=all'] | ||
environment: | ||
POSTGRES_DB: postgres | ||
POSTGRES_USER: nextauth | ||
POSTGRES_PASSWORD: changeme | ||
ports: | ||
- '5432:5432' |
Oops, something went wrong.