Skip to content

Commit

Permalink
升级依赖,添加jwtKey环境变量
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry committed Jun 25, 2024
1 parent 775e538 commit bb78c21
Show file tree
Hide file tree
Showing 8 changed files with 2,480 additions and 2,528 deletions.
1 change: 0 additions & 1 deletion lib/constant.ts

This file was deleted.

3 changes: 3 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default defineNuxtConfig({
classSuffix: "",
storageKey:'vueuse-color-scheme'
},
runtimeConfig:{
jwtKey:"",
},
shadcn: {
/**
* Prefix for all the imported component
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,45 @@
},
"dependencies": {
"@alicloud/pop-core": "^1.7.13",
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/s3-request-presigner": "^3.550.0",
"@aws-sdk/client-s3": "^3.600.0",
"@aws-sdk/s3-request-presigner": "^3.600.0",
"@fancyapps/ui": "^5.0.36",
"@prisma/client": "^5.14.0",
"@prisma/client": "^5.15.1",
"@radix-icons/vue": "^1.0.0",
"@vueuse/core": "^10.9.0",
"@vueuse/core": "^10.11.0",
"bcrypt": "^5.1.1",
"cheerio": "^1.0.0-rc.12",
"cheerio": "1.0.0-rc.12",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"dayjs": "^1.11.11",
"js-base64": "^3.7.7",
"jsonp": "^0.2.1",
"jsonwebtoken": "^9.0.2",
"lucide-vue-next": "^0.383.0",
"nodemailer": "^6.9.13",
"nuxt": "^3.11.1",
"radix-vue": "^1.8.3",
"nodemailer": "^6.9.14",
"nuxt": "^3.12.2",
"radix-vue": "^1.8.5",
"reflect-metadata": "^0.2.2",
"short-uuid": "^5.2.0",
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7",
"vue": "^3.4.27",
"vue-router": "^4.3.2",
"vue-sonner": "^1.1.2",
"vue": "^3.4.30",
"vue-router": "^4.4.0",
"vue-sonner": "^1.1.3",
"vuedraggable": "^2.24.3"
},
"devDependencies": {
"@nuxtjs/color-mode": "^3.4.1",
"@nuxtjs/color-mode": "^3.4.2",
"@nuxtjs/tailwindcss": "^6.12.0",
"@types/bcrypt": "^5.0.2",
"@types/jsonp": "^0.2.3",
"@types/jsonwebtoken": "^9.0.6",
"@types/nodemailer": "^6.4.15",
"all-contributors-cli": "^6.26.1",
"prisma": "^5.14.0",
"sass": "^1.77.2",
"prisma": "^5.15.1",
"sass": "^1.77.6",
"shadcn-nuxt": "^0.10.4",
"typescript": "^5.4.5"
"typescript": "^5.5.2"
},
"optionalDependencies": {
"@rollup/rollup-linux-x64-gnu": "4.9.5"
Expand Down
4,954 changes: 2,450 additions & 2,504 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions server/api/memo/list.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import prisma from "~/lib/db";
import fs from "fs/promises";
import type { SysConfig } from "~/lib/types";
import jwt from "jsonwebtoken";
import { jwtKey } from "~/lib/constant";
import { JwtPayload } from "../user/login.post";

type ListMemoReq = {
Expand All @@ -13,10 +12,12 @@ type ListMemoReq = {

export default defineEventHandler(async (event) => {
const token = getCookie(event, "token");
const runtimeConfig = useRuntimeConfig()

let userId = 0;
if (token) {
try {
const user = jwt.verify(token, jwtKey) as JwtPayload;
const user = jwt.verify(token, runtimeConfig.jwtKey) as JwtPayload;
userId = user.userId;
} catch {}
}
Expand Down
5 changes: 3 additions & 2 deletions server/api/user/login.post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { jwtKey } from "~/lib/constant";
import prisma from "~/lib/db";

type loginReq = {
Expand All @@ -15,6 +14,8 @@ export type JwtPayload = {
};

export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig()

const { username, password } = (await readBody(event)) as loginReq;
let token = "";
if (!username || !password) {
Expand Down Expand Up @@ -45,7 +46,7 @@ export default defineEventHandler(async (event) => {
username: user.username,
userId: user.id,
},
jwtKey
runtimeConfig.jwtKey
);
setCookie(event, "token", token, {
expires: new Date(Date.now() + 60 * 60 * 24 * 1000),
Expand Down
5 changes: 3 additions & 2 deletions server/api/user/validateToken.post.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import jwt from "jsonwebtoken";
import { jwtKey } from "~/lib/constant";

export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig()

const token = getCookie(event, "token");
if (token) {
try {
jwt.verify(token, jwtKey);
jwt.verify(token, runtimeConfig.jwtKey);
} catch (e) {
return {
success: false,
Expand Down
5 changes: 3 additions & 2 deletions server/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { jwtKey } from "~/lib/constant";
import jwt from "jsonwebtoken";
import { JwtPayload } from "../api/user/login.post";

Expand All @@ -23,6 +22,8 @@ const needLoginUrl = [
export default defineEventHandler(async (event) => {
const token = getCookie(event, "token");
const url = getRequestURL(event);
const runtimeConfig = useRuntimeConfig()


if (token && url.pathname === "/login") {
await sendRedirect(event, "/", 302);
Expand All @@ -31,7 +32,7 @@ export default defineEventHandler(async (event) => {

if (url.pathname === "/api/comment/save" && token) {
try {
const result = jwt.verify(token, jwtKey);
const result = jwt.verify(token, runtimeConfig.jwtKey);
const payload = result as JwtPayload;
event.context.userId = payload.userId;
} catch (error) {}
Expand Down

0 comments on commit bb78c21

Please sign in to comment.