use UserManager instead of client

This commit is contained in:
b1ek 2023-05-16 15:15:37 +10:00
parent 14704c36d4
commit 77ab081aaf
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 38 additions and 29 deletions

View File

@ -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);
}
}

View File

@ -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 });
export default Object.freeze({ API_ROOT, OIDCConfig, api_path });

View File

@ -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() {

View File

@ -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() {