structure user and izitoast and add medicalcenter

This commit is contained in:
saeid_01
2019-04-20 01:32:52 +04:30
parent a4eb09fb67
commit 5831a29686
42 changed files with 7424 additions and 4948 deletions
+80 -54
View File
@@ -1,13 +1,12 @@
import ApiService from './api.service'
import { TokenService } from './storage.service'
import { ApiService } from "../global/api.service";
import { TokenService } from "./storage.service";
class AuthenticationError extends Error {
constructor(errorCode, message) {
super(message)
this.name = this.constructor.name
this.message = message
this.errorCode = errorCode
super(message);
this.name = this.constructor.name;
this.message = message;
this.errorCode = errorCode;
}
}
@@ -17,34 +16,37 @@ const UserService = {
*
* @returns access_token
* @throws AuthenticationError
**/
**/
login: async function(cell_number, password) {
const requestData = {
method: 'post',
method: "post",
url: "/login",
data: {
cell_number,
password,
grant_type: 'password',
grant_type: "password"
},
auth: {
username: process.env.MIX_APP_CLIENT_ID,
password: process.env.MIX_APP_CLIENT_SECRET
}
}
};
try {
const response = await ApiService.customRequest(requestData);
TokenService.saveToken(response.data.access_token);
TokenService.saveRefreshToken(response.data.refresh_token);
ApiService.setHeader();
ApiService.setAuthHeader();
// NOTE: We haven't covered this yet in our ApiService
// but don't worry about this just yet - I'll come back to it later
ApiService.mount401Interceptor();
return response.data.access_token;
} catch (error) {
throw new AuthenticationError(error.response.status, error.response.data.detail);
throw new AuthenticationError(
error.response.status,
error.response.data.detail
);
}
},
/**
@@ -52,27 +54,30 @@ const UserService = {
*
* @returns access_token
* @throws AuthenticationError
**/
**/
forgotPassword: async function(cell_number) {
const requestData = {
method: 'post',
method: "post",
url: "forgotPassword",
data: {
cell_number,
cell_number
},
auth: {
username: process.env.MIX_APP_CLIENT_ID,
password: process.env.MIX_APP_CLIENT_SECRET
}
}
};
try {
const response = await ApiService.customRequest(requestData);
ApiService.setHeader();
ApiService.setAuthHeader();
return response.data;
} catch (error) {
throw new AuthenticationError(error.response.status, error.response.data.detail);
throw new AuthenticationError(
error.response.status,
error.response.data.detail
);
}
},
/**
@@ -80,97 +85,118 @@ const UserService = {
*
* @returns access_token
* @throws AuthenticationError
**/
**/
verifyCode: async function(verifyCode) {
const requestData = {
method: 'post',
method: "post",
url: "forgotPassword",
data: {
verifyCode,
verifyCode
},
auth: {
username: process.env.MIX_APP_CLIENT_ID,
password: process.env.MIX_APP_CLIENT_SECRET
}
}
};
try {
const response = await ApiService.customRequest(requestData);
ApiService.setHeader();
ApiService.setAuthHeader();
return response.data;
} catch (error) {
throw new AuthenticationError(error.response.status, error.response.data.detail);
throw new AuthenticationError(
error.response.status,
error.response.data.detail
);
}
},
/**
* Refresh the access token.
**/
**/
refreshToken: async function() {
const refreshToken = TokenService.getRefreshToken()
const refreshToken = TokenService.getRefreshToken();
const requestData = {
method: 'post',
method: "post",
url: "login",
data: {
grant_type: 'refresh_token',
grant_type: "refresh_token",
refresh_token: refreshToken
},
auth: {
username: process.env.MIX_APP_CLIENT_ID,
password: process.env.MIX_APP_CLIENT_SECRET
}
}
};
try {
const response = await ApiService.customRequest(requestData)
const response = await ApiService.customRequest(requestData);
TokenService.saveToken(response.data.access_token)
TokenService.saveRefreshToken(response.data.refresh_token)
TokenService.saveToken(response.data.access_token);
TokenService.saveRefreshToken(response.data.refresh_token);
// Update the header in ApiService
ApiService.setHeader()
ApiService.setAuthHeader();
return response.data.access_token
return response.data.access_token;
} catch (error) {
throw new AuthenticationError(error.response.status, error.response.data.detail)
throw new AuthenticationError(
error.response.status,
error.response.data.detail
);
}
},
/**
* Logout the current user by removing the token from storage.
*
* Will also remove `Authorization Bearer <token>` header from future requests.
**/
**/
logout: async function() {
const requestData = {
method: 'post',
url: "logout",
auth: {
username: process.env.MIX_APP_CLIENT_ID,
password: process.env.MIX_APP_CLIENT_SECRET
}
}
method: "post",
url: "logout"
};
try {
await ApiService.customRequest(requestData);
const response = await ApiService.customRequest(requestData);
// Remove the token and remove Authorization header from Api Service as well
TokenService.removeToken()
TokenService.removeRefreshToken()
ApiService.removeHeader()
TokenService.removeToken();
TokenService.removeRefreshToken();
TokenService.removeInfo();
ApiService.removeAuthHeader();
// NOTE: Again, we'll cover the 401 Interceptor a bit later.
ApiService.unmount401Interceptor()
ApiService.unmount401Interceptor();
return response.data;
} catch (error) {
throw new AuthenticationError(error.response.status, error.response.data.detail);
throw new AuthenticationError(
error.response.status,
error.response.data.detail
);
}
},
/**
* Get Information the current user ;
*
* Will also Register in Store;
**/
getInfo: async function() {
const requestData = {
method: "get",
url: "getInfo"
};
const response = await ApiService.customRequest(requestData);
TokenService.saveInfo(response.data);
return response.data;
}
}
};
export default UserService
export default UserService;
export { UserService, AuthenticationError }
export { UserService, AuthenticationError };