feat : jwt upload

This commit is contained in:
leejunghyeok 2024-05-22 15:50:15 +09:00
parent 5f34cc0cff
commit 3e10fc726f
3 changed files with 34 additions and 14 deletions

View File

@ -12,10 +12,12 @@ async function findByToken(userid,token, success, fail) {
await local.get(`/member/info/${userid}`).then(success).catch(fail); await local.get(`/member/info/${userid}`).then(success).catch(fail);
} }
async function tokenRegeneration(user, success, fail) { async function tokenRegeneration(userid,token, success, fail) {
// local.defaults.headers["refreshToken"] = sessionStorage.getItem("refreshToken"); //axios header에 refresh-token 셋팅 // local.defaults.headers["refreshToken"] = sessionStorage.getItem("refreshToken"); //axios header에 refresh-token 셋팅
local.defaults.headers["refreshToken"] = localStorage.getItem("refreshToken"); local.defaults.headers["refreshToken"] = token
await local.post(`/member/refresh`, user).then(success).catch(fail); console.log(userid)
console.log(token)
await local.post(`/member/refresh/${userid}`).then(success).catch(fail);
} }
async function logout(userid, success, fail) { async function logout(userid, success, fail) {

View File

@ -7,17 +7,24 @@ import { useMemberStore } from '@/stores/memberStore';
const onlyAuthUser = async (to, from, next) => { const onlyAuthUser = async (to, from, next) => {
const memberStore = useMemberStore(); const memberStore = useMemberStore();
const { userInfo, isValidToken } = storeToRefs(memberStore); const { userInfo, isLogin } = storeToRefs(memberStore);
const { getUserInfo } = memberStore; const { isValidToken } = memberStore;
let token = sessionStorage.getItem('accessToken'); let token = localStorage.getItem("accessToken");
console.log(userInfo)
if (userInfo.value != null && token) { console.log(isLogin)
await getUserInfo(token); // if (userInfo.value != null && token) {
console.log(isLogin.value)
console.log(token)
if (isLogin.value != null && token) {
console.log("이프문들어옴")
await isValidToken(token);
} }
if (!isValidToken.value || userInfo.value === null) { if (isLogin.value === null) {
next({ name: 'user-login' }); console.log("이프문2들어옴")
next({ name: "user-login" });
} else { } else {
console.log("else문 들어옴")
next(); next();
} }
}; };
@ -90,6 +97,10 @@ const router = createRouter({
}, },
], ],
}, },
{
path: '/test',
component: () => import('@/views/TestView.vue'),
},
], ],
}); });

View File

@ -30,9 +30,11 @@ export const useMemberStore = defineStore("memberStore", () => {
} }
const isValidToken = async (token) => { const isValidToken = async (token) => {
const {id} = jwtDecode(token); const { userId } = jwtDecode(token);
findByToken(id,token, () => { }, (error) => { console.log(userId,token)
findByToken(userId,token, () => { }, (error) => {
console.log(error); console.log(error);
console.log("tokenRegenerate")
tokenRegenerate(); tokenRegenerate();
}) })
@ -40,13 +42,18 @@ export const useMemberStore = defineStore("memberStore", () => {
const tokenRegenerate = async () => { const tokenRegenerate = async () => {
const { userId } = jwtDecode(accessToken.value)
const refreshToken = localStorage.getItem('refreshToken'); const refreshToken = localStorage.getItem('refreshToken');
console.log(userId)
console.log(refreshToken)
await tokenRegeneration( await tokenRegeneration(
refreshToken, userId,refreshToken,
(response) => { (response) => {
if (response.status === httpStatusCode.CREATE) { if (response.status === httpStatusCode.CREATE) {
const newAccessToken = response.data["access-token"] const newAccessToken = response.data["access-token"]
const newRefreshToken = response.data["refresh-token"]
localStorage.setItem("accessToken", newAccessToken) localStorage.setItem("accessToken", newAccessToken)
localStorage.setItem("refreshToken", newRefreshToken)
accessToken.value = newAccessToken; accessToken.value = newAccessToken;
} }
}, },