From 77ab081aafb0ae566b8287e5bec196b54d852411 Mon Sep 17 00:00:00 2001 From: b1ek Date: Tue, 16 May 2023 15:15:37 +1000 Subject: [PATCH] use UserManager instead of client --- pairent_frontend_react/src/API/User.js | 25 ++++++++++++++++- pairent_frontend_react/src/constants.js | 27 +++++-------------- .../src/pages/LoggedIn/index.jsx | 8 +++--- .../src/pages/LoginPage/index.jsx | 7 +++-- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/pairent_frontend_react/src/API/User.js b/pairent_frontend_react/src/API/User.js index 0f029f8..4a24dd8 100644 --- a/pairent_frontend_react/src/API/User.js +++ b/pairent_frontend_react/src/API/User.js @@ -1,17 +1,40 @@ import axios from 'axios'; import constants from '../constants'; -const { API_ROOT } = constants; +const { API_ROOT, api_path } = constants; + +class UserLoginResponse { + /** @type {string} */ + session_key; + + /** @type {string} */ + openid_login; + + /** @type {number} */ + id; +} class User { constructor(data) { this = { ...data, ...this }; } + /** @param {string} id */ static async getById(id) { const data = await axios.post(API_ROOT + '/users/get', { id }); if (data.data['error']) throw new Error(data.data['error']); return new User(data.data); } + + /** @param {import('oidc-client-ts').SigninResponse} response */ + static async login(response) { + if (response.error !== null) { + throw new Error(response.error + ': ' + response.error_description); + return; + } + + const data = await axios.post(api_path('/auth/user/login'), response); + + } } \ No newline at end of file diff --git a/pairent_frontend_react/src/constants.js b/pairent_frontend_react/src/constants.js index 7c5e213..7e07e9a 100644 --- a/pairent_frontend_react/src/constants.js +++ b/pairent_frontend_react/src/constants.js @@ -1,5 +1,8 @@ import { nanoid } from 'nanoid' +/** @returns {string} */ +const api_path = path => API_ROOT + path; + /** * Api root path * @type {string} @@ -7,32 +10,14 @@ import { nanoid } from 'nanoid' const API_ROOT = window.location.protocol + '//127.0.0.1:8000'; // ДЛЯ ПРОДА ПОСТАВИТЬ ЭТО: '//pairent.vvsu.ru' -if (window.localStorage.getItem('oidc_client_key') == undefined) { - window.localStorage.setItem('oidc_client_key', nanoid(32)); -} - -const OIDC_CLIENT_KEY = window.localStorage.getItem('oidc_client_key'); - - /** OpenID Connect Client Config * @type {import('oidc-client-ts').OidcClientSettings} */ const OIDCConfig = { - onSignIn: () => {}, - authority: API_ROOT + '/api/auth/vvsu/', + authority: api_path('/api/auth/vvsu/'), client_id: 'it-hub-client', redirect_uri: 'https://pairent.vvsu.ru/sign-in/', - scope: [ - 'openid', - 'vvsu_IdUser', - 'vvsu_IdEmpl', - 'vvsu_IdStud', - 'vvsu_login', - 'given_name', - 'family_name' - ], - client_secret: OIDC_CLIENT_KEY + scope: 'openid vvsu_IdUser vvsu_IdEmpl vvsu_IdStud vvsu_login given_name family_name' }; - -export default Object.freeze({ API_ROOT, OIDCConfig, OIDC_CLIENT_KEY }); \ No newline at end of file +export default Object.freeze({ API_ROOT, OIDCConfig, api_path }); \ No newline at end of file diff --git a/pairent_frontend_react/src/pages/LoggedIn/index.jsx b/pairent_frontend_react/src/pages/LoggedIn/index.jsx index ceffe06..6f3165d 100644 --- a/pairent_frontend_react/src/pages/LoggedIn/index.jsx +++ b/pairent_frontend_react/src/pages/LoggedIn/index.jsx @@ -1,9 +1,11 @@ import React from "react"; import { styled } from "styled-components"; import { HashLoader } from "react-spinners"; -import { SigninResponse, SigninState } from 'oidc-client-ts'; +import { SigninResponse, SigninState, UserManager } from 'oidc-client-ts'; import FloatingBox from "../../components/UI/FloatingBox"; +import constants from "../../constants"; + const CenterContainer = styled.div` position: relative; top: 50%; @@ -34,8 +36,8 @@ export default class LoggedIn extends React.Component { } async componentDidMount() { - console.log(this.response) - console.log(this.signin_state); + if (this.response.error) return; + } render() { diff --git a/pairent_frontend_react/src/pages/LoginPage/index.jsx b/pairent_frontend_react/src/pages/LoginPage/index.jsx index bc89e0e..40b5ba4 100644 --- a/pairent_frontend_react/src/pages/LoginPage/index.jsx +++ b/pairent_frontend_react/src/pages/LoginPage/index.jsx @@ -58,11 +58,10 @@ export default class LoginPage extends React.Component { OpenID.Log.setLogger(console); OpenID.Log.setLevel(OpenID.Log.DEBUG); - let client = new OpenID.OidcClient(OIDCConfig); + let client = new OpenID.UserManager(OIDCConfig); - const req = await client.createSigninRequest({}); - window.localStorage.setItem('oidc_signin_state', req.state.toStorageString()); - window.location.href = req.url; + client.signinRedirect(); + } render() {