Skip to content

Commit 32cc1e2

Browse files
committed
refact(auth): login password with encode
1 parent 5b46da4 commit 32cc1e2

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

src/api/auth.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import { encodePassword } from '@/utils/security';
23

34
export interface LoginRequest {
45
userName?: string;
@@ -22,10 +23,14 @@ export interface ExchangeReply {
2223
refreshToken: string;
2324
}
2425

25-
export function login(request: LoginRequest) {
26+
export async function login(request: LoginRequest) {
27+
const requestLogin = {
28+
...request,
29+
password: await encodePassword(request.password),
30+
};
2631
return axios.post<LoginReply>(
2732
'/api/v1/admin/auth/access/actions/basic-login',
28-
request,
33+
requestLogin,
2934
);
3035
}
3136

src/components/navbar/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:style="{ margin: 0, fontSize: '18px' }"
88
:heading="5"
99
>
10-
PowerX Dashboard
10+
PowerX Dashboard - {{ PowerXVersion }}
1111
</a-typography-title>
1212
<icon-menu-fold
1313
v-if="!topMenu && appStore.device === 'mobile'"

src/store/modules/app/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import { defineStore } from 'pinia';
22
import { Notification } from '@arco-design/web-vue';
3-
import type { NotificationReturn } from '@arco-design/web-vue/es/notification/interface';
3+
44
import type { RouteRecordNormalized } from 'vue-router';
55
import defaultSettings from '@/config/settings.json';
66
import { getMenuList } from '@/api/userinfo';
77
import { AppState } from './types';
88

9-
const sessionPrefix = 'app';
10-
11-
function formatSessionKey(key: string) {
12-
return `${sessionPrefix}:${key}`;
13-
}
14-
159
const useAppStore = defineStore('app', {
1610
state: (): AppState => ({ ...defaultSettings }),
1711

@@ -54,22 +48,21 @@ const useAppStore = defineStore('app', {
5448
},
5549
// 获取服务器菜单配置
5650
async fetchServerMenuConfig() {
57-
let notifyInstance: NotificationReturn | null = null;
5851
try {
59-
notifyInstance = Notification.info({
52+
Notification.info({
6053
id: 'menuNotice',
6154
content: '加载中',
6255
closable: true,
6356
});
6457
const { data } = await getMenuList();
6558
this.serverMenu = data;
66-
notifyInstance = Notification.success({
59+
Notification.success({
6760
id: 'menuNotice',
6861
content: '成功',
6962
closable: true,
7063
});
7164
} catch (error) {
72-
notifyInstance = Notification.error({
65+
Notification.error({
7366
id: 'menuNotice',
7467
content: '错误',
7568
closable: true,

src/utils/security.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const encodePassword = async (data: string): Promise<string> => {
2+
const encoder = new TextEncoder();
3+
const dataBuffer = encoder.encode(data);
4+
const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
5+
const hashArray = Array.from(new Uint8Array(hashBuffer));
6+
const hashHex = hashArray
7+
.map((b) => b.toString(16).padStart(2, '0'))
8+
.join('');
9+
return hashHex;
10+
};
11+
12+
// 验证密码函数
13+
export const verifyPassword = async (
14+
plainPassword: string,
15+
hashedPassword: string,
16+
): Promise<boolean> => {
17+
const encryptedPassword = await encodePassword(plainPassword);
18+
return encryptedPassword === hashedPassword;
19+
};

0 commit comments

Comments
 (0)