Skip to content

Commit

Permalink
feat: 🎸 sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
ZingerLittleBee committed Apr 27, 2024
1 parent 667474d commit 76c1e2a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
14 changes: 10 additions & 4 deletions apps/hub/app/auth/signin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@ import {
} from '@/components/ui/card'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { useToast } from '@/components/ui/use-toast'

export default function SignInPage() {
const { toast } = useToast()

const [username, setUsername] = useState('')
const [password, setPassword] = useState('')

const handleSignIn = async () => {
const result = await signIn('credentials', {
redirect: false,
username,
password,
})

// TODO update status
if (result?.ok) {
console.log('Sign in successful')
toast({
title: 'Sign in successful',
})
} else {
console.error('Sign in failed')
toast({
title: 'Sign in failed',
description: 'Please try again',
})
}
}

Expand Down
19 changes: 19 additions & 0 deletions apps/hub/app/auth/signout/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client'

import { LogOut } from 'lucide-react'
import { signOut } from 'next-auth/react'

import { Button } from '@/components/ui/button'

export default function SignOutPage() {
return (
<Button
variant="outline"
className="w-full max-w-[200px] m-auto"
onClick={() => signOut()}
>
<LogOut className="mr-2 size-4" />
<span>Log out</span>
</Button>
)
}
19 changes: 3 additions & 16 deletions apps/hub/components/header/user-nav.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Link from 'next/link'
import { DISCUSSION_URL, ISSUE_URL } from '@/constant/github'
import { getServerAuthSession } from '@/server/auth'
import { Bug, Cloud, Github, LifeBuoy, LogOut } from 'lucide-react'
import { Bug, Cloud, Github, LifeBuoy } from 'lucide-react'

import { cn } from '@/lib/utils'
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
import { buttonVariants } from '@/components/ui/button'
import {
DropdownMenu,
DropdownMenuContent,
Expand All @@ -14,6 +12,7 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import SignOutPage from '@/app/auth/signout/page'

export async function UserNav() {
const session = await getServerAuthSession()
Expand Down Expand Up @@ -70,19 +69,7 @@ export async function UserNav() {
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem className="focus:bg-transparent">
<Link
href="/api/auth/signout"
rel="noreferrer"
className={cn(
'w-full',
buttonVariants({
variant: 'outline',
})
)}
>
<LogOut className="mr-2 size-4" />
<span>Log out</span>
</Link>
<SignOutPage />
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
Expand Down
8 changes: 7 additions & 1 deletion apps/hub/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ export const authOptions: NextAuthOptions = {
newUser: '/auth/new-user', // New users will be directed here on first sign in (leave the property out if not of interest)
},
callbacks: {
async signIn({ user }) {
signIn: async ({ user }) => {
return !!user?.id
},
redirect: async ({ url, baseUrl }) => {
if (url.startsWith('/')) {
return `${baseUrl}${url}`
}
return baseUrl
},
jwt: async ({ token, user }) => {
if (user) {
token = { ...user }
Expand Down

0 comments on commit 76c1e2a

Please sign in to comment.