Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sweepstake
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Arjun Jhukal
sweepstake
Commits
2ddcb28d
Commit
2ddcb28d
authored
Mar 13, 2026
by
Arjun Jhukal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated the private checkout and other and changed the solution from session to cookie
parent
0ace8a5b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
47 deletions
+127
-47
page.tsx
...d)/(user)/(privateUser)/buy-coins/[slug]/success/page.tsx
+25
-0
index.tsx
...erDashboard/buyCoins/buyCoinSinlgeGame/checkout/index.tsx
+4
-35
Private.tsx
src/routes/Private.tsx
+33
-12
authSession.ts
src/utils/authSession.ts
+65
-0
No files found.
src/app/(dashboard)/(user)/(privateUser)/buy-coins/[slug]/success/page.tsx
View file @
2ddcb28d
// app/buy-coins/[slug]/success/page.tsx
"use client"
import
GlassWrapper
from
'@/components/molecules/GlassWrapper'
import
{
restoreAuthFromCookies
}
from
'@/utils/authSession'
import
Image
from
'next/image'
import
Link
from
'next/link'
import
{
useParams
}
from
'next/navigation'
import
{
useEffect
,
useState
}
from
'react'
export
default
function
PaymentSuccess
()
{
const
params
=
useParams
();
const
slug
=
params
?.
slug
as
string
;
const
[
isReady
,
setIsReady
]
=
useState
(
false
);
useEffect
(()
=>
{
const
wasRestored
=
restoreAuthFromCookies
();
if
(
wasRestored
)
{
window
.
location
.
reload
();
return
;
}
setIsReady
(
true
);
},
[]);
if
(
!
isReady
)
{
return
(
<
div
className=
"flex items-center justify-center min-h-[200px]"
>
<
div
className=
"animate-spin rounded-full h-8 w-8 border-b-2 border-white"
></
div
>
</
div
>
);
}
return
(
<
GlassWrapper
className=
"max-w-[520px] mx-auto flex flex-col gap-3 items-center text-center p-6"
>
<
Image
...
...
src/components/pages/dashboard/userDashboard/buyCoins/buyCoinSinlgeGame/checkout/index.tsx
View file @
2ddcb28d
...
...
@@ -6,6 +6,7 @@ import BitCoinIcon from '@/icons/BitCoinIcon';
import
GoldCoinIcon
from
'@/icons/GoldCoinIcon'
;
import
{
useDepositMutation
}
from
'@/services/transaction'
;
import
{
showToast
,
ToastVariant
}
from
'@/slice/toastSlice'
;
import
{
backupAuthToCookies
,
restoreAuthFromCookies
}
from
'@/utils/authSession'
;
import
{
Box
,
Button
}
from
'@mui/material'
;
import
{
Card
,
TickCircle
}
from
'@wandersonalwes/iconsax-react'
;
import
Image
from
'next/image'
;
...
...
@@ -14,37 +15,7 @@ import PaymentForm from './FortPay';
export
type
PaymentModeProps
=
"crypto"
|
"fortpay"
const
AUTH_KEYS
=
[
'token'
,
'access_token'
,
'authToken'
,
'user'
,
'refresh_token'
];
const
BACKUP_PREFIX
=
'__payment_backup__'
;
function
backupAuthToSession
()
{
AUTH_KEYS
.
forEach
((
key
)
=>
{
const
value
=
localStorage
.
getItem
(
key
);
if
(
value
!==
null
)
{
sessionStorage
.
setItem
(
`
${
BACKUP_PREFIX
}${
key
}
`
,
value
);
}
});
sessionStorage
.
setItem
(
`
${
BACKUP_PREFIX
}
redirected`
,
'true'
);
}
function
restoreAuthFromSession
()
{
const
wasRedirected
=
sessionStorage
.
getItem
(
`
${
BACKUP_PREFIX
}
redirected`
);
if
(
!
wasRedirected
)
return
;
AUTH_KEYS
.
forEach
((
key
)
=>
{
const
backup
=
sessionStorage
.
getItem
(
`
${
BACKUP_PREFIX
}${
key
}
`
);
if
(
backup
!==
null
)
{
if
(
localStorage
.
getItem
(
key
)
===
null
)
{
localStorage
.
setItem
(
key
,
backup
);
}
}
});
AUTH_KEYS
.
forEach
((
key
)
=>
{
sessionStorage
.
removeItem
(
`
${
BACKUP_PREFIX
}${
key
}
`
);
});
sessionStorage
.
removeItem
(
`
${
BACKUP_PREFIX
}
redirected`
);
}
export
default
function
CheckoutPage
({
amount
,
slug
,
bonus
}:
{
amount
:
number
;
...
...
@@ -56,7 +27,7 @@ export default function CheckoutPage({ amount, slug, bonus }: {
const
[
currentPaymentMode
,
setCurrentPaymentMode
]
=
React
.
useState
(
"crypto"
);
useEffect
(()
=>
{
restoreAuthFrom
Session
();
restoreAuthFrom
Cookies
();
},
[]);
return
(
...
...
@@ -161,10 +132,8 @@ export default function CheckoutPage({ amount, slug, bonus }: {
type
:
currentPaymentMode
as
PaymentModeProps
}).
unwrap
();
backupAuthToSession
();
backupAuthToCookies
();
window
.
open
(
response
?.
data
?.
payment_url
,
"_blank"
);
// window.location.href = response?.data?.payment_url;
}
catch
(
e
:
any
)
{
dispatch
(
...
...
src/routes/Private.tsx
View file @
2ddcb28d
// components/Private.tsx
"use client"
;
import
{
useAppDispatch
,
useAppSelector
}
from
"@/hooks/hook"
;
import
{
clearTokens
,
setTokens
}
from
"@/slice/authSlice"
;
import
{
restoreAuthFromCookies
}
from
"@/utils/authSession"
;
import
Cookies
from
"js-cookie"
;
import
{
useRouter
}
from
"next/navigation"
;
import
React
,
{
useEffect
}
from
"react"
;
import
React
,
{
useEffect
,
useState
}
from
"react"
;
function
isTokenExpired
(
token
:
string
):
boolean
{
try
{
...
...
@@ -14,7 +17,7 @@ function isTokenExpired(token: string): boolean {
const
now
=
Math
.
floor
(
Date
.
now
()
/
1000
);
return
exp
<
now
;
}
catch
(
error
)
{
console
.
error
(
"Failed to decode token:"
,
error
);
console
.
error
(
"Failed to decode token:"
,
error
);
return
true
;
}
}
...
...
@@ -22,33 +25,50 @@ function isTokenExpired(token: string): boolean {
export
default
function
Private
({
children
}:
{
children
:
React
.
ReactNode
})
{
const
router
=
useRouter
();
const
dispatch
=
useAppDispatch
();
const
[
isHydrating
,
setIsHydrating
]
=
useState
(
true
);
const
user
=
useAppSelector
((
state
)
=>
state
.
auth
.
user
);
const
token
=
useAppSelector
((
state
)
=>
state
.
auth
.
access_token
);
useEffect
(()
=>
{
// First, try to restore auth from cookies (for payment redirects)
const
wasRestored
=
restoreAuthFromCookies
();
if
(
wasRestored
)
{
window
.
location
.
reload
();
return
;
}
const
accessToken
=
token
||
Cookies
.
get
(
"access_token"
);
if
(
!
accessToken
||
isTokenExpired
(
accessToken
))
{
dispatch
(
clearTokens
());
// router.replace("/"
);
setIsHydrating
(
false
);
return
;
}
// ✅ optional: if Redux was empty, rehydrate it from cookie
if
(
!
token
&&
accessToken
)
{
dispatch
(
setTokens
({
access_token
:
accessToken
,
user
:
user
||
null
}));
}
// if (!user) {
// router.replace("/");
// }
setIsHydrating
(
false
);
},
[
token
,
user
,
dispatch
,
router
]);
if
(
!
user
)
return
null
;
return
<>
{
children
}
</>;
}
if
(
isHydrating
)
{
return
(
<
div
className=
"flex items-center justify-center min-h-[200px]"
>
<
div
className=
"animate-spin rounded-full h-8 w-8 border-b-2 border-white"
></
div
>
</
div
>
);
}
if
(
!
user
)
{
return
(
<
div
className=
"flex items-center justify-center min-h-[200px] text-white"
>
<
p
>
User Not Found. Please log in again.
</
p
>
</
div
>
);
}
return
<>
{
children
}
</>;
}
\ No newline at end of file
src/utils/authSession.ts
0 → 100644
View file @
2ddcb28d
// utils/authSession.ts
import
Cookies
from
'js-cookie'
;
const
AUTH_KEYS
=
[
'token'
,
'access_token'
,
'authToken'
,
'user'
,
'refresh_token'
];
const
BACKUP_PREFIX
=
'__payment_backup__'
;
const
COOKIE_EXPIRY
=
1
/
24
;
// 1 hour (in days)
export
function
backupAuthToCookies
()
{
if
(
typeof
window
===
'undefined'
)
return
;
AUTH_KEYS
.
forEach
((
key
)
=>
{
const
value
=
localStorage
.
getItem
(
key
);
if
(
value
!==
null
)
{
// Store in cookie with 1 hour expiry
Cookies
.
set
(
`
${
BACKUP_PREFIX
}${
key
}
`
,
value
,
{
expires
:
COOKIE_EXPIRY
,
secure
:
true
,
sameSite
:
'lax'
});
}
});
Cookies
.
set
(
`
${
BACKUP_PREFIX
}
redirected`
,
'true'
,
{
expires
:
COOKIE_EXPIRY
,
secure
:
true
,
sameSite
:
'lax'
});
}
export
function
restoreAuthFromCookies
():
boolean
{
if
(
typeof
window
===
'undefined'
)
return
false
;
const
wasRedirected
=
Cookies
.
get
(
`
${
BACKUP_PREFIX
}
redirected`
);
if
(
!
wasRedirected
)
return
false
;
let
restored
=
false
;
AUTH_KEYS
.
forEach
((
key
)
=>
{
const
backup
=
Cookies
.
get
(
`
${
BACKUP_PREFIX
}${
key
}
`
);
if
(
backup
!==
undefined
&&
backup
!==
null
)
{
if
(
localStorage
.
getItem
(
key
)
===
null
)
{
localStorage
.
setItem
(
key
,
backup
);
restored
=
true
;
}
}
});
// Clean up cookies
AUTH_KEYS
.
forEach
((
key
)
=>
{
Cookies
.
remove
(
`
${
BACKUP_PREFIX
}${
key
}
`
);
});
Cookies
.
remove
(
`
${
BACKUP_PREFIX
}
redirected`
);
return
restored
;
}
export
function
clearAuthBackup
()
{
if
(
typeof
window
===
'undefined'
)
return
;
AUTH_KEYS
.
forEach
((
key
)
=>
{
Cookies
.
remove
(
`
${
BACKUP_PREFIX
}${
key
}
`
);
});
Cookies
.
remove
(
`
${
BACKUP_PREFIX
}
redirected`
);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment