|
@@ -4,7 +4,22 @@ 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) };
|
|
@@ -53,6 +68,8 @@ export default class CustomScheme extends LocalScheme {
|
|
|
this.options.endpoints.login,
|
|
|
);
|
|
|
this.updateTokens(response);
|
|
|
+ localStorage.setItem('auth.username', response.data.data.username);
|
|
|
+
|
|
|
if (!this.requestHandler.interceptor) {
|
|
|
this.initializeRequestInterceptor();
|
|
|
}
|
|
@@ -61,4 +78,38 @@ export default class CustomScheme extends LocalScheme {
|
|
|
}
|
|
|
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);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|