Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/runtime/composables/useAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ export const useAuthentication = () => {
// Remove password from user data before storing
const { password: _, ...userWithoutPassword } = userData
user.value = userWithoutPassword

// Store user data based on rememberMe preference
if (import.meta.client) {
// Clear any existing user data from both storages
localStorage.removeItem('user')
sessionStorage.removeItem('user')

if (rememberMe) {
// Store in localStorage for persistent login across browser sessions
localStorage.setItem('user', JSON.stringify(userWithoutPassword))
} else {
}
else {
// Store in sessionStorage for session-only login
sessionStorage.setItem('user', JSON.stringify(userWithoutPassword))
}
Expand Down Expand Up @@ -55,18 +56,20 @@ export const useAuthentication = () => {
try {
const response = await $fetch<{ user: UserWithoutPassword }>(`${apiBasePath}/me`, { method: 'GET' })
user.value = response.user

// Update the appropriate storage with fresh user data
if (import.meta.client) {
// Determine which storage was being used and update it
const wasInLocalStorage = localStorage.getItem('user') !== null
const wasInSessionStorage = sessionStorage.getItem('user') !== null

if (wasInLocalStorage) {
localStorage.setItem('user', JSON.stringify(response.user))
} else if (wasInSessionStorage) {
}
else if (wasInSessionStorage) {
sessionStorage.setItem('user', JSON.stringify(response.user))
} else {
}
else {
// Default to sessionStorage for new sessions without rememberMe
sessionStorage.setItem('user', JSON.stringify(response.user))
}
Expand Down Expand Up @@ -95,7 +98,7 @@ export const useAuthentication = () => {
const storedUserLocal = localStorage.getItem('user')
const storedUserSession = sessionStorage.getItem('user')
const storedUser = storedUserLocal || storedUserSession

if (storedUser) {
try {
// First set the user from storage for immediate UI response
Expand Down
10 changes: 6 additions & 4 deletions src/runtime/server/api/nuxt-users/session/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export default defineEventHandler(async (event) => {
// For "remember me" sessions, use a longer expiration (30 days default)
const longTermDays = options.auth.rememberMeExpiration || 30 // days
expiresAt.setDate(expiresAt.getDate() + longTermDays)
} else {
}
else {
// For regular sessions, use the configured token expiration
expiresAt.setMinutes(expiresAt.getMinutes() + options.auth.tokenExpiration)
}
Expand All @@ -78,16 +79,17 @@ export default defineEventHandler(async (event) => {
sameSite: 'lax' as const, // Adjust as needed
path: '/',
}

if (rememberMe) {
// For "remember me", set a long-term cookie
const longTermDays = options.auth.rememberMeExpiration || 30
cookieOptions.maxAge = 60 * 60 * 24 * longTermDays // Convert days to seconds
} else {
}
else {
// For regular login, don't set maxAge to create a session cookie
// Session cookies expire when the browser is closed
}

setCookie(event, 'auth_token', token, cookieOptions)

// Return user without password for security
Expand Down
56 changes: 29 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,13 @@
dependencies:
eslint-visitor-keys "^3.4.3"

"@eslint-community/eslint-utils@^4.8.0":
version "4.9.0"
resolved "https://registry.yarnpkg.com/@eslint-community/eslint-utils/-/eslint-utils-4.9.0.tgz#7308df158e064f0dd8b8fdb58aa14fa2a7f913b3"
integrity sha512-ayVFHdtZ+hsq1t2Dy24wCmGXGe4q9Gu3smhLYALJrr473ZH27MsnSL+LKUlimp4BWJqMDMLmPpx/Q9R3OAlL4g==
dependencies:
eslint-visitor-keys "^3.4.3"

"@eslint-community/regexpp@^4.10.0", "@eslint-community/regexpp@^4.11.0", "@eslint-community/regexpp@^4.12.1", "@eslint-community/regexpp@^4.8.0":
version "4.12.1"
resolved "https://registry.yarnpkg.com/@eslint-community/regexpp/-/regexpp-4.12.1.tgz#cfc6cffe39df390a3841cde2abccf92eaa7ae0e0"
Expand All @@ -1467,12 +1474,12 @@
debug "^4.3.1"
minimatch "^3.1.2"

"@eslint/config-helpers@^0.3.0":
"@eslint/config-helpers@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@eslint/config-helpers/-/config-helpers-0.3.1.tgz#d316e47905bd0a1a931fa50e669b9af4104d1617"
integrity sha512-xR93k9WhrDYpXHORXpxVL5oHj3Era7wo6k/Wd8/IsQNnZUTzkGS29lyn3nAT05v6ltUuTFVCCYDEGfy2Or/sPA==

"@eslint/core@^0.15.0", "@eslint/core@^0.15.2":
"@eslint/core@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@eslint/core/-/core-0.15.2.tgz#59386327d7862cc3603ebc7c78159d2dcc4a868f"
integrity sha512-78Md3/Rrxh83gCxoUc0EiciuOHsIITzLy53m3d9UyiW8y9Dj2D29FeETqyKA+BRK76tnTp6RXWb3pCay8Oyomg==
Expand All @@ -1494,10 +1501,10 @@
minimatch "^3.1.2"
strip-json-comments "^3.1.1"

"@eslint/js@9.32.0":
version "9.32.0"
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-9.32.0.tgz#a02916f58bd587ea276876cb051b579a3d75d091"
integrity sha512-BBpRFZK3eX6uMLKz8WxFOBIFFcGFJ/g8XuwjTHCqHROSIsopI+ddn/d5Cfh36+7+e5edVS8dbSHnBNhrLEX0zg==
"@eslint/js@9.36.0":
version "9.36.0"
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-9.36.0.tgz#b1a3893dd6ce2defed5fd49de805ba40368e8fef"
integrity sha512-uhCbYtYynH30iZErszX78U+nR3pJU3RHGQ57NXy5QupD4SBVwDeU8TNBy+MjMngc1UyIW9noKqsRqfjQTBU2dw==

"@eslint/js@^9.33.0":
version "9.33.0"
Expand All @@ -1509,7 +1516,7 @@
resolved "https://registry.yarnpkg.com/@eslint/object-schema/-/object-schema-2.1.6.tgz#58369ab5b5b3ca117880c0f6c0b0f32f6950f24f"
integrity sha512-RBMg5FRL0I0gs51M/guSAj5/e14VQ4tpZnQNWwuDT66P14I43ItmPfIZRhO9fUVIPOAQXU47atlywZ/czoqFPA==

"@eslint/plugin-kit@^0.3.3", "@eslint/plugin-kit@^0.3.4":
"@eslint/plugin-kit@^0.3.3", "@eslint/plugin-kit@^0.3.5":
version "0.3.5"
resolved "https://registry.yarnpkg.com/@eslint/plugin-kit/-/plugin-kit-0.3.5.tgz#fd8764f0ee79c8ddab4da65460c641cefee017c5"
integrity sha512-Z5kJ+wU3oA7MMIqVR9tyZRtjYPr4OC004Q4Rw7pgOKUOKkJfZ3O24nz3WYfGRpMDNmcOi3TwQOmgm7B7Tpii0w==
Expand All @@ -1528,24 +1535,19 @@
integrity sha512-5DyQ4+1JEUzejeK1JGICcideyfUbGixgS9jNgex5nqkW+cY7WZhxBigmieN5Qnw9ZosSNVC9KQKyb+GUaGyKUA==

"@humanfs/node@^0.16.6":
version "0.16.6"
resolved "https://registry.yarnpkg.com/@humanfs/node/-/node-0.16.6.tgz#ee2a10eaabd1131987bf0488fd9b820174cd765e"
integrity sha512-YuI2ZHQL78Q5HbhDiBA1X4LmYdXCKCMQIfw0pw7piHJwyREFebJUvrQN4cMssyES6x+vfUbx1CIpaQUKYdQZOw==
version "0.16.7"
resolved "https://registry.yarnpkg.com/@humanfs/node/-/node-0.16.7.tgz#822cb7b3a12c5a240a24f621b5a2413e27a45f26"
integrity sha512-/zUx+yOsIrG4Y43Eh2peDeKCxlRt/gET6aHfaKpuq267qXdYDFViVHfMaLyygZOnl0kGWxFIgsBy8QFuTLUXEQ==
dependencies:
"@humanfs/core" "^0.19.1"
"@humanwhocodes/retry" "^0.3.0"
"@humanwhocodes/retry" "^0.4.0"

"@humanwhocodes/module-importer@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@humanwhocodes/module-importer/-/module-importer-1.0.1.tgz#af5b2691a22b44be847b0ca81641c5fb6ad0172c"
integrity sha512-bxveV4V8v5Yb4ncFTT3rPSgZBOpCkjfK0y4oVVVJwIuDVBRMDXrPyXRL988i5ap9m9bnyEEjWfm5WkBmtffLfA==

"@humanwhocodes/retry@^0.3.0":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@humanwhocodes/retry/-/retry-0.3.1.tgz#c72a5c76a9fbaf3488e231b13dc52c0da7bab42a"
integrity sha512-JBxkERygn7Bv/GbN5Rv8Ul6LVknS+5Bp6RgDC/O8gEBU/yeH5Ui5C/OlWrTb6qct7LjjfT6Re2NxB0ln0yYybA==

"@humanwhocodes/retry@^0.4.2":
"@humanwhocodes/retry@^0.4.0", "@humanwhocodes/retry@^0.4.2":
version "0.4.3"
resolved "https://registry.yarnpkg.com/@humanwhocodes/retry/-/retry-0.4.3.tgz#c2b9d2e374ee62c586d3adbea87199b1d7a7a6ba"
integrity sha512-bV0Tgo9K4hfPCek+aMAn81RppFKv2ySDQeMoSZuvTASywNTnVJCArCZE2FWqpvIatKu7VMRLWlR1EazvVhDyhQ==
Expand Down Expand Up @@ -4961,14 +4963,14 @@ de-indent@^1.0.2:
resolved "https://registry.yarnpkg.com/de-indent/-/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d"
integrity sha512-e/1zu3xH5MQryN2zdVaF0OrdNLUbvWxzMbi+iNA6Bky7l1RoP8a2fIbRocyHclXt/arDrrR6lL3TqFD9pMQTsg==

debug@4, debug@^4.1.0, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4, debug@^4.3.5:
debug@4, debug@^4.1.0, debug@^4.3.4, debug@^4.3.5:
version "4.4.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.1.tgz#e5a8bc6cbc4c6cd3e64308b0693a3d4fa550189b"
integrity sha512-KcKCqiftBJcZr++7ykoDIEwSa3XWowTfNPo92BYxjXiyYEVrUQh2aLyhxBCwww+heortUFxEJYcRzosstTEBYQ==
dependencies:
ms "^2.1.3"

debug@^4.1.1, debug@^4.4.0, debug@^4.4.1:
debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.4.0, debug@^4.4.1:
version "4.4.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.3.tgz#c6ae432d9bd9662582fce08709b038c58e9e3d6a"
integrity sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==
Expand Down Expand Up @@ -5617,18 +5619,18 @@ eslint-visitor-keys@^4.2.0, eslint-visitor-keys@^4.2.1:
integrity sha512-Uhdk5sfqcee/9H/rCOJikYz67o0a2Tw2hGRPOG2Y1R2dg7brRe1uG0yaNQDHu+TO/uQPF/5eCapvYSmHUjt7JQ==

eslint@^9.30.1:
version "9.32.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-9.32.0.tgz#4ea28df4a8dbc454e1251e0f3aed4bcf4ce50a47"
integrity sha512-LSehfdpgMeWcTZkWZVIJl+tkZ2nuSkyyB9C27MZqFWXuph7DvaowgcTvKqxvpLW1JZIk8PN7hFY3Rj9LQ7m7lg==
version "9.36.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-9.36.0.tgz#9cc5cbbfb9c01070425d9bfed81b4e79a1c09088"
integrity sha512-hB4FIzXovouYzwzECDcUkJ4OcfOEkXTv2zRY6B9bkwjx/cprAq0uvm1nl7zvQ0/TsUk0zQiN4uPfJpB9m+rPMQ==
dependencies:
"@eslint-community/eslint-utils" "^4.2.0"
"@eslint-community/eslint-utils" "^4.8.0"
"@eslint-community/regexpp" "^4.12.1"
"@eslint/config-array" "^0.21.0"
"@eslint/config-helpers" "^0.3.0"
"@eslint/core" "^0.15.0"
"@eslint/config-helpers" "^0.3.1"
"@eslint/core" "^0.15.2"
"@eslint/eslintrc" "^3.3.1"
"@eslint/js" "9.32.0"
"@eslint/plugin-kit" "^0.3.4"
"@eslint/js" "9.36.0"
"@eslint/plugin-kit" "^0.3.5"
"@humanfs/node" "^0.16.6"
"@humanwhocodes/module-importer" "^1.0.1"
"@humanwhocodes/retry" "^0.4.2"
Expand Down