implemented loginAPI

This commit is contained in:
Ulf Gebhardt 2021-02-26 00:36:17 +01:00
parent beb21f4054
commit d4530e2b41
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
2 changed files with 82 additions and 19 deletions

View File

@ -0,0 +1,82 @@
import axios from 'axios';
// TODO move this
const LOGIN_API_URL = 'http://localhost/login_api/'
// control email-text sended with email verification code
const EMAIL_TYPE = {
DEFAULT: 2, // if user has registered directly
ADMIN: 5, // if user was registered by an admin
}
// define a mixin object
const loginAPI = {
login: async (email, password) => {
const payload = {
email,
password,
}
try {
const result = await axios.post(LOGIN_API_URL + 'unsecureLogin', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
},
logout: async (session_id) => {
const payload= { session_id }
try {
const result = await axios.post(LOGIN_API_URL + 'logout', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.details)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
},
create : async (email, first_name, last_name, password) => {
const payload = {
email,
first_name,
last_name,
password,
emailType: EMAIL_TYPE.DEFAULT
}
try {
const result = await axios.post(LOGIN_API_URL + 'createUser', payload);
if(result.status !== 200){
throw new Error('HTTP Status Error '+result.status)
}
if(result.data.state === 'error'){
throw new Error(result.data.details)
}
if(result.data.state === 'exists'){
throw new Error(result.data.msg)
}
if(result.data.state !== 'success'){
throw new Error(result.data)
}
return { success: true, result }
} catch(error){
return { success: false, result: error}
}
},
}
export default loginAPI

View File

@ -1,19 +0,0 @@
// TODO move this
const LOGIN_API_URL = 'http://localhost/login_api/'
// define a mixin object
const loginAPI = {
mutations: {
login: async () => {
return axios.post(LOGIN_API_URL + 'unsecureLogin', data);
},
creatUser : async () => {
return axios.post(LOGIN_API_URL + 'createUser', data);
},
logout: async () => {
return axios.post(LOGIN_API_URL + 'logout', data);
},
}
}
export default loginAPI