Skip to content

Commit

Permalink
add non-redirect link
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed Jan 10, 2025
1 parent 762b2c1 commit 3feec70
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"embla-carousel-react": "^8.5.1",
"emblor": "^1.4.7",
"framer-motion": "^11.15.0",
"gsap": "^3.12.5",
"import-in-the-middle": "^1.12.0",
"input-otp": "^1.4.1",
"lucide-react": "^0.469.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/app/[locale]/(auth)/logout/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@
import { useEffect, useState } from 'react'
import { useUser } from '@/components/contexts/UserContext'
import { useRouter } from '@/i18n/routing'
import { useSearchParams } from 'next/navigation'

export const runtime = 'edge'

export default function LogoutPage() {
const [error, setError] = useState(null)
const { current, logout } = useUser()
const router = useRouter()
const searchParams = useSearchParams()
const redirect = searchParams.get('redirect') !== 'false'

useEffect(() => {
if (!current) {
router.push('/')
if (redirect) {
router.push('/')
}
return
}
logout()
logout(redirect)
.then(() => {
router.push('/')
if (redirect) {
router.push('/')
}
})
.catch((error) => {
setError(error)
})
}, [current, logout, router])
}, [current, logout, router, redirect])

if (error) {
return <div>Error: {error.message}</div>
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/user/account/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createSessionClient } from '@/app/appwrite-session'
import { NextRequest } from 'next/server'

export const runtime = 'edge'

export async function GET(request: Request) {
export async function GET(request: NextRequest) {
if (
request.headers
.get('referer')
Expand Down
31 changes: 17 additions & 14 deletions src/app/api/user/logoutUser/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'
import { cookies } from 'next/headers'
import { createSessionClient } from '@/app/appwrite-session'

export const runtime = 'edge'

export async function POST(request) {
export async function POST(request: NextRequest) {
const { account } = await createSessionClient(request)
const cookie = await cookies()

const redirect = request.nextUrl.searchParams.get('userId') === 'true'

try {
// Delete the specified cookie
;(await cookies()).set(
`a_session_${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`,
'',
{
path: '/',
domain: process.env.NEXT_PUBLIC_COOKIE_DOMAIN,
secure: true,
httpOnly: true,
expires: new Date(0),
}
)
cookie.set(`a_session_${process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID}`, '', {
path: '/',
domain: process.env.NEXT_PUBLIC_COOKIE_DOMAIN,
secure: true,
httpOnly: true,
expires: new Date(0),
})

await account.deleteSession('current')
if (redirect) {
await account.deleteSession('current')
} else {
await account.deleteSessions()
}

return NextResponse.json({ status: 204 })
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/user/oauth/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createAdminClient } from '@/app/appwrite-session'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'

export const runtime = 'edge'

export async function GET(request) {
export async function GET(request: NextRequest) {
const userId = request.nextUrl.searchParams.get('userId')
const secret = request.nextUrl.searchParams.get('secret')
const { account } = await createAdminClient()
Expand Down
6 changes: 3 additions & 3 deletions src/components/contexts/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface UserContextValue {
setUser: React.Dispatch<React.SetStateAction<Account.AccountType | null>>
login: (email: string, password: string) => Promise<void>
loginOAuth: (userId: string, secret: string) => Promise<void>
logout: () => Promise<void>
logout: (redirect: boolean) => Promise<void>
register: (email: string, password: string, username: string) => Promise<void>
}

Expand Down Expand Up @@ -48,9 +48,9 @@ export function UserProvider(props: any) {
setUser(accountData)
}

async function logout() {
async function logout(redirect: boolean) {
try {
fetch(`/api/user/logoutUser`, {
fetch(`/api/user/logoutUser?redirect=${redirect}`, {
method: 'POST',
})
.then((response) => {
Expand Down

0 comments on commit 3feec70

Please sign in to comment.