soapbox/app/gabsocial/actions/auth.js

48 wiersze
1.1 KiB
JavaScript
Czysty Zwykły widok Historia

2020-04-04 20:28:57 +00:00
import api from '../api';
2020-04-05 21:54:51 +00:00
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
export const AUTH_LOGGED_IN = 'AUTH_LOGGED_IN';
export function createAuthApp() {
2020-04-04 20:28:57 +00:00
return (dispatch, getState) => {
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',
scopes: 'read write follow push admin'
}).then(response => {
2020-04-05 21:54:51 +00:00
dispatch(authAppCreated(response.data));
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-04 20:28:57 +00:00
api(getState).post('/oauth/token', {
client_id: app.client_id,
client_secret: app.client_secret,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'password',
username: username,
password: password
}).then(response => {
2020-04-05 21:54:51 +00:00
dispatch(authLoggedIn(response.data));
2020-04-04 20:28:57 +00:00
});
}
}
2020-04-05 21:54:51 +00:00
export function authAppCreated(app) {
return {
type: AUTH_APP_CREATED,
app
};
}
export function authLoggedIn(user) {
return {
type: AUTH_LOGGED_IN,
user
};
}