feat: login redirect and logout
This commit is contained in:
parent
3b1ec9becb
commit
7a52261b1e
@ -13,6 +13,7 @@ import { RichText } from '@/components/RichText'
|
|||||||
import { useMemo, useState } from 'react'
|
import { useMemo, useState } from 'react'
|
||||||
import requestHold from '../../serverCalls/requestHold'
|
import requestHold from '../../serverCalls/requestHold'
|
||||||
import { useGlobal } from '@/providers/GlobalProvider'
|
import { useGlobal } from '@/providers/GlobalProvider'
|
||||||
|
import Link from 'next/link'
|
||||||
|
|
||||||
type DropDownProps = {
|
type DropDownProps = {
|
||||||
currentRepository: Repository
|
currentRepository: Repository
|
||||||
@ -85,6 +86,8 @@ export default function BookByIdPageClient(props: Props) {
|
|||||||
|
|
||||||
const { user } = useGlobal()
|
const { user } = useGlobal()
|
||||||
|
|
||||||
|
console.log(user)
|
||||||
|
|
||||||
const [isRequestingCopy, setIsRequestingCopy] = useState(false)
|
const [isRequestingCopy, setIsRequestingCopy] = useState(false)
|
||||||
const [selectedRepository, setSelectedRepository] = useState<Repository | null>(
|
const [selectedRepository, setSelectedRepository] = useState<Repository | null>(
|
||||||
repos.length ? repos[0] : null,
|
repos.length ? repos[0] : null,
|
||||||
@ -147,15 +150,24 @@ export default function BookByIdPageClient(props: Props) {
|
|||||||
|
|
||||||
<p className="my-6 text-accent-foreground">{book.summary}</p>
|
<p className="my-6 text-accent-foreground">{book.summary}</p>
|
||||||
|
|
||||||
<RepoDropdown
|
{!user ? (
|
||||||
currentRepository={repos[0]}
|
<Link
|
||||||
repositories={repos}
|
href={`/login?redirectUrl=/books/${book.id}`}
|
||||||
isRequesting={isRequestingCopy}
|
className="underline dark:text-amber-200 text-amber-700 text-lg font-medium"
|
||||||
doesHoldExist={doesHoldExist}
|
>
|
||||||
isDisabled={isRequestDisabled || !user}
|
Sign in to request a hold
|
||||||
onClickRequest={onClickRequest}
|
</Link>
|
||||||
onChange={(repo) => setSelectedRepository(repo)}
|
) : (
|
||||||
/>
|
<RepoDropdown
|
||||||
|
currentRepository={repos[0]}
|
||||||
|
repositories={repos}
|
||||||
|
isRequesting={isRequestingCopy}
|
||||||
|
doesHoldExist={doesHoldExist}
|
||||||
|
isDisabled={isRequestDisabled || !user}
|
||||||
|
onClickRequest={onClickRequest}
|
||||||
|
onChange={(repo) => setSelectedRepository(repo)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="mt-10 border-t border-gray-200 pt-10 ">
|
<div className="mt-10 border-t border-gray-200 pt-10 ">
|
||||||
<h3 className="text-sm font-medium text-foreground">Genres</h3>
|
<h3 className="text-sm font-medium text-foreground">Genres</h3>
|
||||||
|
24
src/app/(frontend)/logout/page.client.tsx
Normal file
24
src/app/(frontend)/logout/page.client.tsx
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useSearchParams } from 'next/navigation'
|
||||||
|
|
||||||
|
const LogoutClientPage = () => {
|
||||||
|
const searchParams = useSearchParams()
|
||||||
|
|
||||||
|
fetch('/api/users/logout', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
}).finally(() => {
|
||||||
|
window.location.href = searchParams.get('redirectUrl') || '/'
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section>
|
||||||
|
<p>Logging you out . . .</p>
|
||||||
|
</section>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default LogoutClientPage
|
12
src/app/(frontend)/logout/page.tsx
Normal file
12
src/app/(frontend)/logout/page.tsx
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Suspense } from 'react'
|
||||||
|
import LogoutClientPage from './page.client'
|
||||||
|
|
||||||
|
const LogoutPage = () => {
|
||||||
|
return (
|
||||||
|
<Suspense>
|
||||||
|
<LogoutClientPage />
|
||||||
|
</Suspense>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default LogoutPage
|
@ -5,7 +5,7 @@ import { Button } from '@/components/ui/button'
|
|||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter, useSearchParams } from 'next/navigation'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useGlobal } from '@/providers/GlobalProvider'
|
import { useGlobal } from '@/providers/GlobalProvider'
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { setUser } = useGlobal()
|
const { setUser } = useGlobal()
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
|
const searchParams = useSearchParams()
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
@ -42,7 +43,7 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||||||
|
|
||||||
setUser(user.user)
|
setUser(user.user)
|
||||||
|
|
||||||
if (user.token) router.push('/')
|
if (user.token) router.push(searchParams.get('redirectUrl') || '/')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error)
|
console.error('Login failed:', error)
|
||||||
} finally {
|
} finally {
|
||||||
@ -68,7 +69,7 @@ export function LoginForm({ className, ...props }: React.ComponentProps<'div'>)
|
|||||||
id="email"
|
id="email"
|
||||||
name="email"
|
name="email"
|
||||||
type="email"
|
type="email"
|
||||||
placeholder="m@example.com"
|
placeholder="me@example.com"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user