Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
dtd
/
civitas.ui
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit bff50ad1
authored
Apr 28, 2025
by
Samuel Taniel Mulyadi
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'sam' into 'staging'
done refresehed token See merge request
!24
2 parents
f46013e1
a364264c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
31 deletions
layouts/components/NavbarTokenExpiredTime.vue
plugins/keycloak.ts
layouts/components/NavbarTokenExpiredTime.vue
View file @
bff50ad
...
...
@@ -14,7 +14,7 @@ let timer: ReturnType<typeof setInterval>
onMounted
(()
=>
{
// Set tokenLifetime only once
if
(
keycloakInstance
.
tokenParsed
?.
exp
&&
keycloakInstance
.
tokenParsed
?.
iat
)
tokenLifetime
.
value
=
keycloakInstance
.
tokenParsed
.
exp
-
keycloakInstance
.
t
okenParsed
.
iat
tokenLifetime
.
value
=
keycloakInstance
.
refreshTokenParsed
.
exp
-
keycloakInstance
.
refreshT
okenParsed
.
iat
timer
=
setInterval
(()
=>
{
now
.
value
=
Math
.
floor
(
Date
.
now
()
/
1000
)
...
...
@@ -26,29 +26,30 @@ onUnmounted(() => {
})
const
computedExpIn
=
computed
(()
=>
{
return
authenticated
.
value
&&
keycloakInstance
.
t
okenParsed
?.
exp
?
Math
.
max
(
keycloakInstance
.
t
okenParsed
.
exp
-
now
.
value
,
0
)
return
authenticated
.
value
&&
keycloakInstance
.
refreshT
okenParsed
?.
exp
?
Math
.
max
(
keycloakInstance
.
refreshT
okenParsed
.
exp
-
now
.
value
,
0
)
:
0
})
const
formattedExpIn
=
computed
(()
=>
{
const
minutes
=
Math
.
floor
(
computedExpIn
.
value
/
60
)
const
seconds
=
computedExpIn
.
value
%
60
return
`
${
minutes
}
:
${
seconds
.
toString
().
padStart
(
2
,
'0'
)}
`
})
</
script
>
<
template
>
<div
v-if=
"authenticated"
class=
"me-4"
style=
"position: relative; block-size:
40px; inline-size: 40
px;"
style=
"position: relative; block-size:
55px; inline-size: 55
px;"
>
<VProgressCircular
:model-value=
"(computedExpIn / tokenLifetime) * 100"
:size=
"40"
:width=
"3"
color=
"primary"
/>
<div
class=
"text-subtitle-1 font-weight-bold"
style=
"position: absolute; inset-block-start: 50%; inset-inline-start: 50%; transform: translate(-50%, -50%);"
>
{{
compu
tedExpIn
}}
{{
format
tedExpIn
}}
</div>
</div>
</
template
>
plugins/keycloak.ts
View file @
bff50ad
...
...
@@ -4,18 +4,11 @@ import keycloakInstance from '@/keycloak'
export
default
defineNuxtPlugin
(
async
nuxtApp
=>
{
const
keycloakStore
=
useKeycloakStore
()
/* `const { } = nuxtApp` is a destructuring assignment in JavaScript/TypeScript. In this
case, it is extracting the `` property from the `nuxtApp` object and assigning it to a new
constant named ``. This allows you to access the `` object directly without having
to use `nuxtApp.` every time. It's a shorthand way of accessing nested properties in
objects. */
// const { $router } = nuxtApp
try
{
const
authenticated
=
await
keycloakInstance
.
init
({
onLoad
:
'check-sso'
,
checkLoginIframe
:
true
,
checkLoginIframeInterval
:
5
,
//
in seconds, default is 5
checkLoginIframeInterval
:
5
,
//
seconds
})
keycloakStore
.
authenticated
=
authenticated
...
...
@@ -24,26 +17,39 @@ export default defineNuxtPlugin(async nuxtApp => {
keycloakStore
.
refresh
()
console
.
log
(
'User is authenticated'
)
// $router.push('/profile')
// console.log('Suppose to navigate To')
setInterval
(()
=>
{
setInterval
(
async
()
=>
{
const
now
=
Math
.
floor
(
Date
.
now
()
/
1000
)
const
tokenExp
=
keycloakInstance
.
tokenParsed
?.
exp
??
0
const
accessTokenExp
=
keycloakInstance
.
tokenParsed
?.
exp
??
0
const
refreshTokenExp
=
keycloakInstance
.
refreshTokenParsed
?.
exp
??
0
if
(
refreshTokenExp
<=
now
)
{
console
.
warn
(
'Refresh token expired. Logging out...'
)
await
keycloakInstance
.
logout
({
redirectUri
:
`
${
window
.
location
.
origin
}
/login`
})
return
}
if
(
tokenExp
<=
now
)
{
console
.
warn
(
'Token expired. Logging out...'
)
keycloakInstance
.
logout
({
redirectUri
:
`
${
window
.
location
.
origin
}
/login`
})
// Refresh the token if the access token is close to expiring (e.g., within 60 seconds)
if
(
accessTokenExp
-
now
<
60
)
{
try
{
const
refreshed
=
await
keycloakInstance
.
updateToken
(
60
)
if
(
refreshed
)
{
console
.
log
(
'Access token refreshed'
)
keycloakStore
.
refresh
()
// update store data if necessary
}
else
{
// console.log('Token expires in:', tokenExp - now, 'seconds')
console
.
log
(
'Access token still valid, no need to refresh'
)
}
}
catch
(
err
)
{
console
.
error
(
'Failed to refresh token'
,
err
)
await
keycloakInstance
.
logout
({
redirectUri
:
`
${
window
.
location
.
origin
}
/login`
})
}
},
10
_000
)
}
},
10
_000
)
// check every 10 seconds
}
else
{
console
.
log
(
'User is not authenticated'
)
// $router.push('/login')
}
}
catch
(
error
)
{
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment