Skip to content

Commit

Permalink
Optimize login and build process (#739)
Browse files Browse the repository at this point in the history
* handle await response

* Bug fix for jwt token refresh

* Enable logs in development

* Optimize vite build

* Remove ckeditor chunk
  • Loading branch information
hiveer authored Oct 28, 2024
1 parent 343a2a4 commit 3004e1e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
6 changes: 0 additions & 6 deletions frontend/src/packs/refreshJWT.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ const refreshJWT = async () => {
const currentTime = Date.now()/1000;
const loginIdentity = cookies.get('login_identity');

// user logged in
if(loginIdentity) {
// jwt exists
if (jwt) {
const jwtInfos = jwtDecode(jwt);
const expireTime = jwtInfos.exp;
if (currentTime >= expireTime) {
// user token expired, refresh token
console.log('refresh jwt')
await fetch('/internal_api/users/jwt_token', {method: 'PUT'})
} else {
// if user token will expire soon, refresh
// if user token will not expire soon, do nothing
const differenceInMinutes = Math.floor((expireTime - currentTime) / (60));
if (differenceInMinutes < 120) {
console.log('refresh jwt')
await fetch('/internal_api/users/jwt_token', {method: 'PUT'})
}
}
} else {
console.log('refresh jwt')
await fetch('/internal_api/users/jwt_token', {method: 'PUT'})
}
}
Expand Down
9 changes: 2 additions & 7 deletions frontend/src/packs/useFetchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@ const useFetchApi = createFetch({
combination: 'chain',
options: {
async beforeFetch({ options }) {
await refreshJWT()

const loginIdentity = cookies.get('login_identity')
const jwtToken = cookies.get('user_token')

// if user is login send the request with jwt token no matter what the status of the token
// and handle the error if its failed
if (loginIdentity) {
await refreshJWT()
const jwtToken = cookies.get('user_token')
options.headers = {
...options.headers,
Authorization: `Bearer ${jwtToken}`
}
}

return { options }
},
onFetchError({ data, error, response }) {
Expand Down
14 changes: 12 additions & 2 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ export default defineConfig((configEnv) => {
input: getHtmlEntryFiles('src'),
output: {
manualChunks(id) {
// console.log('Processing:', id);
if (id.includes('node_modules')) {
if (id.includes('lodash')) {
return 'lodash';
}
if (id.includes('element-plus')) {
return 'element-plus';
}
if (id.includes('highlight.js')) {
return 'highlightjs';
}
return 'vendor'
}
if (id.includes('src/components')) {
Expand All @@ -26,8 +36,8 @@ export default defineConfig((configEnv) => {
minify: 'terser',
terserOptions: {
compress: {
drop_console: true, // Remove console logs
drop_debugger: true // Remove debugger statements
drop_console: configEnv.mode !== 'development', // Remove console logs
drop_debugger: configEnv.mode !== 'development' // Remove debugger statements
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/frontend/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func (i *TokenHandlerImpl) RefreshToken(c *gin.Context) {

c.SetCookie("user_token", r.Data.Token, 3600*24*7, "/", "", false, false)

c.JSON(http.StatusOK, gin.H{"message": "Token set successfully"})
c.JSON(http.StatusOK, gin.H{"jwt_token": r.Data.Token})
}

0 comments on commit 3004e1e

Please sign in to comment.