diff --git a/public/config.json b/public/config.json index eb91e6d6..9e71a366 100644 --- a/public/config.json +++ b/public/config.json @@ -1,5 +1,6 @@ { "ApiEndpoint": "https://api.campus.kpi.ua/", "OldUIAddress": "https://campus.kpi.ua/", - "LoginPageAddress": "https://ecampus.kpi.ua/login" + "LoginPageAddress": "https://ecampus.kpi.ua/login", + "AppId": "3d1488ae-128e-4655-8ca2-1ef554379335" } diff --git a/src/App.js b/src/App.js index de5ab560..3a5eb07c 100644 --- a/src/App.js +++ b/src/App.js @@ -11,7 +11,6 @@ import SocialForbidden from './components/SocialForbidden'; import Header from './components/Header'; import Login from './components/Login'; import LecturerHelp from './components/LecturerHelp'; -import KPIIDLogin from './components/KPIIDLogin'; import * as campus from './CampusClient'; import Settings from './components/Settings'; import SettingsEditor from './components/SettingsEditor'; @@ -21,7 +20,6 @@ import Feedback from './components/Feedback'; import Faq from './components/Faq'; import FindCurator from './components/FindCurator'; import AuthContainerDefault from './components/AuthContainerDefault'; -import AuthContainerExternal from './components/AuthContainerExternal'; import EmploymentSystem from './components/EmploymentSystem'; const InternalLogin = () => { @@ -32,13 +30,6 @@ const InternalLogin = () => { ); }; -const ExternalLogin = () => { - return ( - - - - ); -}; class App extends Component { constructor(props) { @@ -50,7 +41,7 @@ class App extends Component { } async componentDidMount() { - this.setState({ user: await campus.getCurrentUser() }); + this.setState({ user: await campus.getCurrentUser(true) }); } render() { @@ -67,8 +58,6 @@ class App extends Component { - - diff --git a/src/ApplicationConfiguration.js b/src/ApplicationConfiguration.js index ca4dbab1..aeb70de9 100644 --- a/src/ApplicationConfiguration.js +++ b/src/ApplicationConfiguration.js @@ -10,6 +10,10 @@ class ApplicationConfiguration { return this.configuratiun.ApiEndpoint; } + get KpiIdRedirectAddress() { + return `https://auth.kpi.ua?appId=${this.configuratiun.AppId}`; + } + get OldUIAddress() { return this.configuratiun.OldUIAddress; } diff --git a/src/CampusClient.js b/src/CampusClient.js index 58d7b0f4..d964673f 100644 --- a/src/CampusClient.js +++ b/src/CampusClient.js @@ -4,10 +4,6 @@ import ApplicationConfiguration from './ApplicationConfiguration'; * Application configuration */ export const config = { - fb: { - appId: '1214335051921931', - redirectUrl: `${ApplicationConfiguration.ApiEndpoint}account/oauth/login/fb`, - }, appDomains: [ 'kpi.ua', 'campus.kpi.ua', @@ -15,8 +11,8 @@ export const config = { 'login.kpi.ua', 'localtest.me', 'api.localtest.me', - 'ecampus.localtest.me' - ] + 'ecampus.localtest.me', + ], }; /** @@ -57,72 +53,6 @@ export const auth = async (login, password) => { return await getCurrentUser(); }; -export const externalAuth = (login, password, appId, redirectionId) => { - const payload = { - Username: login, - Password: password, - AppId: appId, - RedirectUrl: redirectionId, - }; - - return fetch(`https://ecampus.kpi.ua/oauth/app/authorize`, { - method: 'POST', - cache: 'no-cache', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - body: toUrlEncode(payload), - }); -}; - -/** - * Request secret code for KPI ID auth - * @param phone - * @returns {Promise} - */ -export const requestKpiIdSecret = async (phone) => { - return await callApi( - 'Account/oauth/login/kpiid/secret?phone=' + phone, - 'GET', - ); -}; - -/** - * - * @param phone - * @param secret - * @returns {Promise} - */ -export const authByKpiId = async (phone, secret) => { - const payload = { - phone: phone, - secret: secret, - }; - const response = await fetch(`${ApplicationConfiguration.ApiEndpoint}Account/oauth/login/kpiid`, { - method: 'POST', - cache: 'no-cache', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(payload), - }); - - if (response.status < 200 || response.status >= 300) { - console.warn(`Incorrect credentials`); - return null; - } - - const credentials = await response.json(); - - if (!credentials) { - return null; - } - - await storeCredentials(credentials.sessionId, credentials.access_token); - - return await getCurrentUser(); -}; - - - /** * Logout from system * @returns {Promise} @@ -189,15 +119,6 @@ export const callApi = async (path, method, payload = null) => { return await fetch(url, request); }; -/** - * Get URL for Facebook auth - * @returns {string} - */ -export const generateFacebookAuthorizationLink = () => { - const scope = 'email'; - return `https://www.facebook.com/dialog/oauth?client_id=${config.fb.appId}&redirect_uri=${config.fb.redirectUrl}&scope=${scope}`; -}; - /** * Check authorization status for current user * For authorization saved to local storage token will be used @@ -382,12 +303,8 @@ const setCookie = (name, value, domain, days) => { const expires = date.toUTCString(); document.cookie = - name + - '=' + - (value || '') + - ';expires=' + - expires + - ';domain=.' + - domain + + name + '=' + (value || '') + + ';expires=' + expires + + ';domain=.' + domain + ';path=/'; }; diff --git a/src/components/AuthContainerDefault.js b/src/components/AuthContainerDefault.js index c4fd8555..91dab86a 100644 --- a/src/components/AuthContainerDefault.js +++ b/src/components/AuthContainerDefault.js @@ -1,11 +1,17 @@ import React, { useState } from 'react'; import AuthForm from './AuthForm'; import * as campus from '../CampusClient'; -import { useHistory } from 'react-router-dom'; +import { useLocation, useHistory } from 'react-router-dom'; +import ApplicationConfiguration from '../ApplicationConfiguration'; const AuthContainerDefault = () => { const [authFail, setAuthFail] = useState(false); const history = useHistory(); + const location = useLocation(); + + // Parse query parameters + const queryParams = new URLSearchParams(location.search); + const showKpiIdAuthButton = queryParams.get('kid') === 'true'; const authorize = async (e, { login, password }) => { e.preventDefault(); @@ -27,29 +33,21 @@ const AuthContainerDefault = () => { authFail={authFail} dismissInvalid={dismissInvalid} > - {/**/} - {/* */} - {/* */} - {/* */} - {/* */} - {/* Увiйти через Facebook*/} - {/* */} - {/**/} - - {/**/} - {/* */} - {/* */} - {/* */} - {/* */} - {/* Увiйти через KPI ID*/} - {/* */} - {/**/} - + {showKpiIdAuthButton && ( + + + + + + Увiйти через KPI ID + + + )} ); }; -export default AuthContainerDefault; +export default AuthContainerDefault; \ No newline at end of file diff --git a/src/components/AuthContainerExternal.js b/src/components/AuthContainerExternal.js deleted file mode 100644 index 7575a551..00000000 --- a/src/components/AuthContainerExternal.js +++ /dev/null @@ -1,45 +0,0 @@ -import React, { useState } from 'react'; -import AuthForm from './AuthForm'; -import * as campus from '../CampusClient'; -import { useLocation } from 'react-router-dom'; - -const useQuery = () => { - const { search } = useLocation(); - - return React.useMemo(() => new URLSearchParams(search), [search]); -}; - -const AuthContainerExternal = () => { - const [authFail, setAuthFail] = useState(false); - const query = useQuery(); - - const authorize = async (e, { login, password }) => { - e.preventDefault(); - - const redirectionUrl = query.get('redirect_url'); - - try { - const req = await campus.externalAuth(login, password, query.get('appId'), redirectionUrl); - - if (req.ok) { - document.location.replace(redirectionUrl); - // TODO redirection back + passing auth token - } else { - setAuthFail(true); - } - } catch { - setAuthFail(true); - } - }; - - const dismissInvalid = () => setAuthFail(false); - - return ( - - ); -}; - -export default AuthContainerExternal; diff --git a/src/components/KPIIDLogin.js b/src/components/KPIIDLogin.js deleted file mode 100644 index 673c3b56..00000000 --- a/src/components/KPIIDLogin.js +++ /dev/null @@ -1,143 +0,0 @@ -import React from 'react'; -import { Link } from 'react-router-dom'; -import SupportInformationDialog from './SupportInformationDialog'; -import * as campus from '../CampusClient'; - -class KPIIDLogin extends React.Component { - state = { - phone: '', - secret: '', - authFail: false, - codeWasRequested: false, - }; - - componentDidMount = async () => { - if (!!(await campus.getCurrentUser())) { - this.props.history.push('/home'); - } - }; - - setPhone = (event) => { - this.setState({ phone: event.target.value }); - }; - - setSecret = (event) => { - this.setState({ secret: event.target.value }); - }; - - getCode = async (e) => { - e.preventDefault(); - - await campus.requestKpiIdSecret(this.state.phone); - - this.setState({ codeWasRequested: true }); - }; - - authorize = async (e) => { - e.preventDefault(); - - const user = await campus.authByKpiId(this.state.phone, this.state.secret); - await this.setState({ authFail: !user }); - - if (!!user) { - this.props.history.push(`/home`); - window.location.reload(); - } - }; - - render = () => ( - - - - - - - - - {' '} - {' '} - - Авторизацiя через KPI ID - - - - - - - - {this.state.codeWasRequested === true && ( - - - - )} - - {this.state.authFail && ( - - - this.setState({ authFail: false })} - data-dismiss="alert" - aria-hidden="true" - > - × - - Перевірте корректність телефону та коду. - - - )} - - - {this.state.codeWasRequested === false && ( - - )} - - {this.state.codeWasRequested === true && ( - - )} - - - - - - - Вiдновити втрачений пароль - - - - - - - - ); -} - -export default KPIIDLogin; diff --git a/src/config.json b/src/config.json index eb91e6d6..9e71a366 100644 --- a/src/config.json +++ b/src/config.json @@ -1,5 +1,6 @@ { "ApiEndpoint": "https://api.campus.kpi.ua/", "OldUIAddress": "https://campus.kpi.ua/", - "LoginPageAddress": "https://ecampus.kpi.ua/login" + "LoginPageAddress": "https://ecampus.kpi.ua/login", + "AppId": "3d1488ae-128e-4655-8ca2-1ef554379335" }