soapbox/app/gabsocial/actions/auth.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-04-04 20:28:57 +00:00
import api from '../api';
2020-04-11 19:41:13 +00:00
import { showAlert } from 'gabsocial/actions/alerts';
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
export function createAuthApp() {
2020-04-04 20:28:57 +00:00
return (dispatch, getState) => {
const appToken = getState().getIn(['auth', 'app', 'access_token']);
if (appToken) return; // Skip for now, FIXME: call verify_credentials
2020-04-06 01:29:19 +00:00
return api(getState).post('/api/v1/apps', {
2020-04-05 21:54:51 +00:00
// TODO: Add commit hash to client_name
2020-04-04 20:28:57 +00:00
client_name: `SoapboxFE_${(new Date()).toISOString()}`,
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
2020-04-14 18:44:40 +00:00
scopes: 'read write follow push admin',
2020-04-04 20:28:57 +00:00
}).then(response => {
2020-04-05 21:54:51 +00:00
dispatch(authAppCreated(response.data));
2020-04-05 23:39:22 +00:00
}).then(() => {
const app = getState().getIn(['auth', 'app']);
return api(getState).post('/oauth/token', {
client_id: app.get('client_id'),
client_secret: app.get('client_secret'),
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
2020-04-14 18:44:40 +00:00
grant_type: 'client_credentials',
2020-04-05 23:39:22 +00:00
});
}).then(response => {
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
}
export function logIn(username, password) {
return (dispatch, getState) => {
2020-04-05 21:54:51 +00:00
const app = getState().getIn(['auth', 'app']);
2020-04-06 01:29:19 +00:00
return api(getState).post('/oauth/token', {
2020-04-05 23:39:22 +00:00
client_id: app.get('client_id'),
client_secret: app.get('client_secret'),
2020-04-04 20:28:57 +00:00
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'password',
username: username,
2020-04-14 18:44:40 +00:00
password: password,
2020-04-04 20:28:57 +00:00
}).then(response => {
2020-04-05 21:54:51 +00:00
dispatch(authLoggedIn(response.data));
2020-04-11 19:41:13 +00:00
}).catch((error) => {
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-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
};
}