Skip to content
Snippets Groups Projects
Commit e20e2c77 authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

Show user in frontend

parent f986dfc2
No related branches found
No related tags found
1 merge request!44Feature/comp 208 google o auth poc
......@@ -7,6 +7,7 @@ Missing info is filled in from the survey db for some questions.
Registered as click cli command when installing compendium-v2.
"""
from __future__ import annotations
import logging
import math
import click
......
......
......@@ -11,6 +11,7 @@ from compendium_v2.routes.organization import routes as org_routes
from compendium_v2.routes.ec_projects import routes as ec_routes
from compendium_v2.routes.policy import routes as policy
from compendium_v2.routes.survey import routes as survey
from compendium_v2.routes.user import routes as user_routes
routes = Blueprint('compendium-v2-api', __name__)
routes.register_blueprint(budget_routes, url_prefix='/budget')
......@@ -21,6 +22,8 @@ routes.register_blueprint(org_routes, url_prefix='/organization')
routes.register_blueprint(ec_routes, url_prefix='/ec-project')
routes.register_blueprint(policy, url_prefix='/policy')
routes.register_blueprint(survey, url_prefix='/survey')
routes.register_blueprint(user_routes, url_prefix='/user')
logger = logging.getLogger(__name__)
......
......
import pkg_resources
from flask import Blueprint, jsonify, render_template, Response, redirect, url_for
from flask_login import current_user # type: ignore
from flask import Blueprint, jsonify, render_template, Response
from compendium_v2.routes import common
routes = Blueprint('compendium-v2-default', __name__)
......@@ -50,10 +49,6 @@ def index(path):
def survey_index(path):
is_api = path.startswith('api')
# implement login_required with a redirect to login instead of a 401
if not current_user.is_authenticated:
return redirect(url_for('authentication.login'))
if is_api:
# return 404 for API requests that don't match a route
return Response(status=404, mimetype='application/json', response='{"error": "Not Found"}')
......
......
import logging
from typing import Any, Union
from flask import Blueprint, jsonify
from flask_login import current_user, AnonymousUserMixin # type: ignore
from compendium_v2.db.auth_model import User
from compendium_v2.routes import common
routes = Blueprint('user', __name__)
logger = logging.getLogger(__name__)
USER_RESPONSE_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'user': {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'email': {'type': ['string', 'null']},
},
'required': ['name'],
'additionalProperties': False
}
},
'type': 'array',
'items': {'$ref': '#/definitions/charging'}
}
@routes.route('/', methods=['GET'])
@common.require_accepts_json
def charging_structure_view() -> Any:
"""
handler for /api/user/ requests
response will be formatted as:
.. asjson::
compendium_v2.routes.user.USER_RESPONSE_SCHEMA
:return:
"""
def _extract_data(entry: Union[User, AnonymousUserMixin]):
if isinstance(entry, AnonymousUserMixin):
return {
'name': 'Anonymous User',
}
return {
'name': entry.fullname,
}
return jsonify(_extract_data(current_user))
This diff is collapsed.
import React, { useState, useEffect } from "react";
interface User {
id?: string,
name: string,
email?: string
}
async function fetchUser(): Promise<User> {
try {
const response = await fetch('/api/user');
const user = await response.json();
return user
} catch (error) {
return {
'name': 'Error Fetching User'
}
}
}
function ShowUser() {
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
// Fetch user
fetchUser().then((user) => {
setUser(user);
});
}, []);
if (!user || user.name.match(/Anonymous/gi)) {
return (
<>
<h2>Not logged in.</h2>
<a href="/login">Login</a>
</>
);
}
return <h2>Hello, {user.name}!</h2>;
}
export default ShowUser;
\ No newline at end of file
......@@ -2,6 +2,7 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import SurveySelectionComponent from './SurveySelectionComponent';
import ShowUser from './ShowUser';
const container = document.getElementById('root') as HTMLElement;
......@@ -10,6 +11,7 @@ const root = createRoot(container);
root.render(
<React.StrictMode>
<ShowUser />
<SurveySelectionComponent />
</React.StrictMode>
)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment