mirror of
https://git.meshkee.com/cssd/cssd-front.git
synced 2026-10-12 13:13:15 +03:30
fix
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import qs from 'qs';
|
||||
|
||||
const TOKEN_KEY = 'access_token'
|
||||
const REFRESH_TOKEN_KEY = 'refresh_token'
|
||||
const INFO = 'info'
|
||||
/**
|
||||
* Manage the how Access Tokens are being stored and retreived from storage.
|
||||
*
|
||||
* Current implementation stores to localStorage. Local Storage should always be
|
||||
* accessed through this instace.
|
||||
**/
|
||||
const TokenService = {
|
||||
getToken() {
|
||||
return localStorage.getItem(TOKEN_KEY)
|
||||
},
|
||||
|
||||
saveToken(accessToken = null) {
|
||||
localStorage.setItem(TOKEN_KEY, accessToken)
|
||||
},
|
||||
|
||||
removeToken() {
|
||||
localStorage.removeItem(TOKEN_KEY)
|
||||
},
|
||||
|
||||
getRefreshToken() {
|
||||
return localStorage.getItem(REFRESH_TOKEN_KEY)
|
||||
},
|
||||
|
||||
saveRefreshToken(refreshToken = null) {
|
||||
localStorage.setItem(REFRESH_TOKEN_KEY, refreshToken)
|
||||
},
|
||||
|
||||
removeRefreshToken() {
|
||||
localStorage.removeItem(REFRESH_TOKEN_KEY)
|
||||
},
|
||||
|
||||
getInfo() {
|
||||
return qs.parse(localStorage.getItem(INFO))
|
||||
},
|
||||
|
||||
saveInfo(info = null) {
|
||||
info = qs.stringify(info);
|
||||
localStorage.setItem(INFO, info)
|
||||
},
|
||||
|
||||
removeInfo() {
|
||||
localStorage.removeItem(INFO)
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
export { TokenService }
|
||||
@@ -0,0 +1,127 @@
|
||||
import { ApiService } from "../global/api.service";
|
||||
import { TokenService } from "./storage.service";
|
||||
import { urls } from "@/config/urls";
|
||||
import toast from "../global/toast.service";
|
||||
|
||||
class AuthenticationError extends Error {
|
||||
constructor(errorCode, message) {
|
||||
super(message);
|
||||
this.name = this.constructor.name;
|
||||
this.message = message;
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
}
|
||||
|
||||
const UserService = {
|
||||
/**
|
||||
* Login the user and store the access token to TokenService.
|
||||
*
|
||||
* @returns access_token
|
||||
* @throws AuthenticationError
|
||||
**/
|
||||
login: async function(cell_number, password) {
|
||||
const requestData = {
|
||||
method: "post",
|
||||
url: urls.login,
|
||||
data: {
|
||||
cell_number,
|
||||
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.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;
|
||||
} catch (error) {
|
||||
toast.error('Username Or Password Is Incorrect', 'Error');
|
||||
throw new AuthenticationError(
|
||||
error.response.status,
|
||||
error.response.data.detail
|
||||
);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Refresh the access token.
|
||||
**/
|
||||
refreshToken: async function() {
|
||||
const refreshToken = TokenService.getRefreshToken();
|
||||
|
||||
const requestData = {
|
||||
method: "post",
|
||||
url: urls.login,
|
||||
data: {
|
||||
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);
|
||||
|
||||
TokenService.saveToken(response.data.access_token);
|
||||
TokenService.saveRefreshToken(response.data.refresh_token);
|
||||
// Update the header in ApiService
|
||||
ApiService.setAuthHeader();
|
||||
|
||||
return response.data.access_token;
|
||||
} catch (error) {
|
||||
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() {
|
||||
try {
|
||||
await ApiService.post(urls.logout);
|
||||
// Remove the token and remove Authorization header from Api Service as well
|
||||
TokenService.removeToken();
|
||||
TokenService.removeRefreshToken();
|
||||
TokenService.removeInfo();
|
||||
ApiService.removeAuthHeader();
|
||||
|
||||
// NOTE: Again, we'll cover the 401 Interceptor a bit later.
|
||||
ApiService.unmount401Interceptor();
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return true
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Get Information the current user ;
|
||||
*
|
||||
* Will also Register in Store;
|
||||
**/
|
||||
getInfo: async function() {
|
||||
const response = await ApiService.get(urls.getInfo);
|
||||
TokenService.saveInfo(response.data.data);
|
||||
return response.data.data;
|
||||
}
|
||||
};
|
||||
|
||||
export default UserService;
|
||||
|
||||
export { UserService, AuthenticationError };
|
||||
Reference in New Issue
Block a user