Change models, new migrations, fix front and make tests work
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -1,87 +1,61 @@
|
||||
import router from '../router/index';
|
||||
import { API_HOST, API_PATH } from '../api/_entrypoint';
|
||||
|
||||
// URL and endpoint constants
|
||||
const LOGIN_URL = `${API_HOST}${API_PATH}/login`;
|
||||
const SIGNUP_URL = `${API_HOST}${API_PATH}/users/`;
|
||||
const CHECK_AUTH = `${API_HOST}${API_PATH}/users/`;
|
||||
const CHECK_AUTH = `${API_HOST}${API_PATH}/user/`;
|
||||
const REFRESH_TOKEN = `${API_HOST}${API_PATH}/token/refresh`;
|
||||
|
||||
function AuthError(field, message) {
|
||||
this.field = field;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
AuthError.prototype.toString = function AuthErrorToString() {
|
||||
return `AuthError: ${this.message}`;
|
||||
};
|
||||
|
||||
export default {
|
||||
|
||||
// User object will let us check authentication status
|
||||
user: false,
|
||||
authenticated: false,
|
||||
token: false,
|
||||
|
||||
// Send a request to the login URL and save the returned JWT
|
||||
login(creds, $store, redirect, error) {
|
||||
login(creds, success, error) {
|
||||
fetch(LOGIN_URL, { method: 'POST', body: creds, headers: { 'Content-Type': 'application/json' } })
|
||||
.then(response => response.json())
|
||||
.then((data) => {
|
||||
if (data.code >= 300) {
|
||||
throw new AuthError(null, data.message);
|
||||
.then((response) => {
|
||||
if (response.status === 200) {
|
||||
return response.json();
|
||||
}
|
||||
$store.commit('LOGIN_USER');
|
||||
|
||||
throw response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
localStorage.setItem('token', data.token);
|
||||
localStorage.setItem('refresh_token', data.refresh_token);
|
||||
this.getUser(
|
||||
$store,
|
||||
() => router.push(redirect)
|
||||
);
|
||||
|
||||
}).catch((err) => {
|
||||
error(err);
|
||||
});
|
||||
// localStorage.setItem('refresh_token', data.refresh_token);
|
||||
return success(data);
|
||||
})
|
||||
.catch(err => error(err));
|
||||
},
|
||||
|
||||
signup(creds, $store, redirect, error) {
|
||||
signup(creds, success, error) {
|
||||
fetch(SIGNUP_URL, { method: 'POST', body: creds, headers: { 'Content-Type': 'application/json' } })
|
||||
.then(response => response.json())
|
||||
.then((response) => {
|
||||
if (response.status === 200) {
|
||||
return response.json();
|
||||
}
|
||||
throw response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
if (data.error) {
|
||||
throw new AuthError(data.error.field, data.error.message);
|
||||
}
|
||||
|
||||
$store.commit('LOGIN_USER');
|
||||
localStorage.setItem('token', data.token);
|
||||
localStorage.setItem('refresh_token', data.refresh_token);
|
||||
// localStorage.setItem('refresh_token', data.refresh_token);
|
||||
|
||||
if (redirect) {
|
||||
router.push(redirect);
|
||||
}
|
||||
}).catch((err) => {
|
||||
error(err);
|
||||
});
|
||||
return success(data);
|
||||
}).catch(err => error(err));
|
||||
},
|
||||
refreshToken(store, successHandler, errorHandler) {
|
||||
const refreshToken = localStorage.getItem('refresh_token');
|
||||
console.log("We are refreshing the jwt token");
|
||||
fetch(REFRESH_TOKEN, { method: 'POST', body: JSON.stringify({refresh_token: refreshToken}), headers: { 'Content-Type': 'application/json' }})
|
||||
console.log('We are refreshing the jwt token');
|
||||
fetch(REFRESH_TOKEN, { method: 'POST', body: JSON.stringify({ refresh_token: refreshToken }), headers: { 'Content-Type': 'application/json' } })
|
||||
.then((response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
} else {
|
||||
errorHandler('Error while authenticating');
|
||||
}
|
||||
return errorHandler('Error while authenticating');
|
||||
})
|
||||
.then((response) => {
|
||||
console.log("We have a new token");
|
||||
console.log('We have a new token');
|
||||
this.authenticated = true;
|
||||
store.commit('LOGIN_USER', response);
|
||||
localStorage.setItem('token', response.token);
|
||||
console.log("Let's try to auth again");
|
||||
this.getUser(store, successHandler, errorHandler);
|
||||
successHandler();
|
||||
});
|
||||
},
|
||||
@@ -114,19 +88,8 @@ export default {
|
||||
return expirationDate < new Date();
|
||||
},
|
||||
|
||||
checkAuth(store = null) {
|
||||
const token = localStorage.getItem('token');
|
||||
if (store && token) {
|
||||
this.getUser(store,() => null, () => null);
|
||||
}
|
||||
/* if (!!token && store && !this.isTokenExpired(token)) {
|
||||
this.refreshToken(store, () => null, () => null);
|
||||
} */
|
||||
return !!token;
|
||||
},
|
||||
|
||||
getUser(store, successHandler, errorHandler) {
|
||||
console.log("We are checking the auth");
|
||||
console.log('We are checking the auth');
|
||||
this.token = localStorage.getItem('token');
|
||||
const options = {};
|
||||
options.headers = new Headers();
|
||||
@@ -135,16 +98,14 @@ export default {
|
||||
.then((response) => {
|
||||
if (response.ok) {
|
||||
return response.json();
|
||||
} else {
|
||||
errorHandler('Error while authenticating');
|
||||
}
|
||||
})
|
||||
.then((response) => {
|
||||
return errorHandler('Error while authenticating');
|
||||
}).then((response) => {
|
||||
this.authenticated = true;
|
||||
console.log(response);
|
||||
store.commit('SAVE_USER', response);
|
||||
successHandler();
|
||||
});
|
||||
store.commit('LOAD_USER', response.data);
|
||||
return successHandler();
|
||||
});
|
||||
},
|
||||
|
||||
// The object to be passed as a header for authenticated requests
|
||||
|
||||
Reference in New Issue
Block a user