265 lines
6.0 KiB
JavaScript
265 lines
6.0 KiB
JavaScript
import { observable, action, computed} from 'mobx';
|
|
import * as fetch from 'isomorphic-fetch';
|
|
|
|
export class Authentication {
|
|
|
|
@observable isLoading = false;
|
|
@observable isRegistering = false;
|
|
@observable isLoggingIn = false;
|
|
@observable isInviting = false;
|
|
@observable removingUser = false;
|
|
|
|
@observable userWallet = {};
|
|
@observable userProfile = {
|
|
email : '',
|
|
role : '',
|
|
username : '',
|
|
status :'',
|
|
agent : {
|
|
name : '',
|
|
phone_number : '',
|
|
email : '',
|
|
address : '',
|
|
id: '',
|
|
created_at: '',
|
|
user_id:'',
|
|
id_tax_number : '',
|
|
id_type : '',
|
|
id_number : '',
|
|
id_photo : '',
|
|
passport_number: '',
|
|
passport_photo : '',
|
|
error_email : '',
|
|
},
|
|
// role_data: {
|
|
// airline_blacklisteds: [],
|
|
// airline_settings: []
|
|
// }
|
|
};
|
|
|
|
constructor(context) {
|
|
this.context = context;
|
|
this.http = context.http;
|
|
}
|
|
|
|
@action
|
|
login(data) {
|
|
// this.context.setToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkaWdpc3VpdGVzIiwiZXhwIjoxNDg1ODcyMTYzLCJzdWIiOiJjcmVkZW50aWFsIiwidXNlciI6Imhhc3RhLnJhZ2lsQGdtYWlsLmNvbSIsInJvbGVzIjpbIlRyYXZlbCBBZ2VudCJdfQ.u3MEelOz3FWyP78QLTvTh4n-ueH1G4R0GWcXrjXUPMY');
|
|
this.isLoggingIn = true;
|
|
|
|
return this.http.post("authentication/login", data)
|
|
.then(res => {
|
|
this.isLoggingIn = false;
|
|
this.context.setToken(res.token);
|
|
this.context.loadDataAfterLogin();
|
|
})
|
|
.catch(err => {
|
|
this.isLoggingIn = false;
|
|
throw err;
|
|
})
|
|
}
|
|
|
|
@action
|
|
forgetPassword(email) {
|
|
return this.http.post("authentication/forgot_password", email)
|
|
.then(res => {
|
|
return res;
|
|
})
|
|
.catch(err => {
|
|
throw err;
|
|
})
|
|
}
|
|
|
|
@action
|
|
logout() {
|
|
this.context.setToken("");
|
|
this.context.setUserData({});
|
|
// return this.http.post("authentication/logout", {token : localStorage.getItem('tokens')})
|
|
// .then(res => {
|
|
// this.context.setToken("");
|
|
// this.context.setUserData({});
|
|
//
|
|
// localStorage.removeItem('id_token');
|
|
// localStorage.removeItem('user.name');
|
|
// localStorage.removeItem('user.profile_image');
|
|
// localStorage.setItem('isLoggingIn', false);
|
|
// let cookies = document.cookie.split(";");
|
|
// for (let i = 0; i < cookies.length; i++) {
|
|
// this.eraseCookie(cookies[i].split("=")[0]);
|
|
// }
|
|
//
|
|
// if (window && window.Tawk_API && window.Tawk_API.isChatHidden()) {
|
|
// window.Tawk_API.hideWidget()
|
|
// }
|
|
//
|
|
// return res;
|
|
// })
|
|
// .catch(err => {
|
|
// throw err;
|
|
// })
|
|
|
|
}
|
|
|
|
@action
|
|
register(data) {
|
|
this.isRegistering = true;
|
|
return this.http.post("register", data)
|
|
.then(res => {
|
|
this.isRegistering = false;
|
|
return res;
|
|
})
|
|
.catch(err => {
|
|
this.isRegistering = false;
|
|
throw err;
|
|
})
|
|
}
|
|
|
|
createCookie(name,value,days) {
|
|
let expires
|
|
if (days) {
|
|
let date = new Date();
|
|
date.setTime(date.getTime()+(days*24*60*60*1000));
|
|
let expires = "; expires="+date.toGMTString();
|
|
}
|
|
else {
|
|
let expires = "";
|
|
}
|
|
document.cookie = name+"="+value+expires+"; path=/";
|
|
}
|
|
|
|
@action
|
|
changePassword(data){
|
|
return this.http.post(`authentication/reset_password`,{password : data.password,old_password : data.old_password,id : this.userProfile.id})
|
|
.then(res=>{
|
|
return res;
|
|
})
|
|
}
|
|
|
|
eraseCookie(name) {
|
|
this.createCookie(name,"",-1);
|
|
}
|
|
|
|
invite(email) {
|
|
this.isInviting = true;
|
|
return this.http.post(`invite_user`, {email})
|
|
.then(res => {
|
|
this.isInviting = false;
|
|
return res;
|
|
})
|
|
}
|
|
|
|
removeUserFromEntity(id) {
|
|
this.removingUser = true;
|
|
return this.http.post('remove_user_from_entity', {id})
|
|
.then(res => {
|
|
this.removingUser = false;
|
|
|
|
return res;
|
|
})
|
|
}
|
|
|
|
inviteUserToEntity(email, roleId, entityId) {
|
|
this.isInviting = true;
|
|
return this.http.post(`invite_user_to_entity_or_project`, {email, role_id: roleId, entity_id: entityId})
|
|
.then(res => {
|
|
this.isInviting = false;
|
|
return res;
|
|
})
|
|
}
|
|
|
|
inviteUserToProject(email, roleId, entityId, projectId) {
|
|
this.isInviting = true;
|
|
return this.http.post(`invite_user_to_entity_or_project`, {email, role_id: roleId, entity_id: entityId, project_id: projectId})
|
|
.then(res => {
|
|
this.isInviting = false;
|
|
return res;
|
|
})
|
|
}
|
|
|
|
@action
|
|
acceptInvite(token, password) {
|
|
return this.http.post('invite_user_accept', {token, password});
|
|
}
|
|
|
|
@action
|
|
getProfile() {
|
|
// console.log('get profile nih');
|
|
this.isLoading = true;
|
|
return this.http.get('profile')
|
|
.then(res => {
|
|
this.userProfile.username = res.user.username;
|
|
this.userProfile.role = res.user.type.name;
|
|
this.isLoading = false;
|
|
// this.userProfile = res;
|
|
})
|
|
.catch(err => {
|
|
this.isLoading = false;
|
|
throw err;
|
|
})
|
|
}
|
|
|
|
@action
|
|
updateProfile() {
|
|
this.isLoading = true;
|
|
return this.http.put('profile', this.userProfile.agent)
|
|
.then(res => this.getProfile())
|
|
.catch(err => {
|
|
this.isLoading = false;
|
|
throw err;
|
|
})
|
|
}
|
|
|
|
@action
|
|
getWallet() {
|
|
this.isLoading = true;
|
|
return this.http.get("wallet")
|
|
.then(res => {
|
|
this.userWallet = res;
|
|
this.isLoading = false;
|
|
})
|
|
.catch(err => {
|
|
this.isLoading = false;
|
|
throw err;
|
|
})
|
|
}
|
|
|
|
@action
|
|
checkEmailAvaliability(data){
|
|
return this.http.get(`users?email=${data}`)
|
|
.then(res=>{
|
|
if(res.length == 0){
|
|
return true;
|
|
}
|
|
else{
|
|
return false
|
|
}
|
|
})
|
|
}
|
|
|
|
@action
|
|
checkUsernameAvaliability(data){
|
|
return this.http.get(`users?username=${data}`)
|
|
.then(res=>{
|
|
if(res.length == 0){
|
|
return true;
|
|
}
|
|
else{
|
|
return false
|
|
}
|
|
})
|
|
}
|
|
|
|
@computed
|
|
get isLoggedIn() {
|
|
// alert(!!(this.context.token));
|
|
return !!(this.context.token);
|
|
}
|
|
|
|
toJson() {
|
|
return {
|
|
items: this.items
|
|
};
|
|
}
|
|
}
|
|
|