frontend changes

This commit is contained in:
Chandini 2025-09-18 08:36:56 +05:30
parent 48023f9333
commit 6baf9080a7
5 changed files with 45 additions and 11 deletions

View File

@ -131,7 +131,8 @@ const addTokenRefreshInterceptor = (client: typeof authApiClient) => {
safeLocalStorage.removeItem('codenuk_user'); safeLocalStorage.removeItem('codenuk_user');
// Prevent redirect loops by checking current location // Prevent redirect loops by checking current location
if (typeof window !== 'undefined' && !window.location.pathname.includes('/signin')) { if (typeof window !== 'undefined' && !window.location.pathname.includes('/signin')) {
window.location.href = '/signin?error=Please sign in to continue.'; const returnUrl = encodeURIComponent(window.location.pathname + window.location.search)
window.location.href = `/signin?error=Please sign in to continue.&returnUrl=${returnUrl}`;
} }
return Promise.reject(error); return Promise.reject(error);
} catch (refreshError) { } catch (refreshError) {
@ -140,7 +141,8 @@ const addTokenRefreshInterceptor = (client: typeof authApiClient) => {
safeLocalStorage.removeItem('codenuk_user'); safeLocalStorage.removeItem('codenuk_user');
// Prevent redirect loops by checking current location // Prevent redirect loops by checking current location
if (typeof window !== 'undefined' && !window.location.pathname.includes('/signin')) { if (typeof window !== 'undefined' && !window.location.pathname.includes('/signin')) {
window.location.href = '/signin?error=Session expired. Please sign in again.'; const returnUrl = encodeURIComponent(window.location.pathname + window.location.search)
window.location.href = `/signin?error=Session expired. Please sign in again.&returnUrl=${returnUrl}`;
} }
return Promise.reject(refreshError); return Promise.reject(refreshError);
} }

View File

@ -27,6 +27,16 @@ export function SignInForm({ }: SignInFormProps) {
const router = useRouter() const router = useRouter()
const { setUserFromApi } = useAuth() const { setUserFromApi } = useAuth()
// Get return URL from query parameters
const getReturnUrl = () => {
if (typeof window !== 'undefined') {
const urlParams = new URLSearchParams(window.location.search)
const returnUrl = urlParams.get('returnUrl')
return returnUrl ? decodeURIComponent(returnUrl) : null
}
return null
}
const handleSubmit = async (e: React.FormEvent) => { const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault() e.preventDefault()
@ -48,9 +58,12 @@ export function SignInForm({ }: SignInFormProps) {
// Persist for refresh // Persist for refresh
localStorage.setItem("codenuk_user", JSON.stringify(response.data.user)) localStorage.setItem("codenuk_user", JSON.stringify(response.data.user))
// Redirect based on user role with error handling // Redirect based on return URL or user role
try { try {
if (response.data.user.role === 'admin') { const returnUrl = getReturnUrl()
if (returnUrl && returnUrl !== '/signin' && returnUrl !== '/signup') {
router.push(returnUrl)
} else if (response.data.user.role === 'admin') {
router.push("/admin") router.push("/admin")
} else { } else {
router.push("/") router.push("/")
@ -58,7 +71,12 @@ export function SignInForm({ }: SignInFormProps) {
} catch (redirectError) { } catch (redirectError) {
console.error('Redirect failed:', redirectError) console.error('Redirect failed:', redirectError)
// Fallback redirect // Fallback redirect
window.location.href = response.data.user.role === 'admin' ? '/admin' : '/' const returnUrl = getReturnUrl()
if (returnUrl && returnUrl !== '/signin' && returnUrl !== '/signup') {
window.location.href = returnUrl
} else {
window.location.href = response.data.user.role === 'admin' ? '/admin' : '/'
}
} }
} else { } else {
setError("Invalid response from server. Please try again.") setError("Invalid response from server. Please try again.")

View File

@ -73,12 +73,16 @@ export function SignUpForm({ onSignUpSuccess }: SignUpFormProps) {
// Default behavior - redirect to signin with message // Default behavior - redirect to signin with message
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
const message = encodeURIComponent("Account created successfully! Please check your email to verify your account.") const message = encodeURIComponent("Account created successfully! Please check your email to verify your account.")
// Preserve return URL if it exists
const urlParams = new URLSearchParams(window.location.search)
const returnUrl = urlParams.get('returnUrl')
const redirectUrl = returnUrl ? `/signin?message=${message}&returnUrl=${returnUrl}` : `/signin?message=${message}`
try { try {
router.push(`/signin?message=${message}`) router.push(redirectUrl)
} catch (redirectError) { } catch (redirectError) {
console.error('Signup form redirect failed:', redirectError) console.error('Signup form redirect failed:', redirectError)
// Fallback redirect // Fallback redirect
window.location.href = `/signin?message=${message}` window.location.href = redirectUrl
} }
} }
} }

View File

@ -16,12 +16,16 @@ export function SignUpPage() {
setTimeout(() => { setTimeout(() => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
const message = encodeURIComponent("Please check your email to verify your account") const message = encodeURIComponent("Please check your email to verify your account")
// Preserve return URL if it exists
const urlParams = new URLSearchParams(window.location.search)
const returnUrl = urlParams.get('returnUrl')
const redirectUrl = returnUrl ? `/signin?message=${message}&returnUrl=${returnUrl}` : `/signin?message=${message}`
try { try {
router.push(`/signin?message=${message}`) router.push(redirectUrl)
} catch (redirectError) { } catch (redirectError) {
console.error('Signup redirect failed:', redirectError) console.error('Signup redirect failed:', redirectError)
// Fallback redirect // Fallback redirect
window.location.href = `/signin?message=${message}` window.location.href = redirectUrl
} }
} }
}, 3000) }, 3000)

View File

@ -339,7 +339,10 @@ function TemplateSelectionStep({ onNext }: { onNext: (template: Template) => voi
<p className="text-white/60">{error}</p> <p className="text-white/60">{error}</p>
<div className="flex justify-center space-x-4"> <div className="flex justify-center space-x-4">
<Button <Button
onClick={() => window.location.href = '/signin'} onClick={() => {
const returnUrl = encodeURIComponent(window.location.pathname + window.location.search)
window.location.href = `/signin?returnUrl=${returnUrl}`
}}
className="bg-orange-500 hover:bg-orange-400 text-black" className="bg-orange-500 hover:bg-orange-400 text-black"
> >
Sign In Sign In
@ -462,7 +465,10 @@ function TemplateSelectionStep({ onNext }: { onNext: (template: Template) => voi
You&apos;re currently viewing public templates. You&apos;re currently viewing public templates.
<Button <Button
variant="link" variant="link"
onClick={() => window.location.href = '/signin'} onClick={() => {
const returnUrl = encodeURIComponent(window.location.pathname + window.location.search)
window.location.href = `/signin?returnUrl=${returnUrl}`
}}
className="text-orange-400 hover:text-orange-300 p-0 h-auto font-semibold ml-1" className="text-orange-400 hover:text-orange-300 p-0 h-auto font-semibold ml-1"
> >
Sign in Sign in