password.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. function getProp(holder, propName) {
  8. if (!propName || !holder || typeof holder !== 'object') {
  9. return holder;
  10. }
  11. if (propName in holder) {
  12. return holder[propName];
  13. }
  14. const propParts = Array.isArray(propName)
  15. ? propName
  16. : (propName + '').split('.');
  17. let result = holder;
  18. while (propParts.length && result) {
  19. result = result[propParts.shift()];
  20. }
  21. return result;
  22. }
  23. export default class CustomScheme extends LocalScheme {
  24. async login(data, { reset = true } = {}) {
  25. const endpoint = { data: Object.assign({}, data) };
  26. // endpoint.data = Object.assign({}, endpoint.data);
  27. // endpoint.data = endpoint.data;
  28. if (!this.options.endpoints.login) {
  29. return;
  30. }
  31. if (reset) {
  32. this.$auth.reset({ resetInterceptor: false });
  33. }
  34. if (this.options.clientId) {
  35. endpoint.data.client_id = this.options.clientId;
  36. }
  37. if (this.options.grantType) {
  38. endpoint.data.grant_type = this.options.grantType;
  39. }
  40. if (this.options.scope) {
  41. endpoint.data.scope = this.options.scope;
  42. }
  43. if (!publicKey) {
  44. publicKey = (
  45. await this.$auth.ctx.$axios.$post('/user/v1/client/login/getPbKey')
  46. ).data.publicKey;
  47. encrypt.setPublicKey(publicKey);
  48. }
  49. // console.log(
  50. // '🚀 ~ file: password.js ~ line 37 ~ CustomScheme ~ login ~ publicKey',
  51. // publicKey,
  52. // );
  53. const md5 = createHash('md5');
  54. endpoint.data.password = encrypt.encrypt(
  55. md5.update(endpoint.data.password, 'utf-8').digest('hex'),
  56. );
  57. // privateEncrypt(publicKey, endpoint.data.md5).toString('base64');
  58. // endpoint.data.uuid = '132514568481654';
  59. endpoint.data.client = 7;
  60. const response = await this.$auth.request(
  61. endpoint,
  62. this.options.endpoints.login,
  63. );
  64. this.updateTokens(response);
  65. localStorage.setItem('auth.username', response.data.data.username);
  66. if (!this.requestHandler.interceptor) {
  67. this.initializeRequestInterceptor();
  68. }
  69. if (this.options.user.autoFetch) {
  70. await this.fetchUser();
  71. }
  72. return response;
  73. }
  74. fetchUser(endpoint) {
  75. console.log(
  76. '🚀 ~ file: password.js ~ line 83 ~ CustomScheme ~ fetchUser ~ endpoint',
  77. endpoint,
  78. );
  79. if (!this.check().valid) {
  80. return Promise.resolve();
  81. }
  82. if (!this.options.endpoints.user) {
  83. this.$auth.setUser({});
  84. return Promise.resolve();
  85. }
  86. return this.$auth
  87. .requestWith(this.name, endpoint, this.options.endpoints.user)
  88. .then((response) => {
  89. const userData = getProp(response.data, this.options.user.property);
  90. userData.username = localStorage.getItem('auth.username');
  91. if (!userData) {
  92. const error = new Error(
  93. `User Data response does not contain field ${this.options.user.property}`,
  94. );
  95. return Promise.reject(error);
  96. }
  97. this.$auth.setUser(userData);
  98. return response;
  99. })
  100. .catch((error) => {
  101. this.$auth.callOnError(error, { method: 'fetchUser' });
  102. return Promise.reject(error);
  103. });
  104. }
  105. }