1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 07:39:44 +02:00
planka/client/src/sagas/login/services/login.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-10-17 19:18:19 +02:00
import { apply, call, put, select } from 'redux-saga/effects';
import { replace } from '../../../lib/redux-router';
2019-08-31 04:07:25 +05:00
2023-10-17 19:18:19 +02:00
import selectors from '../../../selectors';
2022-08-04 13:31:14 +02:00
import actions from '../../../actions';
import api from '../../../api';
2023-10-17 19:18:19 +02:00
import { createOidcManager } from '../../../utils/oidc-manager';
import { setAccessToken } from '../../../utils/access-token-storage';
2023-10-17 19:18:19 +02:00
import Paths from '../../../constants/Paths';
export function* initializeLogin() {
const { item: config } = yield call(api.getConfig); // TODO: handle error
yield put(actions.initializeLogin(config));
}
2019-08-31 04:07:25 +05:00
2022-08-04 13:31:14 +02:00
export function* authenticate(data) {
yield put(actions.authenticate(data));
2023-10-17 19:18:19 +02:00
let accessToken;
try {
2023-10-17 19:18:19 +02:00
({ item: accessToken } = yield call(api.createAccessToken, data));
} catch (error) {
2022-08-04 13:31:14 +02:00
yield put(actions.authenticate.failure(error));
return;
}
yield call(setAccessToken, accessToken);
2022-08-04 13:31:14 +02:00
yield put(actions.authenticate.success(accessToken));
2019-08-31 04:07:25 +05:00
}
2023-10-17 19:18:19 +02:00
export function* authenticateWithOidc() {
const oidcConfig = yield select(selectors.selectOidcConfig);
const oidcManager = createOidcManager(oidcConfig);
yield apply(oidcManager, oidcManager.login);
}
export function* authenticateWithOidcCallback() {
const oidcConfig = yield select(selectors.selectOidcConfig);
const oidcManager = createOidcManager(oidcConfig);
let oidcToken;
try {
({ access_token: oidcToken } = yield apply(oidcManager, oidcManager.loginCallback));
} catch (error) {
yield put(actions.authenticateWithOidc.failure(error));
}
yield put(replace(Paths.LOGIN));
if (oidcToken) {
let accessToken;
try {
({ item: accessToken } = yield call(api.exchangeToAccessToken, {
token: oidcToken,
}));
} catch (error) {
yield put(actions.authenticateWithOidc.failure(error));
return;
}
yield call(setAccessToken, accessToken);
yield put(actions.authenticateWithOidc.success(accessToken));
}
}
2022-08-04 13:31:14 +02:00
export function* clearAuthenticateError() {
yield put(actions.clearAuthenticateError());
2019-08-31 04:07:25 +05:00
}
2022-08-04 13:31:14 +02:00
export default {
2023-10-17 19:18:19 +02:00
initializeLogin,
2022-08-04 13:31:14 +02:00
authenticate,
2023-10-17 19:18:19 +02:00
authenticateWithOidc,
authenticateWithOidcCallback,
2022-08-04 13:31:14 +02:00
clearAuthenticateError,
};