2020-04-04 20:28:57 +00:00
|
|
|
import api from '../api';
|
2020-04-29 19:07:22 +00:00
|
|
|
import { showAlert, showAlertForError } from 'gabsocial/actions/alerts';
|
2020-04-24 00:12:42 +00:00
|
|
|
import { fetchMe } from 'gabsocial/actions/me';
|
2020-04-04 20:28:57 +00:00
|
|
|
|
2020-04-05 23:39:22 +00:00
|
|
|
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
|
|
|
|
export const AUTH_APP_AUTHORIZED = 'AUTH_APP_AUTHORIZED';
|
|
|
|
export const AUTH_LOGGED_IN = 'AUTH_LOGGED_IN';
|
2020-04-11 19:41:13 +00:00
|
|
|
export const AUTH_LOGGED_OUT = 'AUTH_LOGGED_OUT';
|
2020-04-05 21:54:51 +00:00
|
|
|
|
2020-04-23 23:41:20 +00:00
|
|
|
export const AUTH_REGISTER_REQUEST = 'AUTH_REGISTER_REQUEST';
|
|
|
|
export const AUTH_REGISTER_SUCCESS = 'AUTH_REGISTER_SUCCESS';
|
|
|
|
export const AUTH_REGISTER_FAIL = 'AUTH_REGISTER_FAIL';
|
|
|
|
|
2020-04-29 21:53:10 +00:00
|
|
|
const hasAppToken = getState => getState().hasIn(['auth', 'app', 'access_token']);
|
2020-04-29 23:29:41 +00:00
|
|
|
const noOp = () => () => new Promise(f => f());
|
2020-04-29 19:07:22 +00:00
|
|
|
|
2020-04-30 00:10:53 +00:00
|
|
|
function initAuthApp() {
|
2020-04-29 19:07:22 +00:00
|
|
|
return (dispatch, getState) => {
|
2020-04-29 21:53:10 +00:00
|
|
|
const hasToken = hasAppToken(getState);
|
2020-04-30 01:45:04 +00:00
|
|
|
const action = hasToken ? verifyApp : createAppAndToken;
|
2020-04-29 21:53:10 +00:00
|
|
|
return dispatch(action())
|
|
|
|
.catch(error => {
|
|
|
|
dispatch(showAlertForError(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAppAndToken() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
return dispatch(createApp()).then(() => {
|
2020-04-30 00:10:53 +00:00
|
|
|
return dispatch(createAppToken());
|
2020-04-29 19:07:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:53:10 +00:00
|
|
|
const appName = () => {
|
|
|
|
const timestamp = (new Date()).toISOString();
|
|
|
|
return `SoapboxFE_${timestamp}`; // TODO: Add commit hash
|
|
|
|
};
|
|
|
|
|
|
|
|
function createApp() {
|
2020-04-04 20:28:57 +00:00
|
|
|
return (dispatch, getState) => {
|
2020-04-29 19:07:22 +00:00
|
|
|
return api(getState, 'app').post('/api/v1/apps', {
|
2020-04-29 21:53:10 +00:00
|
|
|
client_name: appName(),
|
2020-04-04 20:28:57 +00:00
|
|
|
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
|
2020-04-29 19:07:22 +00:00
|
|
|
scopes: 'read write follow push admin',
|
2020-04-04 20:28:57 +00:00
|
|
|
}).then(response => {
|
2020-04-30 00:10:53 +00:00
|
|
|
return dispatch(authAppCreated(response.data));
|
2020-04-29 19:07:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:53:10 +00:00
|
|
|
function createAppToken() {
|
2020-04-29 19:07:22 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const app = getState().getIn(['auth', 'app']);
|
|
|
|
|
2020-04-29 21:53:10 +00:00
|
|
|
return api(getState, 'app').post('/oauth/token', {
|
2020-04-29 19:07:22 +00:00
|
|
|
client_id: app.get('client_id'),
|
|
|
|
client_secret: app.get('client_secret'),
|
2020-04-29 21:53:10 +00:00
|
|
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
grant_type: 'client_credentials',
|
2020-04-05 23:39:22 +00:00
|
|
|
}).then(response => {
|
2020-04-30 00:10:53 +00:00
|
|
|
return dispatch(authAppAuthorized(response.data));
|
2020-04-04 20:28:57 +00:00
|
|
|
});
|
2020-04-14 18:44:40 +00:00
|
|
|
};
|
2020-04-04 20:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 01:45:04 +00:00
|
|
|
function verifyApp() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
return api(getState, 'app').get('/api/v1/apps/verify_credentials')
|
|
|
|
.then (response => dispatch(authAppCreated(response.data)))
|
|
|
|
.catch(error => dispatch(createAppAndToken()));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-30 00:10:53 +00:00
|
|
|
function createUserToken(username, password) {
|
2020-04-04 20:28:57 +00:00
|
|
|
return (dispatch, getState) => {
|
2020-04-05 21:54:51 +00:00
|
|
|
const app = getState().getIn(['auth', 'app']);
|
2020-04-29 21:53:10 +00:00
|
|
|
return api(getState, 'app').post('/oauth/token', {
|
|
|
|
client_id: app.get('client_id'),
|
2020-04-05 23:39:22 +00:00
|
|
|
client_secret: app.get('client_secret'),
|
2020-04-29 21:53:10 +00:00
|
|
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
grant_type: 'password',
|
|
|
|
username: username,
|
|
|
|
password: password,
|
2020-04-30 00:38:24 +00:00
|
|
|
}).then(response => {
|
|
|
|
dispatch(authLoggedIn(response.data));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function refreshUserToken() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const refreshToken = getState().getIn(['auth', 'user', 'refresh_token']);
|
|
|
|
const app = getState().getIn(['auth', 'app']);
|
|
|
|
|
|
|
|
if (!refreshToken) return dispatch(noOp());
|
|
|
|
|
|
|
|
return api(getState, 'app').post('/oauth/token', {
|
|
|
|
client_id: app.get('client_id'),
|
|
|
|
client_secret: app.get('client_secret'),
|
|
|
|
refresh_token: refreshToken,
|
|
|
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
}).then(response => {
|
|
|
|
dispatch(authLoggedIn(response.data));
|
2020-04-30 00:10:53 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logIn(username, password) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
return dispatch(initAuthApp()).then(() => {
|
|
|
|
return dispatch(createUserToken(username, password));
|
|
|
|
}).catch(error => {
|
2020-04-11 19:41:13 +00:00
|
|
|
dispatch(showAlert('Login failed.', 'Invalid username or password.'));
|
|
|
|
throw error;
|
2020-04-04 20:28:57 +00:00
|
|
|
});
|
2020-04-14 18:44:40 +00:00
|
|
|
};
|
2020-04-04 20:28:57 +00:00
|
|
|
}
|
2020-04-05 21:54:51 +00:00
|
|
|
|
2020-04-11 19:41:13 +00:00
|
|
|
export function logOut() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: AUTH_LOGGED_OUT });
|
|
|
|
dispatch(showAlert('Successfully logged out.', ''));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-23 23:41:20 +00:00
|
|
|
export function register(params) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: AUTH_REGISTER_REQUEST });
|
2020-04-30 00:10:53 +00:00
|
|
|
return dispatch(initAuthApp()).then(() => {
|
|
|
|
return api(getState, 'app').post('/api/v1/accounts', params);
|
|
|
|
}).then(response => {
|
2020-04-24 00:12:42 +00:00
|
|
|
dispatch({ type: AUTH_REGISTER_SUCCESS, token: response.data });
|
|
|
|
dispatch(authLoggedIn(response.data));
|
2020-04-30 00:10:53 +00:00
|
|
|
return dispatch(fetchMe());
|
2020-04-23 23:41:20 +00:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: AUTH_REGISTER_FAIL, error });
|
2020-04-30 01:00:01 +00:00
|
|
|
throw error;
|
2020-04-23 23:41:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-24 01:48:25 +00:00
|
|
|
export function fetchCaptcha() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
return api(getState).get('/api/pleroma/captcha');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-05 21:54:51 +00:00
|
|
|
export function authAppCreated(app) {
|
|
|
|
return {
|
|
|
|
type: AUTH_APP_CREATED,
|
2020-04-14 18:44:40 +00:00
|
|
|
app,
|
2020-04-05 21:54:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-05 23:39:22 +00:00
|
|
|
export function authAppAuthorized(app) {
|
|
|
|
return {
|
|
|
|
type: AUTH_APP_AUTHORIZED,
|
2020-04-14 18:44:40 +00:00
|
|
|
app,
|
2020-04-05 23:39:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-05 21:54:51 +00:00
|
|
|
export function authLoggedIn(user) {
|
|
|
|
return {
|
|
|
|
type: AUTH_LOGGED_IN,
|
2020-04-14 18:44:40 +00:00
|
|
|
user,
|
2020-04-05 21:54:51 +00:00
|
|
|
};
|
|
|
|
}
|