Skip to content

Commit

Permalink
Merge pull request #365 from kpi-ua/cleanup
Browse files Browse the repository at this point in the history
Update KPI ID integration
  • Loading branch information
a-gubskiy authored Feb 7, 2025
2 parents 5e64037 + e979e39 commit dc452f6
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 63 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Faq from './components/Faq';
import FindCurator from './components/FindCurator';
import AuthContainerDefault from './components/AuthContainerDefault';
import EmploymentSystem from './components/EmploymentSystem';
import KpiId from './components/KPIID';

const InternalLogin = () => {
return (
Expand Down Expand Up @@ -68,6 +69,7 @@ class App extends Component {
<Route exact path="/bb" component={BbIndex} />
<Route exact path="/feedback" component={Feedback} />
<Route exact path="/faq" component={Faq} />
<Route exact path="/kpi-id" component={KpiId} />
<Route exact path="/settings" component={Settings} />
<Route exact path="/settings-editor" component={SettingsEditor} />
<Route
Expand Down
48 changes: 41 additions & 7 deletions src/CampusClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,46 @@ export const auth = async (login, password) => {
return await getCurrentUser();
};

/**
* Exchange KPI ID ticket for sessionId and token
* @param {string} ticketId
* @returns {Promise<Object|null>}
*/
export const exchangeKpiIdTicket = async (ticketId) => {
if (!ticketId) {
console.warn('Ticket ID is required');
return null;
}

const response = await fetch(`${ApplicationConfiguration.ApiEndpoint}auth/kpi-id?ticketId=${encodeURIComponent(ticketId)}`, {
method: 'GET',
cache: 'no-cache',
headers: {
'Accept': 'application/json',
},
});

if (response.status !== 200) {

console.warn(`Failed to exchange ticketId: ${ticketId}`);

return null;
}

const credentialsCollection = await response.json();

if (!credentialsCollection || credentialsCollection.length === 0) {
return null;
}

// Assuming the first user in the response should be used
const credentials = credentialsCollection[0];

await storeCredentials(credentials.sessionId, credentials.access_token);

return await getCurrentUser();
};

/**
* Logout from Campus API
* @returns {Promise<void>}
Expand Down Expand Up @@ -256,13 +296,7 @@ const toUrlEncode = (obj) => {
* @returns {Promise<void>}
*/
export const getBulletinBoardForCurrentUser = async (page, size) => {
const response = await callApi(`Board/All?page=${page}&size=${size}`, 'GET');

if (response.status < 200 || response.status >= 300) {
return null;
}

return await response.json();
return null;
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/components/Bb/BbList.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BbList extends React.Component {
1,
this.state.pageSize,
);

if (!!response) {
this.setState({
items: response.data,
Expand Down
1 change: 0 additions & 1 deletion src/components/Faq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component } from 'react';
// import { Link } from 'react-router-dom';

class Faq extends Component {
render() {
Expand Down
60 changes: 60 additions & 0 deletions src/components/KPIID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { Redirect } from 'react-router-dom';
import * as campus from '../CampusClient';

class KpiId extends React.Component {
state = {
modal: false,
redirect: null
};

constructor(props) {
super(props);
}

componentDidMount = async () => {
try {
const params = new URLSearchParams(this.props.location.search);
const ticketId = params.get('ticketId');

if (!ticketId) {
this.setState({ error: 'Ticket ID is missing' });
return;
}

const currentUser = await campus.exchangeKpiIdTicket(ticketId);

if (!!currentUser) {
this.setState({ redirect: '/home' });
} else {
this.setState({ error: 'Failed to authenticate using KPI ID' });
}
} catch (error) {
console.error('Error during KPI ID authentication:', error);
this.setState({ error: 'An unexpected error occurred' });
}
};

render() {
if (!!this.state.redirect) {
return <Redirect to={this.state.redirect} />;
}

return (
<section>
<div className="row">
<div className="col-md-12">
<h1>KPI ID</h1>
{this.state.error ? (
<div className="alert alert-danger">{this.state.error}</div>
) : (
<p>Authorizing...</p>
)}
</div>
</div>
</section>
);
}
}

export default KpiId;
116 changes: 61 additions & 55 deletions src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,76 @@ class Login extends React.Component {
};

componentDidMount = async () => {
let currentUser = await campus.getCurrentUser(true);
try {
const currentUser = await campus.getCurrentUser(true);

if (!!currentUser) {
this.setState({redirect: '/home'});
if (!!currentUser) {
this.setState({ redirect: '/home' });
} else {
this.setState({ error: 'Failed to authenticate using KPI ID' });
}
} catch (error) {
console.error('Error during KPI ID authentication:', error);
this.setState({ error: 'An unexpected error occurred' });
}
};

render() {

if (!!this.state.redirect) {
return <Redirect to={this.state.redirect} />;
}

return (
<section>
<div className='row'>
<div className='col-md-4' />
<div className='col-md-4'>
<div className="row">
<div className="col-md-4" />
<div className="col-md-4">
<br />
<div className='card'>
<div className='card-body'>
<div className='text-center'>
<a href='/'>
<div className="card">
<div className="card-body">
<div className="text-center">
<a href="/">
{' '}
<img
src='/images/logo-big-green.png'
alt='Електроний кампус'
className='img-responsive logo-green'
src="/images/logo-big-green.png"
alt="Електроний кампус"
className="img-responsive logo-green"
/>{' '}
</a>
<h2 className='text-center'>Авторизацiя у системi</h2>
<div className='panel-body'>{this.props.children}</div>
<h2 className="text-center">Авторизацiя у системi</h2>
<div className="panel-body">{this.props.children}</div>
</div>
</div>
</div>
</div>
<div className='col-md-4' />
<div className="col-md-4" />
</div>

{!this.props.isExternal ?
<>
<div className='row'>
<div className='col-md-4' />
<div className='col-md-4'>
<div className='card'>
<div className='card-body'>
<div className='text-center'>
<div className='panel-body'>
<div className='row'>
<div className='col-md-4'>
<Link className='menu-icon' to={`/restore-password`}>
<i className='fa fa-unlock-alt' aria-hidden='true' />
<div className="row">
<div className="col-md-4" />
<div className="col-md-4">
<div className="card">
<div className="card-body">
<div className="text-center">
<div className="panel-body">
<div className="row">
<div className="col-md-4">
<Link className="menu-icon" to={`/restore-password`}>
<i className="fa fa-unlock-alt" aria-hidden="true" />
Вiдновити втрачений пароль
</Link>
</div>
<div className='col-md-4'>
<Link className='menu-icon' to={`/find-curator`}>
<i className='fa fa-search' aria-hidden='true' />
<div className="col-md-4">
<Link className="menu-icon" to={`/find-curator`}>
<i className="fa fa-search" aria-hidden="true" />
Знайти куратора групи
</Link>
</div>
<div className='col-md-4'>
<Link className='menu-icon' to={`/feedback`}>
<i className='fa fa-comments-o' aria-hidden='true' />
<div className="col-md-4">
<Link className="menu-icon" to={`/feedback`}>
<i className="fa fa-comments-o" aria-hidden="true" />
Форма скарг i пропозицiй
</Link>
</div>
Expand All @@ -83,35 +89,35 @@ class Login extends React.Component {
</div>
</div>
</div>
<div className='col-md-4' />
<div className="col-md-4" />
</div>

<div className='row'>
<div className='col-md-4' />
<div className='col-md-4'>
<div className='card'>
<div className='card-body'>
<div className='text-center'>
<div className='panel-body'>
<div className='row'>
<div className='col-md-4'>
<div className="row">
<div className="col-md-4" />
<div className="col-md-4">
<div className="card">
<div className="card-body">
<div className="text-center">
<div className="panel-body">
<div className="row">
<div className="col-md-4">
<SupportInformationDialog />
</div>
<div className='col-md-4'>
<div className="col-md-4">
<a
target='_tg'
href='https://t.me/joinchat/HtJ6IROiP8Rv5BR-eZ64fw'
className='menu-icon'
target="_tg"
href="https://t.me/joinchat/HtJ6IROiP8Rv5BR-eZ64fw"
className="menu-icon"
>
<i className='fa fa-telegram' aria-hidden='true' />
<i className="fa fa-telegram" aria-hidden="true" />
Telegram чат
</a>
</div>
<div className='col-md-4'>
<Link className='menu-icon' to={`/faq`}>
<div className="col-md-4">
<Link className="menu-icon" to={`/faq`}>
<i
className='fa fa-question-circle'
aria-hidden='true'
className="fa fa-question-circle"
aria-hidden="true"
/>
Поширенi запитання
</Link>
Expand All @@ -122,12 +128,12 @@ class Login extends React.Component {
</div>
</div>
</div>
<div className='col-md-4' />
<div className="col-md-4" />
</div>
</> : null}
</section>
);
}
}

export default Login;
export default Login;

0 comments on commit dc452f6

Please sign in to comment.