legacy/pairent_frontend_react/src/API/User.js

48 lines
1.1 KiB
JavaScript

import axios from 'axios';
import constants from '../constants';
const { API_ROOT, api_path } = constants;
class UserLoginResponse {
/** @type {string} */
session_key;
/** @type {string} */
openid_login;
/** @type {number} */
id;
}
class User {
constructor(data) {
for (const key in data) {
this[key] = data[key];
}
}
static restoreFromLocalStorage() {
}
/** @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('/api/auth/user/login'), response);
return data.data;
}
}
export { User, UserLoginResponse }