import { createHash } from 'crypto'; import JSEncrypt from 'jsencrypt'; import { LocalScheme } from '~auth/runtime'; // const { createHash } = await import('node:crypto'); let publicKey; const encrypt = new JSEncrypt(); function getProp(holder, propName) { if (!propName || !holder || typeof holder !== 'object') { return holder; } if (propName in holder) { return holder[propName]; } const propParts = Array.isArray(propName) ? propName : (propName + '').split('.'); let result = holder; while (propParts.length && result) { result = result[propParts.shift()]; } return result; } export default class CustomScheme extends LocalScheme { async login(data, { reset = true } = {}) { const endpoint = { data: Object.assign({}, data) }; // endpoint.data = Object.assign({}, endpoint.data); // endpoint.data = endpoint.data; if (!this.options.endpoints.login) { return; } if (reset) { this.$auth.reset({ resetInterceptor: false }); } if (this.options.clientId) { endpoint.data.client_id = this.options.clientId; } if (this.options.grantType) { endpoint.data.grant_type = this.options.grantType; } if (this.options.scope) { endpoint.data.scope = this.options.scope; } if (!publicKey) { publicKey = ( await this.$auth.ctx.$axios.$post('/user/v1/client/login/getPbKey') ).data.publicKey; encrypt.setPublicKey(publicKey); } // console.log( // '🚀 ~ file: password.js ~ line 37 ~ CustomScheme ~ login ~ publicKey', // publicKey, // ); const md5 = createHash('md5'); endpoint.data.password = encrypt.encrypt( md5.update(endpoint.data.password, 'utf-8').digest('hex'), ); // privateEncrypt(publicKey, endpoint.data.md5).toString('base64'); // endpoint.data.uuid = '132514568481654'; endpoint.data.client = 7; const response = await this.$auth.request( endpoint, this.options.endpoints.login, ); this.updateTokens(response); localStorage.setItem('auth.username', response.data.data.username); if (!this.requestHandler.interceptor) { this.initializeRequestInterceptor(); } if (this.options.user.autoFetch) { await this.fetchUser(); } return response; } fetchUser(endpoint) { console.log( '🚀 ~ file: password.js ~ line 83 ~ CustomScheme ~ fetchUser ~ endpoint', endpoint, ); if (!this.check().valid) { return Promise.resolve(); } if (!this.options.endpoints.user) { this.$auth.setUser({}); return Promise.resolve(); } return this.$auth .requestWith(this.name, endpoint, this.options.endpoints.user) .then((response) => { const userData = getProp(response.data, this.options.user.property); userData.username = localStorage.getItem('auth.username'); if (!userData) { const error = new Error( `User Data response does not contain field ${this.options.user.property}`, ); return Promise.reject(error); } this.$auth.setUser(userData); return response; }) .catch((error) => { this.$auth.callOnError(error, { method: 'fetchUser' }); return Promise.reject(error); }); } }