c9755bee7c
* Added session check before route changes, but express isn't finding route * Added a logout component. Changed route to logout on server. Typing 'logout' in URL will logout of Vue + Express * Removed commented text from previous version * Updated logout function to comply with formatting and eliminate unused blocks * Added package-lock.json back * package-lock.json * recreated package-lock file * fix(auth): allow logout from direct visit to /logout path * fix(merge): clean up more misc changes * fix(merge): remove extra file
73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
import axios from 'axios';
|
|
|
|
const LOCALSTORAGE_AUTH_KEY = 'habit-mobile-settings';
|
|
const LOCALSTORAGE_SOCIAL_AUTH_KEY = 'hello'; // Used by hello.js for social auth
|
|
|
|
export async function register (store, params) {
|
|
let url = '/api/v4/user/auth/local/register';
|
|
|
|
if (params.groupInvite) url += `?groupInvite=${params.groupInvite}`;
|
|
|
|
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,
|
|
},
|
|
});
|
|
localStorage.setItem(LOCALSTORAGE_AUTH_KEY, userLocalData);
|
|
}
|
|
|
|
export async function login (store, params) {
|
|
let url = '/api/v4/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,
|
|
},
|
|
});
|
|
|
|
localStorage.setItem(LOCALSTORAGE_AUTH_KEY, userLocalData);
|
|
}
|
|
|
|
export async function socialAuth (store, params) {
|
|
let url = '/api/v4/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,
|
|
},
|
|
});
|
|
|
|
localStorage.setItem(LOCALSTORAGE_AUTH_KEY, userLocalData);
|
|
}
|
|
|
|
export function logout () {
|
|
localStorage.removeItem(LOCALSTORAGE_AUTH_KEY);
|
|
localStorage.removeItem(LOCALSTORAGE_SOCIAL_AUTH_KEY);
|
|
window.location.href = '/logout-server';
|
|
}
|