-
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.
Merge pull request #68 from fac27/cond-login-link
Cond login link
- Loading branch information
Showing
2 changed files
with
56 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,32 @@ import Link from 'next/link' | |
import { Dropdown, Navbar, Avatar } from 'flowbite-react' | ||
import { RxHamburgerMenu } from 'react-icons/rx' | ||
import HamburgerSidebar from './HamburgerSidebar' | ||
import { useState } from 'react' | ||
import { useEffect, useState } from 'react' | ||
import { useRouter } from 'next/navigation' | ||
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs' | ||
import { | ||
Session, | ||
createClientComponentClient, | ||
} from '@supabase/auth-helpers-nextjs' | ||
import { Database } from '../../types/supabase' | ||
|
||
const Header = () => { | ||
const [showSidebar, setShowSidebar] = useState(false) | ||
const [session, setSession] = useState<Session | null>(null) | ||
const router = useRouter() | ||
const supabase = createClientComponentClient<Database>() | ||
|
||
useEffect(() => { | ||
// check whether user is logged in | ||
const getSession = async () => { | ||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession() | ||
console.log(session) | ||
if (session) return setSession(session) | ||
} | ||
getSession() | ||
}, []) | ||
|
||
const handleLogout = async () => { | ||
await supabase.auth.signOut() | ||
router.refresh() | ||
|
@@ -54,25 +70,39 @@ const Header = () => { | |
/> | ||
} | ||
> | ||
<Dropdown.Header> | ||
<span className="block text-lg">Gertrude Pickle</span> | ||
<span className="block truncate text-md font-medium"> | ||
[email protected] | ||
</span> | ||
</Dropdown.Header> | ||
<Dropdown.Item>Settings</Dropdown.Item> | ||
<Dropdown.Divider /> | ||
<Link href="/add-listing"> | ||
<Dropdown.Item>Add Listing</Dropdown.Item> | ||
</Link> | ||
<Dropdown.Divider /> | ||
<Dropdown.Item>Sign out</Dropdown.Item> | ||
<Link href="/log-in"> | ||
<Dropdown.Item>Log In</Dropdown.Item> | ||
</Link> | ||
<Link href="/sign-up"> | ||
<Dropdown.Item>Sign Up</Dropdown.Item> | ||
</Link> | ||
{session ? ( | ||
<> | ||
<Dropdown.Header> | ||
{/* <span className="block text-lg">Gertrude Pickle</span> */} | ||
<span className="block truncate text-lg font-medium"> | ||
{session.user.email} | ||
</span> | ||
</Dropdown.Header> | ||
<Dropdown.Item>Settings</Dropdown.Item> | ||
{session.user.user_metadata.role_id === 2 ? ( | ||
<> | ||
<Dropdown.Divider /> | ||
<Link href="/add-listing"> | ||
<Dropdown.Item>Add Listing</Dropdown.Item> | ||
</Link> | ||
</> | ||
) : ( | ||
'' | ||
)} | ||
|
||
<Dropdown.Divider /> | ||
<Dropdown.Item>Sign out</Dropdown.Item> | ||
</> | ||
) : ( | ||
<> | ||
<Link href="/log-in"> | ||
<Dropdown.Item>Log In</Dropdown.Item> | ||
</Link> | ||
<Link href="/sign-up"> | ||
<Dropdown.Item>Sign Up</Dropdown.Item> | ||
</Link> | ||
</> | ||
)} | ||
</Dropdown> | ||
</div> | ||
</Navbar> | ||
|