2017-06-28 03:53:59 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
2017-08-29 14:18:26 +00:00
|
|
|
const LOCALSTORAGE_AUTH_KEY = 'habit-mobile-settings';
|
|
|
|
|
const LOCALSTORAGE_SOCIAL_AUTH_KEY = 'hello'; // Used by hello.js for social auth
|
|
|
|
|
|
2017-06-28 03:53:59 +00:00
|
|
|
export async function register (store, params) {
|
|
|
|
|
let url = '/api/v3/user/auth/local/register';
|
|
|
|
|
let result = await axios.post(url, {
|
|
|
|
|
username: params.username,
|
|
|
|
|
email: params.email,
|
|
|
|
|
password: params.password,
|
|
|
|
|
confirmPassword: params.passwordConfirm,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let user = result.data.data;
|
|
|
|
|
|
|
|
|
|
let userLocalData = JSON.stringify({
|
|
|
|
|
auth: {
|
|
|
|
|
apiId: user._id,
|
|
|
|
|
apiToken: user.apiToken,
|
|
|
|
|
},
|
|
|
|
|
});
|
2017-08-29 14:18:26 +00:00
|
|
|
localStorage.setItem(LOCALSTORAGE_AUTH_KEY, userLocalData);
|
2017-06-28 03:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function login (store, params) {
|
|
|
|
|
let url = '/api/v3/user/auth/local/login';
|
|
|
|
|
let result = await axios.post(url, {
|
|
|
|
|
username: params.username,
|
|
|
|
|
// email: params.email,
|
|
|
|
|
password: params.password,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let user = result.data.data;
|
|
|
|
|
|
|
|
|
|
let userLocalData = JSON.stringify({
|
|
|
|
|
auth: {
|
|
|
|
|
apiId: user.id,
|
|
|
|
|
apiToken: user.apiToken,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2017-08-29 14:18:26 +00:00
|
|
|
localStorage.setItem(LOCALSTORAGE_AUTH_KEY, userLocalData);
|
2017-06-28 03:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function socialAuth (store, params) {
|
|
|
|
|
let url = '/api/v3/user/auth/social';
|
|
|
|
|
let result = await axios.post(url, {
|
|
|
|
|
network: params.auth.network,
|
|
|
|
|
authResponse: params.auth.authResponse,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let user = result.data.data;
|
|
|
|
|
|
|
|
|
|
let userLocalData = JSON.stringify({
|
|
|
|
|
auth: {
|
|
|
|
|
apiId: user.id,
|
|
|
|
|
apiToken: user.apiToken,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2017-08-29 14:18:26 +00:00
|
|
|
localStorage.setItem(LOCALSTORAGE_AUTH_KEY, userLocalData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function logout () {
|
|
|
|
|
localStorage.removeItem(LOCALSTORAGE_AUTH_KEY);
|
|
|
|
|
localStorage.removeItem(LOCALSTORAGE_SOCIAL_AUTH_KEY);
|
|
|
|
|
window.location.href = '/logout';
|
2017-06-28 03:53:59 +00:00
|
|
|
}
|