password.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { createHash } from 'crypto';
  2. import JSEncrypt from 'jsencrypt';
  3. import { LocalScheme } from '~auth/runtime';
  4. // const { createHash } = await import('node:crypto');
  5. let publicKey;
  6. const encrypt = new JSEncrypt();
  7. export default class CustomScheme extends LocalScheme {
  8. async login(data, { reset = true } = {}) {
  9. const endpoint = { data: Object.assign({}, data) };
  10. // endpoint.data = Object.assign({}, endpoint.data);
  11. // endpoint.data = endpoint.data;
  12. if (!this.options.endpoints.login) {
  13. return;
  14. }
  15. if (reset) {
  16. this.$auth.reset({ resetInterceptor: false });
  17. }
  18. if (this.options.clientId) {
  19. endpoint.data.client_id = this.options.clientId;
  20. }
  21. if (this.options.grantType) {
  22. endpoint.data.grant_type = this.options.grantType;
  23. }
  24. if (this.options.scope) {
  25. endpoint.data.scope = this.options.scope;
  26. }
  27. if (!publicKey) {
  28. publicKey = (
  29. await this.$auth.ctx.$axios.$post('/user/v1/client/login/getPbKey')
  30. ).data.publicKey;
  31. encrypt.setPublicKey(publicKey);
  32. }
  33. // console.log(
  34. // '🚀 ~ file: password.js ~ line 37 ~ CustomScheme ~ login ~ publicKey',
  35. // publicKey,
  36. // );
  37. const md5 = createHash('md5');
  38. endpoint.data.password = encrypt.encrypt(
  39. md5.update(endpoint.data.password, 'utf-8').digest('hex'),
  40. );
  41. // privateEncrypt(publicKey, endpoint.data.md5).toString('base64');
  42. // endpoint.data.uuid = '132514568481654';
  43. endpoint.data.client = 7;
  44. const response = await this.$auth.request(
  45. endpoint,
  46. this.options.endpoints.login,
  47. );
  48. this.updateTokens(response);
  49. if (!this.requestHandler.interceptor) {
  50. this.initializeRequestInterceptor();
  51. }
  52. if (this.options.user.autoFetch) {
  53. await this.fetchUser();
  54. }
  55. return response;
  56. }
  57. }