Skip to content

Commit

Permalink
Merge pull request #81 from dnd-side-project/feature/80-version
Browse files Browse the repository at this point in the history
버저닝 추가
  • Loading branch information
froggy1014 authored Oct 20, 2024
2 parents bb4b103 + 37e049d commit 4cfaed1
Show file tree
Hide file tree
Showing 22 changed files with 1,236 additions and 173 deletions.
39 changes: 25 additions & 14 deletions auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { User } from "next-auth";
import type { JWT } from "next-auth/jwt";
import { decode, type JWT } from "next-auth/jwt";
import NextAuth from "next-auth";
import KakaoProvider from "next-auth/providers/kakao";

Expand All @@ -10,6 +10,10 @@ import { env } from "@/env";
import { decodeToken } from "@/lib/jwt";
import type { UserMeResponse } from "@/apis/user/me/meType";

const serviceReadyRoute = ["/chat", "/map", "/calander"];
const matchersForAuth = ["/mypage"];
const matchersForSignIn = ["/auth/sign-in"];

export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
providers: [
KakaoProvider({
Expand All @@ -24,12 +28,16 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
error: "/auth/sign-in",
},
callbacks: {
async signIn() {
authorized: async ({ request, auth }) => {
if (matchersForAuth.some((v) => v == request.nextUrl.pathname)) {
return !!auth;
}

return true;
},

redirect: async ({ url, baseUrl }) => {
if (url.startsWith("/")) return `${baseUrl}${url}`;

if (url) {
const { search, origin } = new URL(url);
const callbackUrl = new URLSearchParams(search).get("callbackUrl");
Expand All @@ -41,6 +49,7 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
}
return baseUrl;
},

async jwt({ token, session, user, trigger, account }) {
if (trigger === "update") {
token.user = {
Expand All @@ -56,8 +65,7 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
accessToken: account.access_token as string,
};

const { accessToken, refreshToken, isProfileRegistered } =
await postOauthLogin(body);
const { accessToken, refreshToken } = await postOauthLogin(body);

const userResponse = await getMe({
headers: {
Expand All @@ -71,6 +79,7 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
refreshToken,
accessToken,
};

return token;
}
}
Expand All @@ -79,30 +88,32 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
return null;
}

const decodedJWT = decodeToken(token.accessToken);
if (!!token.accessToken) {
const decodedJWT = decodeToken(token.accessToken);

if (decodedJWT) {
token.exp = decodedJWT.exp;
}
}

if (
!!token.refreshToken &&
!!decodedJWT?.exp &&
decodedJWT?.exp * 1000 < Date.now()
token?.exp &&
token?.exp * 1000 < Date.now()
) {
console.log("토큰 재발급");
const { accessToken, refreshToken } = await getRefreshToken(
token.refreshToken,
);

const decodedJWT = decodeToken(accessToken);

token.accessToken = accessToken;
token.refreshToken = refreshToken;
token.exp = decodedJWT?.exp;
}

return token;
},

async session({ session, token }) {
if (token?.accessToken) {
const decodedJWT = decodeToken(token.accessToken ?? "");
session.user = {
...session.user,
userId: token.user.userId as number,
Expand All @@ -115,7 +126,7 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
};
session.accessToken = token.accessToken;
session.refreshToken = token.refreshToken;
session.exp = decodedJWT?.exp;
session.exp = token.exp;
session.iat = token.iat;
session.sub = token.sub;
}
Expand Down
6 changes: 0 additions & 6 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ const nextConfig = {
},
],
},
logging: {
fetches: {
fullUrl: true,
},
},
reactStrictMode: true,

experimental: { instrumentationHook: true },
webpack(config) {
// SVG 가져오기를 처리하는 기존 규칙을 가져옵니다.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"formatting": "prettier --write '**/*.{ts,tsx}'",
"lint": "eslint --fix .",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
"build-storybook": "storybook build",
"release": "release-it"
},
"dependencies": {
"@hookform/resolvers": "3.9.0",
Expand Down Expand Up @@ -96,6 +97,7 @@
"msw": "^2.3.5",
"postcss": "^8",
"prettier": "^3.3.2",
"release-it": "^17.10.0",
"storybook": "^8.2.6",
"tailwindcss": "^3.4.6",
"typescript": "^5"
Expand Down
Loading

0 comments on commit 4cfaed1

Please sign in to comment.