Browse Source

fix(web): fix redirect query not redirecting after login issue

pull/52/head
isra el 1 year ago
parent
commit
3ee58b28f2
  1. 6
      web/app/(app)/(auth)/(components)/login-with-google.tsx
  2. 11
      web/app/(app)/(auth)/login/page.tsx
  3. 11
      web/app/(app)/(auth)/register/page.tsx

6
web/app/(app)/(auth)/(components)/login-with-google.tsx

@ -4,10 +4,12 @@ import { Routes } from '@/config/routes'
import { toast } from '@/hooks/use-toast' import { toast } from '@/hooks/use-toast'
import { CredentialResponse, GoogleLogin } from '@react-oauth/google' import { CredentialResponse, GoogleLogin } from '@react-oauth/google'
import { signIn } from 'next-auth/react' import { signIn } from 'next-auth/react'
import { useRouter } from 'next/navigation'
import { useRouter, useSearchParams } from 'next/navigation'
export default function LoginWithGoogle() { export default function LoginWithGoogle() {
const router = useRouter() const router = useRouter()
const searchParams = useSearchParams()
const redirect = searchParams.get('redirect')
const onGoogleLoginSuccess = async ( const onGoogleLoginSuccess = async (
credentialResponse: CredentialResponse credentialResponse: CredentialResponse
@ -21,7 +23,7 @@ export default function LoginWithGoogle() {
redirect: false, redirect: false,
idToken: credentialResponse.credential, idToken: credentialResponse.credential,
}) })
router.push(Routes.dashboard)
router.push(redirect ? decodeURIComponent(redirect) : Routes.dashboard)
} }
const onGoogleLoginError = () => { const onGoogleLoginError = () => {

11
web/app/(app)/(auth)/login/page.tsx

@ -1,6 +1,7 @@
'use client' 'use client'
import Link from 'next/link' import Link from 'next/link'
import { useSearchParams } from 'next/navigation'
import { import {
Card, Card,
@ -16,6 +17,9 @@ import LoginForm from '../(components)/login-form'
import { Routes } from '@/config/routes' import { Routes } from '@/config/routes'
export default function LoginPage() { export default function LoginPage() {
const searchParams = useSearchParams()
const redirect = searchParams.get('redirect')
return ( return (
<div className='flex items-center justify-center min-h-screen bg-gray-100 dark:bg-muted'> <div className='flex items-center justify-center min-h-screen bg-gray-100 dark:bg-muted'>
<Card className='w-[400px] shadow-lg'> <Card className='w-[400px] shadow-lg'>
@ -53,7 +57,12 @@ export default function LoginPage() {
<p className='text-sm text-gray-600'> <p className='text-sm text-gray-600'>
Don&apos;t have an account?{' '} Don&apos;t have an account?{' '}
<Link <Link
href={Routes.register}
href={{
pathname: Routes.register,
query: {
redirect: redirect ? decodeURIComponent(redirect) : undefined,
},
}}
className='font-medium text-blue-600 hover:underline' className='font-medium text-blue-600 hover:underline'
> >
Sign up Sign up

11
web/app/(app)/(auth)/register/page.tsx

@ -13,8 +13,12 @@ import {
import LoginWithGoogle from '../(components)/login-with-google' import LoginWithGoogle from '../(components)/login-with-google'
import RegisterForm from '../(components)/register-form' import RegisterForm from '../(components)/register-form'
import { Routes } from '@/config/routes' import { Routes } from '@/config/routes'
import { useSearchParams } from 'next/navigation'
export default function RegisterPage() { export default function RegisterPage() {
const searchParams = useSearchParams()
const redirect = searchParams.get('redirect')
return ( return (
<div className='flex items-center justify-center min-h-screen bg-gray-100 dark:bg-muted'> <div className='flex items-center justify-center min-h-screen bg-gray-100 dark:bg-muted'>
<Card className='w-[450px] shadow-lg'> <Card className='w-[450px] shadow-lg'>
@ -46,7 +50,12 @@ export default function RegisterPage() {
<p className='text-sm text-gray-600'> <p className='text-sm text-gray-600'>
Already have an account?{' '} Already have an account?{' '}
<Link <Link
href={Routes.login}
href={{
pathname: Routes.login,
query: {
redirect: redirect ? decodeURIComponent(redirect) : undefined,
},
}}
className='font-medium text-blue-600 hover:underline' className='font-medium text-blue-600 hover:underline'
> >
Sign in Sign in

Loading…
Cancel
Save