mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-12 10:21:07 +03:30
177 lines
5.2 KiB
JavaScript
Vendored
177 lines
5.2 KiB
JavaScript
Vendored
import ApiService from './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
|
|
}
|
|
}
|
|
|
|
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: "/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.setHeader();
|
|
|
|
// 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);
|
|
}
|
|
},
|
|
/**
|
|
* Login the user and store the access token to TokenService.
|
|
*
|
|
* @returns access_token
|
|
* @throws AuthenticationError
|
|
**/
|
|
forgotPassword: async function(cell_number) {
|
|
const requestData = {
|
|
method: 'post',
|
|
url: "forgotPassword",
|
|
data: {
|
|
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();
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
throw new AuthenticationError(error.response.status, error.response.data.detail);
|
|
}
|
|
},
|
|
/**
|
|
* Login the user and store the access token to TokenService.
|
|
*
|
|
* @returns access_token
|
|
* @throws AuthenticationError
|
|
**/
|
|
verifyCode: async function(verifyCode) {
|
|
const requestData = {
|
|
method: 'post',
|
|
url: "forgotPassword",
|
|
data: {
|
|
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();
|
|
|
|
return response.data;
|
|
} catch (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: "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.setHeader()
|
|
|
|
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() {
|
|
const requestData = {
|
|
method: 'post',
|
|
url: "logout",
|
|
auth: {
|
|
username: process.env.MIX_APP_CLIENT_ID,
|
|
password: process.env.MIX_APP_CLIENT_SECRET
|
|
}
|
|
}
|
|
|
|
try {
|
|
await ApiService.customRequest(requestData);
|
|
// Remove the token and remove Authorization header from Api Service as well
|
|
TokenService.removeToken()
|
|
TokenService.removeRefreshToken()
|
|
ApiService.removeHeader()
|
|
|
|
// NOTE: Again, we'll cover the 401 Interceptor a bit later.
|
|
ApiService.unmount401Interceptor()
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
throw new AuthenticationError(error.response.status, error.response.data.detail);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
export default UserService
|
|
|
|
export { UserService, AuthenticationError }
|