use UserManager instead of client
This commit is contained in:
parent
14704c36d4
commit
77ab081aaf
|
@ -1,17 +1,40 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import constants from '../constants';
|
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 {
|
class User {
|
||||||
constructor(data) {
|
constructor(data) {
|
||||||
this = { ...data, ...this };
|
this = { ...data, ...this };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param {string} id */
|
||||||
static async getById(id) {
|
static async getById(id) {
|
||||||
const data = await axios.post(API_ROOT + '/users/get', { id });
|
const data = await axios.post(API_ROOT + '/users/get', { id });
|
||||||
if (data.data['error'])
|
if (data.data['error'])
|
||||||
throw new Error(data.data['error']);
|
throw new Error(data.data['error']);
|
||||||
return new User(data.data);
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,8 @@
|
||||||
import { nanoid } from 'nanoid'
|
import { nanoid } from 'nanoid'
|
||||||
|
|
||||||
|
/** @returns {string} */
|
||||||
|
const api_path = path => API_ROOT + path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Api root path
|
* Api root path
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
@ -7,32 +10,14 @@ import { nanoid } from 'nanoid'
|
||||||
const API_ROOT = window.location.protocol + '//127.0.0.1:8000';
|
const API_ROOT = window.location.protocol + '//127.0.0.1:8000';
|
||||||
// ДЛЯ ПРОДА ПОСТАВИТЬ ЭТО: '//pairent.vvsu.ru'
|
// ДЛЯ ПРОДА ПОСТАВИТЬ ЭТО: '//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
|
/** OpenID Connect Client Config
|
||||||
* @type {import('oidc-client-ts').OidcClientSettings}
|
* @type {import('oidc-client-ts').OidcClientSettings}
|
||||||
*/
|
*/
|
||||||
const OIDCConfig = {
|
const OIDCConfig = {
|
||||||
onSignIn: () => {},
|
authority: api_path('/api/auth/vvsu/'),
|
||||||
authority: API_ROOT + '/api/auth/vvsu/',
|
|
||||||
client_id: 'it-hub-client',
|
client_id: 'it-hub-client',
|
||||||
redirect_uri: 'https://pairent.vvsu.ru/sign-in/',
|
redirect_uri: 'https://pairent.vvsu.ru/sign-in/',
|
||||||
scope: [
|
scope: 'openid vvsu_IdUser vvsu_IdEmpl vvsu_IdStud vvsu_login given_name family_name'
|
||||||
'openid',
|
|
||||||
'vvsu_IdUser',
|
|
||||||
'vvsu_IdEmpl',
|
|
||||||
'vvsu_IdStud',
|
|
||||||
'vvsu_login',
|
|
||||||
'given_name',
|
|
||||||
'family_name'
|
|
||||||
],
|
|
||||||
client_secret: OIDC_CLIENT_KEY
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default Object.freeze({ API_ROOT, OIDCConfig, api_path });
|
||||||
export default Object.freeze({ API_ROOT, OIDCConfig, OIDC_CLIENT_KEY });
|
|
|
@ -1,9 +1,11 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { styled } from "styled-components";
|
import { styled } from "styled-components";
|
||||||
import { HashLoader } from "react-spinners";
|
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 FloatingBox from "../../components/UI/FloatingBox";
|
||||||
|
|
||||||
|
import constants from "../../constants";
|
||||||
|
|
||||||
const CenterContainer = styled.div`
|
const CenterContainer = styled.div`
|
||||||
position: relative;
|
position: relative;
|
||||||
top: 50%;
|
top: 50%;
|
||||||
|
@ -34,8 +36,8 @@ export default class LoggedIn extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
console.log(this.response)
|
if (this.response.error) return;
|
||||||
console.log(this.signin_state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -58,11 +58,10 @@ export default class LoginPage extends React.Component {
|
||||||
OpenID.Log.setLogger(console);
|
OpenID.Log.setLogger(console);
|
||||||
OpenID.Log.setLevel(OpenID.Log.DEBUG);
|
OpenID.Log.setLevel(OpenID.Log.DEBUG);
|
||||||
|
|
||||||
let client = new OpenID.OidcClient(OIDCConfig);
|
let client = new OpenID.UserManager(OIDCConfig);
|
||||||
|
|
||||||
const req = await client.createSigninRequest({});
|
client.signinRedirect();
|
||||||
window.localStorage.setItem('oidc_signin_state', req.state.toStorageString());
|
|
||||||
window.location.href = req.url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
Loading…
Reference in New Issue