Skip to content
Merged
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
20 changes: 14 additions & 6 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@ export const postLogin = async (nickName: string, password: string) => {
path: '/',
sameSite: 'strict'
})
Cookies.set('refreshToken', response.data.refreshToken, {
path: '/',
sameSite: 'strict'
})
if (response.data.refreshToken) {
Cookies.set('refreshToken', response.data.refreshToken, {
path: '/',
sameSite: 'strict'
})
}
return response.data
}

export const patchPassword = async (password: string) => {
const request = { password }
const response = await axiosInstance.patch('/api/members/password', request)
return response.data
const refreshToken = Cookies.get('refreshToken')
if (refreshToken) {
const response = await axiosInstance.patch('/api/members/password', request)
return response.data
} else {
const response = await axiosInstance.patch('/api/members/initial-password', request)
return response.data
}
}

export const deleteLogout = async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const COLOR_LIST = [
]

export const PERMITTED_URL = {
UNKNOWN: ['/login', '/pw-change-email'],
UNKNOWN: ['/login', '/pw-change-email', '/pw-change'],
ROLE_USER: ['/my-request', '/task-request', '/edit-information', '/pw-change'],
ROLE_MANAGER: [
'/my-task',
Expand Down
18 changes: 15 additions & 3 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ router.beforeEach(async (to, from, next) => {
ROLE_ADMIN: '/member-management'
}

if ((info.role && PERMITTED_URL.UNKNOWN.includes(to.path)) || (info.role && to.path === '/')) {
if (
(info.role && PERMITTED_URL.UNKNOWN.includes(to.path) && to.path !== '/pw-change') ||
(info.role && to.path === '/')
) {
return next(redirectMap[info.role])
}

Expand All @@ -167,11 +170,20 @@ router.beforeEach(async (to, from, next) => {
ROLE_ADMIN: PERMITTED_URL.ROLE_ADMIN
}

if (from.path === redirectMap[info.role] && !permittedUrlMap[info.role].includes(to.path)) {
const isPathPermitted = (path: string, permittedPaths: string[]) => {
return permittedPaths.some(permittedPath => {
return path.startsWith(permittedPath)
})
}

if (
from.path === redirectMap[info.role] &&
!isPathPermitted(to.path, permittedUrlMap[info.role])
) {
return false
}

if (!permittedUrlMap[info.role].includes(to.path)) {
if (!isPathPermitted(to.path, permittedUrlMap[info.role])) {
if (to.path === redirectMap[info.role]) {
return next()
}
Expand Down
3 changes: 2 additions & 1 deletion src/stores/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const useMemberStore = defineStore('memberInfo', () => {

async function updateMemberInfoWithToken() {
const token = Cookies.get('accessToken')
if (!token) return
const refreshToken = Cookies.get('refreshToken')
if (!token || !refreshToken) return

const { data }: { data: User } = await axiosInstance.get('/api/members/info')
info.value = data
Expand Down
2 changes: 1 addition & 1 deletion src/views/LoginView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const handleLogin = async () => {

if (!Cookies.get('refreshToken')) {
router.push('/pw-change')
} else if (res && role && Cookies.get('refreshToken')) {
} else if (res) {
switch (role) {
case 'ROLE_ADMIN':
router.push('/member-management')
Expand Down
22 changes: 19 additions & 3 deletions src/views/PwChangeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
v-model="pw"
placeholder="비밀번호를 입력해주세요"
required
class="input-box" />
class="input-box"
autocomplete="current-password" />
<div class="flex flex-col gap-2">
<button
type="submit"
Expand All @@ -60,6 +61,7 @@
placeholder="새 비밀번호를 입력해주세요"
required
ref="passwordInput"
autocomplete="new-password"
:class="[
'block w-full px-4 py-4 border rounded focus:outline-none',
isInvalid ? 'border-red-1' : 'border-border-1'
Expand All @@ -78,6 +80,7 @@
ref="checkPwInput"
placeholder="새 비밀번호를 다시 입력해주세요"
required
autocomplete="new-password"
:class="[
'block w-full px-4 py-4 border rounded focus:outline-none',
isDifferent ? 'border-red-1' : 'border-border-1'
Expand Down Expand Up @@ -108,9 +111,10 @@
<script setup lang="ts">
import { patchPassword, postPasswordCheck } from '@/api/auth'
import TitleContainer from '@/components/common/TitleContainer.vue'
import { nextTick, ref } from 'vue'
import { nextTick, onMounted, ref } from 'vue'
import ModalView from '@/components/common/ModalView.vue'
import { useRouter } from 'vue-router'
import Cookies from 'js-cookie'

const isErrorVisible = ref(false)

Expand All @@ -134,6 +138,13 @@ const passwordInput = ref<HTMLInputElement | null>(null)
const isModalVisible = ref(false)
const router = useRouter()

onMounted(() => {
const refreshToken = Cookies.get('refreshToken')
if (!refreshToken) {
isConfirmed.value = true
}
})

const validatePassword = () => {
const regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*()_+{}\[\]:;<>,.?/~`-]).{8,20}$/
isInvalid.value = !regex.test(newPw.value)
Expand Down Expand Up @@ -165,7 +176,12 @@ const handleChange = async () => {

const closeModal = () => {
isModalVisible.value = !isModalVisible.value
router.replace('/edit-information')
if (Cookies.get('refreshToken')) {
router.replace('/edit-information')
} else {
Cookies.remove('accessToken')
router.replace('/login')
}
}
const closeError = () => {
isErrorVisible.value = !isErrorVisible.value
Expand Down